I’ve seen programmers writting in Page_Load like
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
}
}
So that the codes will be worked only once.
If so, why don’t we put that code in Page_InitComplete? Since Page_InitComplete is done only once.
What are the pros and cons by doing so?
I don’t see any advantage to what you’re suggesting but I can see problems with it. For one thing, server controls do not yet have their data loaded during
InitComplete. For another thing, code in this event executes regardless if whether or not it’s a postback.Use the
Loadevent. That’s what it’s for. It only executes once per request. If you want to restrict code to only run on postbacks, or only run during non-postback requests, testIsPostBack.