Get submit button id
7 replies
The argument sender
to the handler contains a reference to the control that raised the event.
private void MyClickEventHandler(object sender, EventArgs e)
{
Button theButton = (Button)sender;
...
}
Edit: Wait, at the loading stage? This is a bit of a trick. One thing I can think of is this: the collection of the request form will hold the key / value for the submit button, but not the rest. Therefore, 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 ...
+8
source to share
protected void Page_Load(object sender, EventArgs e) {
string id = "";
foreach (string key in Request.Params.AllKeys) {
if (!String.IsNullOrEmpty(Request.Params[key]) && Request.Params[key].Equals("Click"))
id = key;
}
if (!String.IsNullOrEmpty(id)) {
Control myControl = FindControl(id);
// Some code with myControl
}
}
+1
source to share
Use the CommandArgument property to determine which button represents the form.
Edit: I just got it, you said you need this in PageLoad, this only works for the Click server side event, not for PageLoad.
-2
source to share