asp 按钮的 e.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" />
cs页面
protected void allbuttons_Click(object sender, EventArgs e)
{
//Here i want to know which button is pressed
//e.CommandArgument gives an error
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
@Tejs 在他的评论中是正确的,看起来你想要这样的东西:
@Tejs is correct in his comment, looks like you want something like this:
使用
和
Use
and
实际上,您根本不需要传递 CommandArgument 来知道您按下了哪个按钮。您可以获取按钮的 ID,如下所示:
Actually you don't need to pass the CommandArgument at all to know which button you pressed. You can get the ID of the button like below:
您可以将命令文本分配给按钮,如下所示:
You can assign command-text to your buttons as follows: