e.CommandArgument for asp按钮不起作用

lia*_*ali 20 asp.net aspbutton commandargument

我正在使用C#开发一个asp.net应用程序.我创建了一个.aspx页面,并在页面的不同位置放置了四个按钮.在服务器端,我想对所有四个按钮只使用一次单击事件.

这是我的代码:

aspx页面

<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" />
Run Code Online (Sandbox Code Playgroud)

cs页面

protected void allbuttons_Click(object sender, EventArgs e)
{
    //Here i want to know which button is pressed
    //e.CommandArgument gives an error
}
Run Code Online (Sandbox Code Playgroud)

Gra*_*mas 40

@Tejs的评论是正确的,看起来你想要这样的东西:

protected void allbuttons_Click(object sender, EventArgs e)
{
    var argument = ((Button)sender).CommandArgument;
}
Run Code Online (Sandbox Code Playgroud)


AGu*_*ald 9

使用

OnCommand = 
Run Code Online (Sandbox Code Playgroud)

protected void allbuttons_Click(object sender, CommandEventArgs e) { }
Run Code Online (Sandbox Code Playgroud)