获取提交按钮ID

Ars*_*yan 3 .net c# asp.net

在asp.net表单中我有几个动态生成的按钮,所有这些按钮都提交了一个表单,有没有办法在页面加载事件中获取哪个按钮提交表单?

Tor*_*gen 8

sender处理程序的参数包含对引发事件的控件的引用.

private void MyClickEventHandler(object sender, EventArgs e)
{
    Button theButton = (Button)sender;
    ...
}
Run Code Online (Sandbox Code Playgroud)

编辑:等待,在Load事件中?那是一个小问题.我能想到的一件事是:Request的Form集合将包含提交按钮的键/值,但不包含其他键.所以你可以这样做:

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;
    ...
}
Run Code Online (Sandbox Code Playgroud)

不是很优雅,但你明白了......