The order in which the CreateChildControls () and ApplyChanges () methods are called in the SharePoint Web Part

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

The problem is that when I click OK in edit mode, the page switches to view mode, but the data (propeties) that were changed in the editor and saved in the ApplyChanges () method are not updated. I have to go back to the page (reload without POSTing data) to see the change.

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

I realized 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, this causes the WebDataTree or UltraWebTree controls (from Infragistics) to be added, but this can also happen with a regular ASP.NET text block (as described here for the same problem in more detail: ASP.net forum topic ).

So, if I add a tree, CreateChildControls () is called first, and ApplyChanges is called second, so this is not relevant. I have to update to see the changes made to the editor part.

If I comment on adding a tree to the collection of controls, ApplyChanges is called first, and everything is fine (except that I need this tree :)) ...

Does anyone know what might cause this strange behavior?

+4
source share
4 answers

Procedure for calling methods and evetns: CreateChildControls Apply changes OnPreRender

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

+4
source

I'm not sure if this is what you are facing, but the problem that I had seems a bit similar, so I will describe it here along with my solution.

I have problems with synchronization in my parts of the editor with some types of user interface controls (namely dropdowns). My problem is that my web part property has a drop / value value, but when I create a part of my editor, it still does not have drop-down list items when SynchChanges () is called, so I cannot set my snapshot value to this time. I handle this with a synchronization member variable as follows.

private DropDownList _dropDownList; private string _syncDropDownId; public override SyncChanges() { // This will make sure CreateChildControls() is called // but that doesn't help me with my drop down list data // which is loaded in OnPreRender() this.EnsureChildControls(); MyWebPart webPart = this.WebPartToEdit as MyWebPart; // Temporarily store the drop down value for now // since our drop down is not fully built yet _syncDropDownId = myWebPart.SomeId; } protected override void CreateChildControls() { base.CreateChildControls(); // Create our drop down list, but don't populate it yet _dropDownList = new DropDownList(); this.Controls.Add(_dropDownList); } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); // Load drop down list items _dropDownList.Items.AddRange(GetListItems()); // Select item in drop down list based on web part property if (_syncDropDownId != null) { if (_dropDownList.Items.FindByValue(_syncDropDownId) != null) { _dropDownList.SelectedValue = _syncDropDownId; } _syncDropDownId = null; } } 
+2
source

You can force refresh the page using:

Page.Response.Redirect (Page.Request.Url.ToString ());

0
source

Order of use of the methods

 protected override void CreateChildControls() { } public override void SyncChanges() { } public override bool ApplyChanges() { } 
0
source

All Articles