How to get gridview cell value iterating with JS or JQuery

五迷三道 提交于 2021-02-11 14:32:58

问题


I need to get cell values by iterating my GridView with JQuery. The thing is that my gridview in a content placeholder, so I can't call my grid by using #GridId. The way I call a grid in JQuery is using $('[id$=GridId']). I'm new in JQuery :p


回答1:


Let's say you have a GridView like this:

<form id="form1" runat="server">
    <asp:GridView ID="MyGridView" runat="server">
    </asp:GridView>
</form>

You can get values of cell with iterating by using each as following:

<script type="text/javascript">
        $(document).ready(function() {
              $("#<%=MyGridView.ClientID %> tr").each(function() {
                    var firstCellValue = $(this).find("td:eq(1)").html(); 
                    var lastCellValue = $(this).find("td:last").html();
                    //etc...
                });
        });
    </script>

Remember if you have table header then you should skip first row:

if (!this.rowIndex) return;



回答2:


In case if grid view is in content placeholder then inspect your Grid view in browser and copy Id of grid view (i.e. Id of your content placeholder + "_" + Id of Grid view ex: ContentPlaceholder1_GridView1) and use that combined ID in JQuery.

Then get Rows from grid Then get Columns for each row.

For reference have a look : https://www.aspsnippets.com/Articles/Get-selected-Row-Cell-value-in-GridView-using-jQuery-in-ASPNet.aspx



来源:https://stackoverflow.com/questions/59457475/how-to-get-gridview-cell-value-iterating-with-js-or-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!