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

Discussion in 'ASP .Net Web Controls' started by Andreas Klemt, Jul 9, 2004.

  1. Hello,
    how can I find out in Page_Load which button event is fired?

    Thanks for any help in advance!!
    Andreas
    Andreas Klemt, Jul 9, 2004
    #1
    1. Advertising

  2. Andreas Klemt

    Scott G. Guest

    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
    Scott G., Jul 9, 2004
    #2
    1. Advertising

  3. Andreas Klemt

    DalePres Guest

    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
    DalePres, Jul 10, 2004
    #3
  4. 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
    Andreas Klemt, Jul 11, 2004
    #4
  5. Andreas Klemt

    Scott G. Guest

    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
    Scott G., Jul 11, 2004
    #5
  6. Andreas Klemt

    Scott G. Guest

    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" <> 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
    Scott G., Jul 11, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    0
    Views:
    357
    Andreas Klemt
    Feb 27, 2004
  2. jorge
    Replies:
    2
    Views:
    1,261
    jorge
    May 26, 2004
  3. Andreas Klemt
    Replies:
    1
    Views:
    450
    John Saunders
    Jul 9, 2004
  4. rockdale
    Replies:
    1
    Views:
    655
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Nov 16, 2006
  5. WebBuilder451
    Replies:
    1
    Views:
    754
    WebBuilder451
    Aug 8, 2008
Loading...

Share This Page