Page_Load is used to load the page. If you are using it to set up items that
a page needs when loaded, you will not have a problem with event order.
AFAIK, you cannot change the order of events.
The easiest way to avoid issues is only set page state for when IsPostback =
false. Then, you will not collide with items in Page_Load when you click a
button. This may require a bit of rearchitecture.
Consider moving the code that determines state into its own routine. When
IsPostback = false, call the state to pull from Session vars. When button
click, IsPostback = true, so call a routine to drop session from button click
and then call the routine that determines state. The basics of the code is
something like this:
public void Page_Load(objec t sender, EventArgs e)
{
if(!Page.IsPost back)
SetControls(tru e);
}
private void SetControls(boo l IsInSession)
{
if(IsInSession)
//Set controls from Session
else
//Set controls with new state
}
public void Button1_Click(o bject sender, EventArgs e)
{
//Kill session
Session.Abandon ();
SetControls(fal se);
}
Too often, the Page_Load event is used as Control Central, which is a
mistake. Unfortunately, the mistake is propagated by many books on the market
that use an else condition on if(!Page.IsPost back).
---
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
*************** ************
Think Outside the Box!
*************** ************
"Frank Schumacher" wrote:
Hi Folks,
I have a problem with the order of events fired by ASP.NET. I found many
articles which explaining the lifecycle of a site, but I found none
which took the event from a Control on the site into consideration.
Here is what I want to do:
I have a Button, which starts a new search session. When this session is
started, you have several Usercontrols, which can navigate within the
search session.
So: If the button is clicked, a new search session is started. I do not
need to care about any previous states.
If the button is *NOT* clicked, I have to build up the site from some
Session-Variables, which I have saved bevor.
Unfortunatly, the Button_Click event is the last in line. This would
mean, that I need to build up the site first in Page_Load and then, in
Button_Click, redo all my work and init a new search session.
So is it possible, to get to know, if the button was clicked *before*
all other Page-Events are fired?
Thanks,
Frank