Webforms 中继器链接问题

发布于 2024-11-27 04:40:31 字数 109 浏览 0 评论 0原文

我有一个中继器,其中一列中有一个 LinkBut​​ton,并且连接了 onclick 事件。当用户单击其中一个选项时,我需要知道他们单击了哪个 LinkBut​​ton。执行此操作的最佳做​​法是什么?

I have a repeater that has a LinkButton in one of its columns and I have the onclick event wired up. When the user clicks on one of the options, I need to know in the event which LinkButton they clicked on. What is the best practice to do this?

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

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

发布评论

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

评论(4

鸠书 2024-12-04 04:40:31

您应该使用 OnCommand 事件而不是 OnClick 使用一些 CommandNameCommandArgument 来区分黑白项目。 此 MSDN 页面例子。

You should use the OnCommand event instead of OnClick use some CommandName and CommandArgument to distinguish b/w items. This MSDN page has an example.

狼亦尘 2024-12-04 04:40:31

通常 CommandArgument='<%#Eval("Id") 用于此类目的

<asp:LinkButton ID="LinkButton1" runat="server" 
CommandArgument='<%#Eval("Id") %>' CommandName="commandName"></asp:LinkButton>

,然后它会像...

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if(e.CommandName == "commandName")
    {
       Int32 id = Convert.ToInt32(e.CommandArgument);
    }
}

Normally CommandArgument='<%#Eval("Id") is used for such purpose

<asp:LinkButton ID="LinkButton1" runat="server" 
CommandArgument='<%#Eval("Id") %>' CommandName="commandName"></asp:LinkButton>

and then it will be like...

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if(e.CommandName == "commandName")
    {
       Int32 id = Convert.ToInt32(e.CommandArgument);
    }
}
不顾 2024-12-04 04:40:31

您想要做的是连接 Repeater 的 ItemCommand 事件,而不是使用 LinkBut​​ton 的 OnClick 事件。而是连接 LinkBut​​ton 的 CommandName

当 ItemCommand 触发时,您将能够根据按钮上设置的 CommandName 判断哪个按钮触发了它。您还可以访问中继器该行中的所有控件。

微软软件定义网络
http://msdn.microsoft.com /en-us/library/system.web.ui.webcontrols.repeater.itemcommand.aspx

What you want to do is wire up the Repeater's ItemCommand event and not use the LinkButton's OnClick event. Instead wire up the CommandName of the LinkButton.

When the ItemCommand fires you'll be able to tell what button triggered it based on the CommandName that was set on the button. You'll also have access to all the controls in that row of the Repeater.

MSDN
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemcommand.aspx

枫以 2024-12-04 04:40:31

检查此代码

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            repTriggers.DataSource = new int[3] { 0, 1, 2 };
            repTriggers.DataBind();
        }
    }

    protected void repTriggers_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "trigger")
        {
            LinkButton btn = e.CommandSource as LinkButton;

            if (btn != null)
            {
                lblUpdate.Text = "Update triggered by " + btn.ID + e.Item.ItemIndex.ToString();
            }

            // [Steve] removed UpdatePanel2.Update()
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="TheScriptManager" runat="server"></asp:ScriptManager>

    <%-- [Steve] removed UpdatePanel1 --%>    
    <asp:Repeater ID="repTriggers" runat="server" OnItemCommand="repTriggers_ItemCommand">
        <ItemTemplate>
            <asp:LinkButton ID="lnkTrigger" runat="server" Text="Trigger" CommandName="trigger"></asp:LinkButton>
        </ItemTemplate>
    </asp:Repeater>

    <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="conditional">
        <%-- [Steve] added repTriggers as an AsyncPostBackTrigger --%>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="repTriggers" />
        </Triggers>
        <ContentTemplate>
            <asp:Label ID="lblUpdate" runat="server"></asp:Label>
        </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html>

源URL http://forums.asp.net/p/1062060/1528071 .aspx

Check with this code

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            repTriggers.DataSource = new int[3] { 0, 1, 2 };
            repTriggers.DataBind();
        }
    }

    protected void repTriggers_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "trigger")
        {
            LinkButton btn = e.CommandSource as LinkButton;

            if (btn != null)
            {
                lblUpdate.Text = "Update triggered by " + btn.ID + e.Item.ItemIndex.ToString();
            }

            // [Steve] removed UpdatePanel2.Update()
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="TheScriptManager" runat="server"></asp:ScriptManager>

    <%-- [Steve] removed UpdatePanel1 --%>    
    <asp:Repeater ID="repTriggers" runat="server" OnItemCommand="repTriggers_ItemCommand">
        <ItemTemplate>
            <asp:LinkButton ID="lnkTrigger" runat="server" Text="Trigger" CommandName="trigger"></asp:LinkButton>
        </ItemTemplate>
    </asp:Repeater>

    <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="conditional">
        <%-- [Steve] added repTriggers as an AsyncPostBackTrigger --%>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="repTriggers" />
        </Triggers>
        <ContentTemplate>
            <asp:Label ID="lblUpdate" runat="server"></asp:Label>
        </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html>

Source URL http://forums.asp.net/p/1062060/1528071.aspx

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