There are a lot of articles about life-cycle of the page and custom controls. The problem is they are all looked at separately. I had to do a bit of research on the order of control events within the events of the web page and I would like to share my findings.
To see what happens I created two items:
- TestCustomControl.cs — custom composite control that gets included on Default.aspx. Adds a line to the log for the following events/methods:
OnInit
LoadViewState
OnLoad
CreateChildControls
SaveViewState
OnPreRender
Render
- Default.aspx page — contains TestCustomControl and submit button. Adds line to the log for the following events:
Page_Init
Page_Load
Page_PreRender
Then I opened Default.aspx page. Here is the log of events:
Control: OnInit
Page: Page_Init
Page: Page_Load
Control: OnLoad
Page: Page_PreRender
Control: CreateChildControls
Control: PreRender
Control: SaveViewState
Control: Render
Looks pretty much what you expected. Here is what happens when I clicked on submit button:
Control: OnInit
Page: Page_Init
Control: LoadViewState
Control: CreateChildControls
Control: LoadPostData
Page: Page_Load
Control: OnLoad
Page: Page_PreRender
Control: PreRender
Control: SaveViewState
Control: Render
There are a couple of differences. LoadViewState and LoadPostData are now included in the cycle. The biggest difference (that has caused many headaches) is that 'CreateChildControls' now gets called before "Page_Load/OnLoad". Let's say you have a simple composite control that contains a text box and some other controls:
public class TextBoxPlus : CompositeControl
{private TextBox _txt;
…#region Class Properties
public string TextBoxValue
{get {return (_txt.Text);}
set {_txt.Text=value;}}
#endregion
protected override void CreateChildControls()
{_txt = new TextBox();
_txt.ID = "txtTemp ";
_txt.CssClass = this.CssClass;
_txt.Columns = 3;
_txt.MaxLength = 3;Controls.Add(_txt);
…
}
protected override void Render(HtmlTextWriter output)
{…
}
}
The issue with CreateChildControls is that if you set 'TextBoxValue' property of the control in 'Page_Load' event of the page – you will get an exception that '_txt' doesn't exist. It does work without any problems if you set that value after the first form submit.
So:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.isPostBack)
textBoxPlus.Value="SomeValue"; //Throws exception
else
textBoxPlus.Value="SomeValue"; //Works without any problems
}
To fix this problem modify TextBoxValue property as follows
public string Value
{get
{EnsureChildControls();
return _txt.Text;
}
set
{EnsureChildControls();
_txt.Text=value;
}
}
EnsureChildControls() checks if CreateChildControls() has been called and if it hasn't — calls it.
$anchor$basketball Betting,final Four,final Four Betting,final Four Gambling,final Four Sports Book,final Four Sportsbook,march Madness,march Madness Betting,march Madness Gambling,march Madness Sports Book,march Madness Sportsbook,ncaa,ncaa Betting,…
$anchor$basketball Betting,final Four,final Four Betting,final Four Gambling,final Four Sports Book,final Four Sportsbook,march Madness,march Madness Betting,march Madness Gambling,march Madness Sports Book,march Madness Sportsbook,ncaa,ncaa Betting,nc…
Trackback by $anchor$basketball Betting,final Four,final Four Betting,final Four Gambling,final Four Sports Book,final Four Sportsbook,march Madness,march Madness Betting,march Madness Gambling,march Madness Sports Book,march Madness Sportsbook,ncaa,ncaa Betting,ncaa — March 9, 2008 @ 5:17 pm |