得到提交按钮ID按钮、ID

2023-09-03 06:43:03 作者:一颦一笑为君留

在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?

推荐答案

发件人参数的处理程序包含一个引用其引发事件的控制。

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

编辑:等等,在Load事件?这是一个小特里克。有一件事我能想到的是:请求的Form集合将包含一个键/值对提交按钮,而不是为别人。所以,你可以这样做:

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