如何使用asp.net Web表单中的ajax更新ListView中的值

时间:2017-01-05 08:54:12

标签: c# asp.net ajax listview

我在asp.net网络表单中使用listview和ajax。在该表格的一部分中,我显示评论,读者可以评价正面或负面。 除非刷新页面,否则此值不会更新,是否有办法更新值而无需刷新页面?

<asp:ScriptManager ID="ScriptManager" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel" runat="server">
            <ContentTemplate>
                <asp:ListView ID="ListView1" runat="server" OnItemCommand="ListView1_ItemCommand">
                    <ItemTemplate>
                        <div class="row comm_info_bar ">
                            <div class="col-md-5 RightDisplay"><%# Eval("name") %></div>
                            <div class="col-md-5 comm_info_date"><%# Eval("date") %></div>

                            <asp:LinkButton ID="negBtn" class="glyphicon glyphicon-minus voteCommentIcon voteContNeg text-danger smallGlyph" runat="server" CommandName="negative" CommandArgument='<%#Eval("id")%>' />

                            <asp:Label ID="lblnegative" name="lblnegative" class=" voteNumNeg" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "negative") %>'></asp:Label>

                            <asp:LinkButton ID="posBtn" class="glyphicon glyphicon-plus voteCommentIcon voteContNeg text-success smallGlyph" runat="server" CommandName="positive" CommandArgument='<%#Eval("id")%>' />
                            <asp:Label ID="lblpositive" class="voteNumPos" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "positive") %>'></asp:Label>

                        </div>
                        <div class="row">
                            <div class="col-md-12 comments"><%# Eval("text") %></div>
                        </div>
                    </ItemTemplate>
                </asp:ListView>
            </ContentTemplate>
        </asp:UpdatePanel>

并在代码中:

static List<Int64> commentsUser = new List<long>();
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    string name = e.Item.DataItemIndex.ToString();
    long commentId = Convert.ToInt64(e.CommandArgument);
    ArticleCommentsDataClass ArticleComment = new ArticleCommentsDataClass();

    if (e.CommandName == "positive")
    {
        if (!searchcomments(commentId))
        {
            ArticleComment.Comments_positive(commentId);
            commentsUser.Add(commentId);
        }
    }
    else
    {
        if (!searchcomments(commentId))
        {
            ArticleComment.Comments_negative(commentId);
            commentsUser.Add(commentId);
        }
    }
}

有没有人知道如何做到这一点?

1 个答案:

答案 0 :(得分:0)

这听起来像是在执行命令之前绑定(读取网格数据)。由于命令会更改数据,因此您应该重新绑定网格或以任何其他方式更新它。

查看此文章talks about when each event is raised