Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get submit button id

Tags:

c#

.net

asp.net

Inside asp.net form I have few dynamically generated buttons, all of this buttons submit a form, is there a way to get which button was submit the form in page load event?

like image 748
Arsen Mkrtchyan Avatar asked Dec 02 '25 19:12

Arsen Mkrtchyan


1 Answers

The sender argument to the handler contains a reference to the control which raised the event.

private void MyClickEventHandler(object sender, EventArgs e)
{
    Button theButton = (Button)sender;
    ...
}

Edit: Wait, in the Load event? That's a little tricker. One thing I can think of is this: The Request's Form collection will contain a key/value for the submitting button, but not for the others. So you can do something like:

protected void Page_Load(object sender, EventArgs e)
{
    Button theButton = null;
    if (Request.Form.AllKeys.Contains("button1"))
        theButton = button1;
    else if (Request.Form.AllKeys.Contains("button2"))
        theButton = button2;
    ...
}

Not very elegant, but you get the idea..

like image 183
Tor Haugen Avatar answered Dec 04 '25 07:12

Tor Haugen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!