Getting the Button_Click Event before Page_Load?

F

Frank Schumacher

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
 
G

Guest

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(object sender, EventArgs e)
{
if(!Page.IsPostback)
SetControls(true);
}

private void SetControls(bool IsInSession)
{
if(IsInSession)
//Set controls from Session
else
//Set controls with new state
}

public void Button1_Click(object sender, EventArgs e)
{
//Kill session
Session.Abandon();

SetControls(false);
}

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.IsPostback).

---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
F

Frank Schumacher

Hi Cowboy,

thanks for your reply!

Your answers gave me some insight into my problem, but I need to think
it over a bit more. But anyway, I think you have pointed me in a good
direction, from which I can go further.

Ciao,
Frank
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top