转发器回传中的动态按钮

发布于 2024-09-06 08:50:32 字数 2878 浏览 4 评论 0原文

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

<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 期间动态创建的动态控件,或者以其他方式识别哪个动态创建的按钮导致回发?我可以将面板放入中继器中并进行更新吗?另外,我可以在这篇文章中再次使用“动态”这个词吗?

I have a nested Repeater control in the ItemTemplate of another Repeater. I want to dynamically add a delete and update button to certain items in the nested Repeater depending on their data value, in this case whether they're associated with the current user. A click to either button should handled server-side based so the data source can be updated, the nested repeater re-bound and an Update Panel that contains both repeaters would be updated.

<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>

The outer repeater is bound in 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);
    }
    }
}

Since the Button is created on item databound and is nested within another control, I can't think of a reasonable way to persist the button and the unique identifier of the Data Item in Viewstate so I can recreate the button(s) on PageInit postback in order to fire the button's Click event.

I tried using Command Arguments, but they are empty:

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

I need a reference to the containing Repeater and a unique identifier for the item in the repeater so I can update the DB and rebind that Repeater (ideally in an UpdatePanel that encompasses just that Repeater). I know this is a classic problem, and I'm sure someone's solved it.

So basically, is there some way to force a command argument into a dynamically control created dynamically during ItemDataBound or otherwise identify which dynamically created button caused postback? Can I put and Update Panel in a Repeater? Also can I possibly use the word dynamically again in this post?

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

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

发布评论

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

评论(1

暖阳 2024-09-13 08:50:32

您可以根据需要动态使用:-)

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

HTH。

You can use dynamically as much as you want :-)

What I would recommend is declaring a static button, adding an itemdatabound event, and showing/hiding the button instead by setting the Visible property of it; that way, you won't have to concern yourself with the reloading issues, but the user won't be able to see the buttons they don't have permissions for.

HTH.

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