469,908 Members | 2,213 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,908 developers. It's quick & easy.

Getting the Button_Click Event before Page_Load?

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
Nov 18 '05 #1
2 2332
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!
***************************
"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

Nov 18 '05 #2
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
Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by David Thielen | last post: by
1 post views Thread by =?Utf-8?B?VmlrcmFt?= | last post: by
2 posts views Thread by Cindy Lee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.