我正在创建一个社交网络网站,我在这里面临的问题是我在转发器内部使用了一个更新面板,并且当用户单击任何链接按钮时,我想刷新updatepanel的内容,当然,此updatepanel将被重复foreach(div-post),但是当我在转发器控件和updatepanel中单击任何linkbutton时,它不会刷新。如何强制刷新?这是我正在使用的代码...

<asp:ScriptManager ID="ScriptManager1" runat="server">
                    </asp:ScriptManager>
                          <asp:Repeater ID="Repeater1" runat="server"
                                OnItemCommand="MyButtonCommandEvent">
                                <ItemTemplate>
                                <asp:UpdatePanel ID="UpdatePanel3" runat="server">
                        <ContentTemplate>
                                    <div class="post">
                                        <asp:HiddenField ID="postID_hidden" runat="server"
                                            Value='<%# Eval("posts_ID") %>' />
                                        <div class="Thumb">
                                            <img src="thumbs/abdo_thumb.jpg"> </img></div>
                                        <span class="user"><%#Eval("poster_name")%>
                                        </span>
                                        <div class="post-body">
                                            <p>
                                                <%#Eval("description")%>
                                            </p>
                                        </div>
                                        <div class="post-options" style=" height:22px; ">
                                            <span class="first"><%#Eval("post_date")%></span>
                                            <ul style="display:inline; list-style-type: none;">
                                                <li>
                                                    <div class="tooltip">
                                                        Comments
                                                        <img class="tool-img" src="Images/comments.png"> : <%#Eval("comment_num") %>
                                                        </img>
                                                    </div>
                                                </li>
                                                <li>
                                                    <div class="tooltip">
                                                        <asp:LinkButton ID="like_linkbtn" runat="server" CommandName="Like" OnClick="Like_Click"><%#(Eval("name_like").ToString() == "") ? "Like" : Eval("name_like")%></asp:LinkButton>
                                                        <img class="tool-img" src="images/likes.png"> : <%#Eval("like_counter") %>
                                                        </img></div>
                                                </li>
                                                <li>
                                                    <div class="tooltip">
                                                        <asp:LinkButton ID="hate_linkbtn" runat="server" CommandName="Hate"><%#(Eval("name_hate").ToString() == "") ? "Hate" : Eval("name_hate")%></asp:LinkButton>
                                                        <img class="tool-img" src="images/hate.png"> : <%#Eval("hate_counter") %>
                                                        </img></div>
                                                </li>
                                            </ul>
                                        </div>
                                        <div class="finish">
                                        </div>
                                    </div>
                                    </ContentTemplate>
                    </asp:UpdatePanel>
                                </ItemTemplate>
                            </asp:Repeater>


和服务器端代码

  protected void Like_Click(object sender, EventArgs e)
    {
        LinkButton1.Text = DateTime.Now.ToString();
    }
  protected void MyButtonCommandEvent(object source, RepeaterCommandEventArgs e)
    {
        var postID = e.Item.FindControl("postID_hidden") as HiddenField;
        NewUser user = new NewUser();
        if (e.CommandName == "Like")
        {
            user.like_post(int.Parse(postID.Value), (int)Session["accountID"]);
        }
        else
        {
            if (e.CommandName == "Hate")
            {
                user.hate_post(int.Parse(postID.Value), (int)Session["accountID"]);
            }
        }

      // repeater_databind();//This will refresh all the contents of the repeater(bad way)
    }


因此,可以看到databind对我不起作用,因为我只想刷新转发器控件中的一项而不是整个项,有人可以帮助我吗?

编辑:
通过将整个转发器包装在另一个updatepanel中并像这样设置触发器来解决问题

<asp:UpdatePanel ID="UpdatePanel4" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
                    <ContentTemplate>
                    <Triggers>
    <asp:AsyncPostBackTrigger ControlID="Repeater1" EventName="MyButtonCommandEvent" />
</Triggers>

最佳答案

您应该尝试将更新面板的UpdateMode属性设置为Conditional

关于c# - 如何使用updatepanel在Repeater控件中刷新单个项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9049305/

10-13 06:59