使用AJAX刷新新内容

本文关键字:新内容 刷新 AJAX 使用 | 更新日期: 2023-09-27 18:01:08

在本例中,当按下ID为"PostCommentsButton"的按钮时,我希望触发此ContentTemplate,并在ID为"CommentListView"的ListView中再次迭代。但这在这里不起作用。我想念什么?

在这个例子中,我从textfield中获取新的文本,在代码后面,我用ado.net将textfield中的新内容放入数据库中。问题是,当按下UpdatePanel中的按钮时,新信息没有与其他内容一起出现在列表中。只有当我重新启动页面时,它才会出现。我希望UpdatePanel中的ListView在按下按钮时再次迭代,以使用AJAX从文本字段中获取这些新内容。我该怎么办?

aspx代码:

                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="PostCommentsButton" />
                    </Triggers>
                    <ContentTemplate>
                        <asp:ListView ID="CommentListView" runat="server" DataSource= '<%# Eval("Comments") %>'>
                            <ItemTemplate>
                                <div class="postComments">
                                    <span class="authorComment"><%# Eval("Author") %></span>
                                    :
                                    <span class="commentContent"><%# Eval("Message") %></span>
                                </div>
                            </ItemTemplate>
                        </asp:ListView>
                    </ContentTemplate>
                </asp:UpdatePanel>

代码背后:

   protected void PostsListView_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        //commandName is for recognize the clicked button
        if (e.CommandName == "postComment")
        {
            //get the comment from textbox from current listview iteration
            BlogProfileEntities blogProfile = new BlogProfileEntities();
            var commentBox = e.Item.FindControl("AddCommentTextbox") as TextBox;
            var hiddenFieldPostID = e.Item.FindControl("CurrentPostIDHiddenField") as HiddenField;
            string text = commentBox.Text;
            var postID = hiddenFieldPostID.Value;
            Comment newComment = new Comment()
            {
                Message = text,
                PostID = int.Parse(postID),
                Author = Membership.GetUser().UserName
            };
            blogProfile.Comments.Add(newComment);
            blogProfile.SaveChanges();

使用AJAX刷新新内容

我在您的示例中没有看到文本框。如果文本框不在更新面板中,当列表视图post反触发时,它会被调用,我认为你不会得到当前值。我怀疑,如果你在commentBox变量上加一个断点,你会看到它返回为Nothing。如果这不是您的完整代码,请发布所有内容。