Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

e.CommandArgument for asp button is not working

Tags:

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
}
like image 831
liaqat ali Avatar asked Apr 15 '11 12:04

liaqat ali


People also ask

What is an ASP button?

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"

Why is my asp button Not Working?

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


2 Answers

@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;
}
like image 123
Grant Thomas Avatar answered Dec 27 '22 18:12

Grant Thomas


Use

OnCommand = 

and

protected void allbuttons_Click(object sender, CommandEventArgs e) { }
like image 26
AGuyCalledGerald Avatar answered Dec 27 '22 19:12

AGuyCalledGerald