|
For one of my SharePoint webparts I created a custom EditorPart to edit the properties of the webpart. I had some issues with this editorpart that caused me a headache.My webpart is an ASP.NET 2.0 webpart (System.Web.UI.WebControls.WebParts) that implements the IWebEditable interface. Here is the code of my webpart:
public class TestEditorPart : System.Web.UI.WebControls.WebParts.WebPart, IWebEditable { private string _myMessage; [WebBrowsable(false)] [Personalizable(PersonalizationScope.Shared)] public string MyMessage { get { return _myMessage; } set { _myMessage = value; } } protected override void Render(HtmlTextWriter writer) { base.Render(writer); writer.WriteLine(string.Format("Message: {0}.", MyMessage)); } EditorPartCollection IWebEditable.CreateEditorParts() { List<EditorPart> editors = new List<EditorPart>(); editors.Add(new MyEditorPart()); return new EditorPartCollection(editors); } object IWebEditable.WebBrowsableObject { get { return this; } } }
In CreateEditorParts() my custom editorpart is created and returned in a new collection. My editor part is called MyEditorPart. I will not discuss the code in detail, because it is pretty straight forward.
public class MyEditorPart : EditorPart { private TextBox _message; protected override void CreateChildControls() { base.CreateChildControls(); _message = new TextBox(); Controls.Add(_message); } public override bool ApplyChanges() { EnsureChildControls(); TestEditorPart webPart = WebPartToEdit as TestEditorPart; if (webPart != null) { webPart.MyMessage = _message.Text; } return true; } public override void SyncChanges() { EnsureChildControls(); TestEditorPart webPart = WebPartToEdit as TestEditorPart; if (webPart != null) { _message.Text = webPart.MyMessage; } } }
After compiling the assembly, deploying it to my SharePoint 2007 server and registering the webpart, I added the webpart to the page. After clicking the “Modify”Shared Web Part””, the page crashes with message “An unexpected error has occurred.”:

After adding a constructor to MyEditorPart and setting an ID for my editor part, this problem is solved.
public MyEditorPart() { this.ID = "MyEditorPart"; }
While testing the webpart, I used this webpart twice on the same page. I changed the message for the first webpart to “Message 1”. Then I set the message for the other webpart to a different text and switched back to the properties of the first webpart. Although I have the correct webpart selected (see screenshot below) the textbox in the editor part shows the message of the other webpart. See the screenshot below.

After some serious debugging I found out that the ID of the editor part has to be unique for each instance of your webpart. I changed the constructor of my EditorPart to take the ID of the webpart as a parameter. This solved my problem.
public MyEditorPart(string webPartID) { this.ID = "MyEditorPart" + webPartID; }
In the webpart code I changed CreateEditorParts to pass the ID in the constructor:
EditorPartCollection IWebEditable.CreateEditorParts() { List<EditorPart> editors = new List<EditorPart>(); editors.Add(new MyEditorPart(this.ID)); return new EditorPartCollection(editors); }
You probably already know this if you develop SharePoint webpart, but I didn’t and after nearly getting crazy because my text boxes displayed the wrong values in the editor part, I decided to share it.
|