Order of calling of CreateChildControls() and ApplyChanges() methods in a SharePoint web part

sharepointweb-parts

I am creating a web part for SharePoint. I have a custom editor part which is overriding the SyncChanges() and ApplyChanges() methods (among others).

The problem is that when I click OK in edit mode, the page is switched to browse mode, but data (propeties) which were changed in the EditorPart and saved in the ApplyChanges() method are not updated. I must 'enter the page' again (reload without POSTing the data again) to see the change that was made.

I debugged it and figured out what it is doing – after clicking OK in edit mode, WebPart.CreateChildControls() is called first, and EditorPart.ApplyChanges() second. So the data was updated but being displayed was the non-updated data.

I figured something else in this behavior: Adding one specific control to my WebPart in CreateChildControls() causes the wrong order of calling WebPart.CreateChildControls() and EditorPart.ApplyChanges(). In my case it causes adding of the WebDataTree or UltraWebTree controls (from Infragistics) but it can also happen with the common ASP.NET TextBox (as described the same problem in detail here: ASP.net forum thread).

So if I add the tree, CreateChildControls() is called first and ApplyChanges second, so it is not actual. I must refresh to see the changes I made in the editor part.

If I comment adding the tree to the controls collection, ApplyChanges is called first and everything is ok (except I need that tree 🙂 )…

Does anyone know what can cause this weird behavior?

Best Answer

The order of calling methods and evetns is this: CreateChildControls ApplyChanges OnPreRender

So if you access the properties in CreateChildControls they are not current. So I moved the code that access webpart properties from CreateChildControls to OnPreRender and everything works correctly.

Related Topic