简体   繁体   中英

How can I identify, in an ASP.NET Page_Load event, what RadButton initiated a postback?

In my ASP.NET page's Page_Load I'm trying to determine whether a certain button has been clicked and is attempting a postback:

if (Page.IsPostBack)
{
    if (Request.Params.Get("__EVENTARGUMENT") == "doStuff")
      doSomething();
}

doStuff is a JavaScript within the markup, but I don't want this alone to trigger the doSomething() method call, I also need to be able to ensure that the button the user has clicked is correct.

How can I identify and reference the button control from the code behind once the user clicks? I searched for and discovered this but when I try to implement it, the control returned is always null .

Or use the CommandName and command events.

        <telerik:RadButton ID="RadButton1" runat="server" CommandName="first" CommandArgument="one" OnCommand="CommandHandler" />
        <telerik:RadButton ID="RadButton2" runat="server" CommandName="second" CommandArgument="two" OnCommand="CommandHandler" />
        <asp:Label ID="Label1" Text="" runat="server" />
        <asp:Label ID="Label2" Text="" runat="server" />

protected void  CommandHandler(object sender, CommandEventArgs e)
{
    Label1.Text = e.CommandArgument.ToString();
    Label2.Text = e.CommandName;
}

You haven't really explained what you're trying to do, but it sounds like you would have a much easier time if you added an OnClick event handler to the button instead of trying to figure it out in Page_Load . The event handler would only be executed when that button is clicked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM