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.