Event ordering problem

R

Rich

Hi everyone,
I have run into a quandry here. Here is a simplified scenario that
mirrors my real problem.

I have a page that has 2 buttons:
On page load, the page runs some complex initialization routines.
Clicking button 1 causes a postback in which I WANT the initialization
routines to run before the event handler. The net of this is that the same
page redisplays slightly differently.
Clicking button 2 causes a postback in which I DO NOT WANT the
initialization performed before the event handler. The net of this is that
I do some processing and move on to another page.

Here is the problem: The page load initialization routines are expensive,
and I really don't want to run these if the user is clicking that second
button. I need to know which event fired before completing the page load.
I really don't want to move where the main initialization routines are
(under page load) if I can help it. Any ideas how to 'peek the events'
prior to the page load?

Thanks,

Rich.
 
S

Steve C. Orr [MVP, MCSD]

To determine which button was clicked (if any) from within your Form_Load
event, you can loop through the Requesst.Form collection.
In the case that a button click caused the postback, you'll find an entry in
the Form collection that matches the button's Name attribute.

Another tip you may find useful is that the Page's PreRender event happens
after the button click events, so you might be able to move your init code
there and then you'll have been able to keep track of which button click
events were fired.
 
T

Teemu Keiski

Hi,

if speaking strictly about buttons (<asp:Button>) here is one way, though it
is an ugly one.

protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;

private void Page_Load(object sender, System.EventArgs e)
{
if(Request.Form[Button1.UniqueID] != null)
Response.Write("Button1 was clicked");

if(Request.Form[Button2.UniqueID] != null)
Response.Write("Button2 was clicked");
}

If you want to generalize this sort of checking, implementing the form post
collection check might make sense. Here is simplified example. First the
general method:

private bool WasControlCausedAPostBack(string uniqueId)
{
return (Request.Form[uniqueId] != null || (Request.Form["__EVENTTARGET"] !=
null && Request.Form["__EVENTTARGET"].Equals(uniqueId)));
}

Then example of its usage:

protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;

private void Page_Load(object sender, System.EventArgs e)
{
if(WasControlCausedAPostBack(Button1.UniqueID))
Response.Write("Button1 was clicked");

if(WasControlCausedAPostBack(Button2.UniqueID))
Response.Write("Button2 was clicked");
}

Works similarly for normal buttons as the previous one but is also capable
to handle it for other postbacking controls like LinkButtons. If you'd use
ImageButton, you'd need to also handle it differently as ImageButton sends
the clicked x and y coordinates with the form post collection, not it's
unique ID like Button (or LinkButton having its ID in posted __EVENTTARGET
field)
 
R

Rich

Thanks Steve. The form collection is an option for one particularly complex
page I have, but for several others, the prerender event was a real charm I
dropped it in a base page and the whole system seemed to come to life. Very
non-invasive!

Thanks a bunch,

Rich.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top