获取点击了哪个链接按钮

发布于 2024-12-17 03:48:43 字数 797 浏览 3 评论 0原文

我正在开发 ASP.NET/C# 应用程序。

我有这样的链接按钮

<asp:LinkButton ID="LinkButton1" runat="server" OnClick="Link_Click">Link1</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" OnClick="Link_Click">Link2<Chart</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" OnClick="Link_Click">Link3</asp:LinkButton>

有没有办法知道在 Link_Click 事件中单击了哪一个?

我不想为每个链接创建不同的事件。

我正在寻找这样的东西:

protected void Link_Click(object sender, EventArgs e)
{
    string LinkClicked = Get_Which_Link_Has_Been_Clicked();

    if(LinkClicked == "Link1")
    {
        //DoSomething;
    }
    else if(LinkClicked == "Link2")
    {
        //Do something else;
    }
    //and so on;
}

提前致谢。

I am working on an ASP.NET/C# Application.

I have Link buttons like this

<asp:LinkButton ID="LinkButton1" runat="server" OnClick="Link_Click">Link1</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" OnClick="Link_Click">Link2<Chart</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" OnClick="Link_Click">Link3</asp:LinkButton>

Is there a way to know which one was clicked in the Link_Click event?

I don't want to create a different event for each link.

I am looking for something like this:

protected void Link_Click(object sender, EventArgs e)
{
    string LinkClicked = Get_Which_Link_Has_Been_Clicked();

    if(LinkClicked == "Link1")
    {
        //DoSomething;
    }
    else if(LinkClicked == "Link2")
    {
        //Do something else;
    }
    //and so on;
}

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

面犯桃花 2024-12-24 03:48:43

尝试将传递给函数的 sender 值转换为 LinkBut​​ton,然后从中获取 id -

LinkButton lbtn = (LinkButton)sender;
string id = lbtn.ID;

Try casting the sender value passed to the function to a LinkButton then getting the id from that -

LinkButton lbtn = (LinkButton)sender;
string id = lbtn.ID;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文