user3179960
user3179960

Reputation: 11

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.

Upvotes: 1

Views: 615

Answers (2)

rdmptn
rdmptn

Reputation: 5601

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;
}

Upvotes: 1

Brandon Ording
Brandon Ording

Reputation: 456

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.

Upvotes: 0

Related Questions