【问题标题】:Dynamic Button in Repeater Postback中继器回发中的动态按钮
【发布时间】:2010-06-18 18:48:14
【问题描述】:

我在另一个Repeater 的ItemTemplate 中有一个嵌套的Repeater 控件。我想根据它们的数据值动态地将删除和更新按钮添加到嵌套中继器中的某些项目,在这种情况下它们是否与当前用户相关联。单击任一按钮应基于服务器端处理,以便可以更新数据源,重新绑定嵌套的转发器,并更新包含两个转发器的更新面板。

<asp:Repeater ID="rpProCom" runat="server">
    <HeaderTemplate><div class="comment"></HeaderTemplate>
    <ItemTemplate>
        <div class="c-score"><%# Eval("Text") %></div>
        <asp:UpdatePanel ID="upOut" runat="server" ChildrenAsTriggers="true" UpdateMode="Always" >
        <ContentTemplate>
        <asp:Repeater ID="rpVotes" runat="server" DataSource='<%# ((ScoreBoard.Score)Container.DataItem).Votes %>' OnItemDataBound="rpVotes_ItemDataBound">
            <HeaderTemplate><ul></HeaderTemplate>
            <ItemTemplate>
            <li>
            <div class="c-ctrl"><asp:PlaceHolder ID="phD" runat="server" /></div>
            <div class="c-box"><!-- placeholder for more tools --></div>
            <div class="c-i"><div runat="server" ID="imgCom" class='<%# Eval("ImgClass") %>' /></div>
            <div class="c-head">
                <strong><%# Eval("UserName") %></strong>
                <br /><%# Eval("Dt")%>
            </div>
            <div class="c-body"><%# Eval("Comment") %></div>
            </li></ItemTemplate><FooterTemplate></ul></FooterTemplate>
        </asp:Repeater>
        </ContentTemplate></asp:UpdatePanel>
    </ItemTemplate> 
    <FooterTemplate></div></FooterTemplate>
</asp:Repeater>

外部中继器绑定在Page_Init中

protected void rpVotes_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
    if (((Vote)e.Item.DataItem).UserName == user.UserName){
        LinkButton btn = new LinkButton();
        btn.Text = "delete";
        btn.CommandName = "delCom";
        btn.CommandArgument = ((Vote)e.Item.DataItem).ID.ToString();
        btn.Click += new EventHandler(DeleteComment);
        btn.ID = "d" + ((Vote)e.Item.DataItem).ID.ToString();
        //find contrl
        e.Item.FindControl("phD").Controls.Add(btn);
    }
    }
}

由于按钮是在数据绑定项上创建的并且嵌套在另一个控件中,我想不出一种合理的方法来将按钮和数据项的唯一标识符保留在 Viewstate 中,因此我可以重新创建按钮在 PageInit 回发以触发按钮的 Click 事件。

我尝试使用命令参数,但它们是空的:

javascript:__doPostBack('ctl00$CPHRight$rpProCom$ctl02$rpVotes$ctl01$d21','')

我需要对包含中继器的引用和中继器中项目的唯一标识符,以便我可以更新数据库并重新绑定该中继器(理想情况下,在仅包含该中继器的 UpdatePanel 中)。我知道这是一个经典问题,我相信有人已经解决了。

所以基本上,有没有办法将命令参数强制到在 ItemDataBound 期间动态创建的动态控件中,或者以其他方式识别哪个动态创建的按钮导致回发?我可以在中继器中放置和更新面板吗?我也可以在这篇文章中再次使用动态这个词吗?

【问题讨论】:

    标签: c# asp.net viewstate repeater web-controls


    【解决方案1】:

    您可以随心所欲地动态使用 :-)

    我建议声明一个静态按钮,添加一个 itemdatabound 事件,并通过设置按钮的 Visible 属性来显示/隐藏按钮;这样,您就不必担心重新加载问题,但用户将无法看到他们无权访问的按钮。

    HTH。

    【讨论】:

    • 我也在想同样的事情,但转发器中可能有数百个项目,只有大约 5-10% 的项目需要一个按钮。添加到所有这些会导致 ViewState 膨胀,如果可能的话,我想避免这种情况。这是一个很好的建议。
    • 相反,如果你有很多这样的项目,添加一个动态按钮可能是一场噩梦。
    • 权衡选项后,我继续前进并接受了打击。太糟糕了,没有办法(我能想到的)禁用 ViewState,然后在 DataBinding 中选择性地启用它。我想这只是页面生命周期的本质。感谢您的共鸣板。
    • 你可以禁用视图状态并在每次页面加载时绑定...这始终是一个选项,但这可能会对数据库产生很多影响。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 2014-11-03
    相关资源
    最近更新 更多