473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Event ordering problem

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.
Nov 18 '05 #1
3 1644
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.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net

"Rich" <none> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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.

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

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke
"Rich" <none> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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.

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ken Fine | last post by:
(originally posted to one of macromedia's groups; no help, so hopefully someone here can help me out. I'm using VBScript ASP.) When designing administrative interfaces to websites, we often need...
6
by: Ken Fine | last post by:
(originally posted to one of macromedia's groups; no help, so hopefully someone here can help me out. I'm using VBScript ASP.) When designing administrative interfaces to websites, we often need...
2
by: D. Dante Lorenso | last post by:
First I created a function that selected the next available pin code from a table of pre-defined pin codes: CREATE FUNCTION "public"."get_next_pin_code" () RETURNS varchar AS' DECLARE...
0
by: cherryparadise001 | last post by:
Hi All, I have moved an asp system from Window Server 2000 to Window Server 2003 recently. Many error occured and most of them are unable to get the control's value in the page. I am newbie to...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.