在 asp.net 表单中我有几个动态生成的按钮,所有这些按钮都提交表单,有没有办法在页面加载事件中获取哪个按钮提交表单?
最佳答案
处理程序的 sender
参数包含对引发事件的控件的引用。
private void MyClickEventHandler(object sender, EventArgs e)
{
Button theButton = (Button)sender;
...
}
编辑:等等,在 Load 事件中?这是一个小骗子。我能想到的一件事是:请求的表单集合将包含提交按钮的键/值,但不包含其他键/值。所以你可以这样做:
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;
...
}
不是很优雅,但你明白了..
关于c# - 获取提交按钮 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1536930/