如何使用updatepanel刷新Repeater控件中的单个项目

时间:2012-01-28 22:47:15

标签: c# asp.net

我正在建立一个社交网站,我在这里遇到的问题是我在转发器中使用了更新面板,当用户点击任何链接按钮时,我想刷新updatepanel的内容将重复foreach(div-post),但是当我点击repeater控件和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对我不起作用,因为我只想刷新转发器控件中的一个项而不是整个项目,有人可以帮我吗?

修改: 通过将整个转发器包装在另一个更新面板中并像这样设置触发器来解决问题

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

1 个答案:

答案 0 :(得分:0)

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