get submit button id

后端 未结 7 718
抹茶落季
抹茶落季 2021-01-21 02:57

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?

7条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-21 03:41

    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..

提交回复
热议问题