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