Web Part requires clicking twice the Apply/OK button to apply the changes
ASP.NET, Development, SharePoint, Tips & TricksRecently I was debugging a Web Part which had an issue with applying its changes. It required clicking the Apply/OK button twice in order to apply the changes. While this problem is quite commonly seen in different examples on the Internet, the solution is very simple.
To illustrate this issue I have created a simple Web Part which displays the text typed in the Web Part’s properties:
Right after adding the value of the My Property property is empty:
Then after having provided some text in the value of My Property you click the Apply button… and nothing changes. The Web Part still doesn’t display the text:
After clicking the Apply button again, the Web Part finally displays the changes:
The Web Part was created using the following code:
public class MyWebPart : WebPart
{
[Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("My Property")]
public string MyProperty { get; set; }
protected Literal literal;
protected override void CreateChildControls()
{
base.CreateChildControls();
literal = new Literal();
Controls.Add(literal);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
EnsureChildControls();
literal.Text = MyProperty;
}
}
Although the above example is very simple and doesn’t really requires the Literal control, I’ve decided to use it instead of the RenderContents method, to give you a good example of how to solve the issue while working with controls.
The reason of the issue that I mentioned, is the lifecycle of controls in ASP.NET and in particular the order in which different events occur. In this particular scenario the event responsible for synchronizing the properties from Web Part’s Editor Pane with the Web Part itself occurs after the Load event. So if the Web Part uses the OnLoad event handler to read the value of one of its properties, it will still get the old value.
The fix to this challenge is very simple. What you can do is to move the implementation from the Load event to the PreRender event:
public class MyWebPart : WebPart
{
[Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("My Property")]
public string MyProperty { get; set; }
protected Literal literal;
protected override void CreateChildControls()
{
base.CreateChildControls();
literal = new Literal();
Controls.Add(literal);
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
EnsureChildControls();
literal.Text = MyProperty;
}
}
Using exactly the same code you can make your Web Part behave as it is supposed to! Every change done to the value of the My Property property, will be applied with a single click.
Although you cannot see the results here, I strongly encourage you to test the code above: both the original one using the OnLoad event handler and the one using the OnPreRender event handler.

















January 6th, 2010 at 7:37 am
Hi Waldek,
nice post on a very common issue. The following post illustrates how the web part life-cycle events change on post back:http://platinumdogs.wordpress.com/2008/10/14/sharepoint-webpart-lifecycle-events/ . In particular the CreateChildControls method fires before OnLoad and ConnectionConsumer, and web part properties load after OnLoad and before PreRender. OnPreRender is always the best life-cycle to bind controls with data.
All the best,
Radi A.
August 11th, 2011 at 9:21 pm
I'm finding that the ItemCommand event does not fire when databinding a repeater in the OnPreRender method. Haven't yet found a workaround for this one.
John
December 1st, 2011 at 11:56 pm
Thank you very much Waldek for writing such a wonderful post. I was stuck in this issue of configuration update. I click Ok after changing configuration to close webpart configuration window. The configuration did not reflect on client side paging used on spgrid using tablesorter jQuery plugin. I need to refresh the page to reflect the new paging configuration. But your post pull me out from that stucky issue. Thanks again.
December 2nd, 2011 at 12:01 pm
@Syed: Great to hear I could help :)