asp 按钮的 e.CommandArgument 不起作用

发布于 2024-11-02 05:28:59 字数 760 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

攒眉千度 2024-11-09 05:28:59

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

protected void allbuttons_Click(object sender, EventArgs e)
{
    var argument = ((Button)sender).CommandArgument;
}

@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;
}
花间憩 2024-11-09 05:28:59

使用

OnCommand = 

protected void allbuttons_Click(object sender, CommandEventArgs e) { }

Use

OnCommand = 

and

protected void allbuttons_Click(object sender, CommandEventArgs e) { }
请帮我爱他 2024-11-09 05:28:59

实际上,您根本不需要传递 CommandArgument 来知道您按下了哪个按钮。您可以获取按钮的 ID,如下所示:

string id = ((Button)sender).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:

string id = ((Button)sender).ID;
奢华的一滴泪 2024-11-09 05:28:59

您可以将命令文本分配给按钮,如下所示:

protected void allbuttons_Click(Object sender, CommandEventArgs e) {
    switch(e.CommandName) {
        case "Button1":
            Message.Text = "You clicked the First button";
            break;
        case "Button2":
            Message.Text = "You clicked the Second button";
            break;
        case "Button3":
            Message.Text = "You clicked Third button";
            break;
        case "Button4":
            Message.Text ="You clicked Fourth button";
            break;
    }
}

You can assign command-text to your buttons as follows:

protected void allbuttons_Click(Object sender, CommandEventArgs e) {
    switch(e.CommandName) {
        case "Button1":
            Message.Text = "You clicked the First button";
            break;
        case "Button2":
            Message.Text = "You clicked the Second button";
            break;
        case "Button3":
            Message.Text = "You clicked Third button";
            break;
        case "Button4":
            Message.Text ="You clicked Fourth button";
            break;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文