【问题标题】:update gridview field without dataBind在没有 dataBind 的情况下更新 gridview 字段
【发布时间】:2012-09-09 15:46:28
【问题描述】:

我有一个显示 cmets 的 gridview。我希望如果有人喜欢评论,请按点赞按钮并增加喜欢的总和,但我不想连接到数据库并绑定。我想使用 ajax 来做到这一点。我怎样才能做到这一点? 这是我的代码:

<asp:UpdatePanel runat="server" id="UpdatePanel" >
  <ContentTemplate>
    <asp:GridView ID="grdCommentsForLoginnedUser" 
                  runat="server" 
                  AutoGenerateColumns="False" 
                  Width="100%" 
                  OnRowCommand="grdCommentsForLoginnedUser_RowCommand"
                  ViewStateMode="Inherit">
      <Columns>
        <asp:TemplateField >
          <ItemTemplate>
            <asp:HiddenField ID="hdnCommentID" runat="server"
                   Value='<%# Bind("CommentID") %>'/>              
              <table border="2" dir="rtl">
              <tr>
                <td rowspan="2" style="background-color: #6699FF" width="200px">
                  <asp:HyperLink ID="lnkCommenterName"
                        runat="server"
                        NavigateUrl='<%# Bind("CommenterProfile") %>'
                        Text='<%# Bind("CommenterName") %>' />                     
                </td>
                <td width="700px">
                  <br />
                  <asp:Label ID="lblCommentText" runat="server"
                        Text='<%# Bind("CommentText") %>' />
                  <br /> 
                </td>
              </tr>
              <tr>
                <td width="700px">
                  <asp:Label ID="lblPreviousAcceptOrNonAccept" runat="server"
                        Text='<%# Bind("PersonPreviousAcceptOrNonAccept") %>' />
                  <asp:LinkButton ID="lnkBtnRemovePreviousAcceptOrNonAccept" 
                        runat="server"   
                        Text='<%# Bind("RemovePersonPreviousAcceptOrNonAccept") %>'
                        CommandName="RemovePreviousAcceptOrNonAccept" 
                        CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
                  <br />
                  <asp:LinkButton ID="lnkBtnUpRate" runat="server" Text="Like"
                        Visible='<%# Bind("isLikeAndUnlikeButtonVisible") %>'  
                        CommandName="LikeComment" 
                        CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
                  <br />sum of likes
                  <asp:Label ID="lblCommentLikes" runat="server"
                             Text='<%# Bind("CommentLikes") %>' />
                </td>
              </tr>
            </table>
          </ItemTemplate>
        </asp:TemplateField>
      </Columns>
    </asp:GridView>
  </ContentTemplate>
</asp:UpdatePanel>

【问题讨论】:

    标签: asp.net ajax gridview


    【解决方案1】:

    为每个按钮添加一个客户端onClick javascript。点击时获取父td标签,然后getElementById("lblCommentLikes")设置"lblCommentLikes"span.innerText属性。

    +(theTD.getElementById("lblCommentLikes").innerText)+=1;
    

    +(theTD.getElementById("lblCommentLikes").innerText)-=1;
    

    提示:如果您使用的是 IE,请按 F12 检查正在运行的页面的DOM

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-24
      • 2018-04-15
      • 2019-12-09
      • 2021-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-01
      相关资源
      最近更新 更多