Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > How can I find out in Page_Load which button event is fired?

Reply
Thread Tools

How can I find out in Page_Load which button event is fired?

 
 
Andreas Klemt
Guest
Posts: n/a
 
      07-09-2004
Hello,
how can I find out in Page_Load which button event is fired?

Thanks for any help in advance!!
Andreas


 
Reply With Quote
 
 
 
 
Scott G.
Guest
Posts: n/a
 
      07-09-2004

Assuming that you know the button ID; the you can do something like this.Request["Btn1"] (i.e. look at the variables in the Request); if you don't necessarily know the button ID, I guess you could iterate over the Request values and look for the control and see what type of control is it.

Scott

<%@ Page language="c#" AutoEventWireup="false" Trace="true" %>
<script language="C#" runat="server">
protected override void OnLoad(EventArgs e)
{
this.Trace.Warn("OnLoad = " + this.Request["Btn1"]);
base.OnLoad(e);
}

public void Btn1_Click(object sender, System.EventArgs e)
{
this.Trace.Warn("Hey");
}
</script>
<html>
<body>
<form id="Form1" method="post" runat="server">
<asp:Button id="Btn1" runat="server" Text="Button" onclick="Btn1_Click"></asp:Button>
</form>
</body>
</html>
"Andreas Klemt" <> wrote in message news:...
Hello,
how can I find out in Page_Load which button event is fired?

Thanks for any help in advance!!
Andreas


 
Reply With Quote
 
 
 
 
DalePres
Guest
Posts: n/a
 
      07-10-2004
You can also use the __EventTarget and __EventArgument hidden form elements.

Dale

"Scott G." <> wrote in message news:...

Assuming that you know the button ID; the you can do something like this.Request["Btn1"] (i.e. look at the variables in the Request); if you don't necessarily know the button ID, I guess you could iterate over the Request values and look for the control and see what type of control is it.

Scott

<%@ Page language="c#" AutoEventWireup="false" Trace="true" %>
<script language="C#" runat="server">
protected override void OnLoad(EventArgs e)
{
this.Trace.Warn("OnLoad = " + this.Request["Btn1"]);
base.OnLoad(e);
}

public void Btn1_Click(object sender, System.EventArgs e)
{
this.Trace.Warn("Hey");
}
</script>
<html>
<body>
<form id="Form1" method="post" runat="server">
<asp:Button id="Btn1" runat="server" Text="Button" onclick="Btn1_Click"></asp:Button>
</form>
</body>
</html>
"Andreas Klemt" <> wrote in message news:...
Hello,
how can I find out in Page_Load which button event is fired?

Thanks for any help in advance!!
Andreas


 
Reply With Quote
 
Andreas Klemt
Guest
Posts: n/a
 
      07-11-2004
Hello Scott,
thank you for your answer and code.
I have now a question: For what do I use override OnLoad() and what
is the difference to Page_Load() ?

I tested also your code but when I load the page or hit the button, I never
get to see the ("OnLoad = " + this.Request["Btn1"]) in my trace.


Thanks in advance for your answer.

Kind Regards,
Andreas
"Scott G." <> schrieb im Newsbeitrag news:...

Assuming that you know the button ID; the you can do something like this.Request["Btn1"] (i.e. look at the variables in the Request); if you don't necessarily know the button ID, I guess you could iterate over the Request values and look for the control and see what type of control is it.

Scott

<%@ Page language="c#" AutoEventWireup="false" Trace="true" %>
<script language="C#" runat="server">
protected override void OnLoad(EventArgs e)
{
this.Trace.Warn("OnLoad = " + this.Request["Btn1"]);
base.OnLoad(e);
}

public void Btn1_Click(object sender, System.EventArgs e)
{
this.Trace.Warn("Hey");
}
</script>
<html>
<body>
<form id="Form1" method="post" runat="server">
<asp:Button id="Btn1" runat="server" Text="Button" onclick="Btn1_Click"></asp:Button>
</form>
</body>
</html>
"Andreas Klemt" <> wrote in message news:...
Hello,
how can I find out in Page_Load which button event is fired?

Thanks for any help in advance!!
Andreas


 
Reply With Quote
 
Scott G.
Guest
Posts: n/a
 
      07-11-2004

The difference between OnLoad and Page_Load is that OnLoad is just a overriden virtual method that corresponds to the "Load" event; the Page_Load is the wired up event handler. So, a "Load" event generates a call to OnLoad, which is turn generates calls to the "Load" event handlers (of which Page_Load is one).... if look at the docs you'll see that there are On* methods for each of the lifecycle events of the Page/Controls (so there's an OnInit, OnLoad, etc...).

The reason you aren't seeing the message is that you might be using Page_Load with the AutoEventWireup set to true. Here's the same code using the Page_Load handler, maybe you'll have an easier time with that.

Scott

<%@ Page language="c#" AutoEventWireup="true" Trace="true" %>
<script language="C#" runat="server">
public void Page_Load(object sender, EventArgs e)
{
this.Trace.Warn("OnLoad = " + this.Request["Btn1"]);
}

public void Btn1_Click(object sender, System.EventArgs e)
{
this.Trace.Warn("Hey");
}
</script>
<html>
<body>
<form id="Form1" method="post" runat="server">
<asp:Button id="Btn1" runat="server" Text="Button" onclick="Btn1_Click"></asp:Button>
</form>
</body>
</html>

"Andreas Klemt" <> wrote in message news:ubfw0%...
Hello Scott,
thank you for your answer and code.
I have now a question: For what do I use override OnLoad() and what
is the difference to Page_Load() ?

I tested also your code but when I load the page or hit the button, I never
get to see the ("OnLoad = " + this.Request["Btn1"]) in my trace.


Thanks in advance for your answer.

Kind Regards,
Andreas
"Scott G." <> schrieb im Newsbeitrag news:...

Assuming that you know the button ID; the you can do something like this.Request["Btn1"] (i.e. look at the variables in the Request); if you don't necessarily know the button ID, I guess you could iterate over the Request values and look for the control and see what type of control is it.

Scott

<%@ Page language="c#" AutoEventWireup="false" Trace="true" %>
<script language="C#" runat="server">
protected override void OnLoad(EventArgs e)
{
this.Trace.Warn("OnLoad = " + this.Request["Btn1"]);
base.OnLoad(e);
}

public void Btn1_Click(object sender, System.EventArgs e)
{
this.Trace.Warn("Hey");
}
</script>
<html>
<body>
<form id="Form1" method="post" runat="server">
<asp:Button id="Btn1" runat="server" Text="Button" onclick="Btn1_Click"></asp:Button>
</form>
</body>
</html>
"Andreas Klemt" <> wrote in message news:...
Hello,
how can I find out in Page_Load which button event is fired?

Thanks for any help in advance!!
Andreas


 
Reply With Quote
 
Scott G.
Guest
Posts: n/a
 
      07-11-2004

This is true for other controls like a LinkButton; but a plain old button doesn't use these hidden fields. The code below shows the difference.

Scott

<%@ Page language="c#" AutoEventWireup="true" Trace="true" %>
<script language="C#" runat="server">
public void Page_Load(object sender, EventArgs e)
{
this.Trace.Warn("OnLoad = " + this.Request["Btn1"]);
this.Trace.Warn("OnLoad2= " + this.Request["__EVENTTARGET"]);
}

public void Btn1_Click(object sender, System.EventArgs e)
{
this.Trace.Warn("Hey");
}
</script>
<html>
<body>
<form id="Form1" method="post" runat="server">
<asp:Button id="Btn1" runat="server" Text="Button" onclick="Btn1_Click"></asp:Button>
<asp:LinkButton id="Btn2" runat="server">Click to see __EVENTTARGET</asp:LinkButton>
</form>
</body>
</html>
"DalePres" <don-t-spa-m-me@lea-ve-me-a-lone--.com> wrote in message news:...
You can also use the __EventTarget and __EventArgument hidden form elements.

Dale

"Scott G." <> wrote in message news:...

Assuming that you know the button ID; the you can do something like this.Request["Btn1"] (i.e. look at the variables in the Request); if you don't necessarily know the button ID, I guess you could iterate over the Request values and look for the control and see what type of control is it.

Scott

<%@ Page language="c#" AutoEventWireup="false" Trace="true" %>
<script language="C#" runat="server">
protected override void OnLoad(EventArgs e)
{
this.Trace.Warn("OnLoad = " + this.Request["Btn1"]);
base.OnLoad(e);
}

public void Btn1_Click(object sender, System.EventArgs e)
{
this.Trace.Warn("Hey");
}
</script>
<html>
<body>
<form id="Form1" method="post" runat="server">
<asp:Button id="Btn1" runat="server" Text="Button" onclick="Btn1_Click"></asp:Button>
</form>
</body>
</html>
"Andreas Klemt" <> wrote in message news:...
Hello,
how can I find out in Page_Load which button event is fired?

Thanks for any help in advance!!
Andreas


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Materpage page_load event fired after content page_load? rockdale ASP .Net 1 11-16-2006 06:59 PM
How can I find out in Page_Load which button event is fired? Andreas Klemt ASP .Net 1 07-09-2004 04:12 PM
Why does page_load fire twice when inheriting from a common overridable Page_Load bminder ASP .Net 1 02-23-2004 08:54 PM
How to find out which button caused the onbeforeunload event? Sunil Menon Javascript 0 08-26-2003 02:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57