简体   繁体   English

如何在ASP.NET Web表单中使用Ajax更新ListView中的值

[英]How to update values in a ListView with ajax in asp.net web form

I'm using a listview and ajax in an asp.net web form. 我在asp.net Web表单中使用Listview和Ajax。 In part of that form, I display comments, which readers can rate, either positive or negative. 在该表格的一部分中,我显示了一些评论,读者可以对其进行正面或负面评价。 This value isn't updated unless the page is refreshed, is there a way to update the value without the need of refreshing the page? 除非刷新页面,否则不会更新此值,是否可以在不刷新页面的情况下更新该值?

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

and in code: 并在代码中:

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);
        }
    }
}

Is there anyone who has an idea on how to do this? 是否有人对如何执行此操作有想法?

This sounds like you are binding (reading the data of your grid) before executing the command. 听起来您在执行命令之前已经绑定(读取网格的数据)。 Since the command changes the data, you should rebind the grid or update it in any other way. 由于该命令更改了数据,因此您应该重新绑定网格或以任何其他方式更新它。

Check out this article that talks about when each event is raised 查看这篇文章,该文章讨论每个事件何时引发

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM