【问题标题】:How to get the accurate control in a repeater with Callback如何使用回调在中继器中获得准确控制
【发布时间】:2014-05-01 00:29:22
【问题描述】:

我开发了一个带有回发的页面,但最近我决定将其更改为使用回调

母版页

页面

中继器

td1

文字显示描述

td2

EditControl***(从查看模式开始,在使用保存按钮回发到编辑模式时)

所以我有 100 条记录,对于每个中继器项目,我可以使用编辑按钮在查看模式下进行控制(带有 href:javascript:void 的链接按钮和回调函数 Edit('',''))

当我点击编辑时,它会引发回调事件,但此时,假设我点击了转发器中的第 15 个控件,但回调事件认为它是转发器中的第一个控件,我的意思是第一行。

那么我该怎么做才能让我真正点击它的精确控件呢?

因为最后,我使用 HtmlTextWriter(RenderControl) 返回控件并将其发送到客户端接收器数据函数,以使用 innerHtml 渲染它,效果很好,但仅使用第一行中的第一个控件。

【问题讨论】:

    标签: c# user-controls callback


    【解决方案1】:

    您需要将CommandArgument 传递给控件,​​以便在引发事件时,您可以检索参数并知道单击了哪个按钮。

    您的中继器需要ItemCommand 事件:

    <asp:Repeater ID="repeater" runat="server" OnItemDataBound="repeater_OnItemDataBound" OnItemCommand="repeater_ItemCommand">
        <ItemTemplate>
            <asp:LinkButton ID="lnkBtnEdit" runat="server" Text="Edit" />
        </ItemTemplate>
    </asp:Repeater>
    

    在转发器ItemDataBound 事件中,您将向链接按钮传递一个命令参数:

    protected void repeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lnkBtnEdit = (LinkButton)e.Item.FindControl("lnkBtnEdit");
            lnkBtnEdit.CommandArgument = "foo args";
        }
     }
    

    接下来在ItemCommand 事件中,当点击其中一个链接按钮时将引发该事件,您将获得命令参数,告诉您点击了哪个链接按钮:

    protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
         //This is how you know which button was clicked
         string foo = e.CommandArgument;
    }
    

    实际上,您可能希望传入您正在单击的项目的对象 ID,这可能会重定向到以 ID 作为查询字符串参数的编辑页面。在编辑页面中,您将通过 ID 提取有关该对象的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      相关资源
      最近更新 更多