I am developing a asp.net application using C#. I created an .aspx page and placed four buttons on different locations on the page. On server side, I want to use just one click event for all four buttons.
Here is my code:
aspx page
<asp:Button ID="Button1" runat="server" CommandArgument="Button1" onClick = "allbuttons_Click" />
<asp:Button ID="Button2" runat="server" CommandArgument="Button2" onClick = "allbuttons_Click" />
<asp:Button ID="Button3" runat="server" CommandArgument="Button3" onClick = "allbuttons_Click" />
<asp:Button ID="Button4" runat="server" CommandArgument="Button4" onClick = "allbuttons_Click" />
cs page
protected void allbuttons_Click(object sender, EventArgs e)
{
//Here i want to know which button is pressed
//e.CommandArgument gives an error
}
asp:Button is an asp.net server control which fire an event on the server side. <input id="Submit1" type="submit" is a client side button of type submit, but it can act as a server side button as well by adding. runat="server" and onserverclick="eventname"
check the CausesValidation property of your button. set it to False if its true. Also check your asp.net page code file link is exactly same in which you have put the event. Is the button inside a template control (such as an <asp:Repeater> or <asp:GridView> )?
@Tejs is correct in his comment, looks like you want something like this:
protected void allbuttons_Click(object sender, EventArgs e)
{
var argument = ((Button)sender).CommandArgument;
}
Use
OnCommand =
and
protected void allbuttons_Click(object sender, CommandEventArgs e) { }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With