jQuery 如何使用jQuery计算asp.net中gridview中的行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3456723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to count the rows in a gridview in asp.net using jQuery
提问by chugh97
Does anyone know how to count the no of rows in asp:GridView using jQuery. If no rows found then I want to do stuff...
有谁知道如何使用 jQuery 计算 asp:GridView 中的行数。如果没有找到行,那么我想做一些事情......
回答by djdd87
A GridView
is just rendered as a standard HTML table, so just count the number of tr
elements under the GridView:
AGridView
只是呈现为标准的 HTML 表格,因此只需计算tr
GridView 下的元素数量:
var totalRows = $("#<%=GridView1.ClientID %> tr").length;
回答by Ayyash
Every GridView produces HTML that is basically a table and that table has an ID (view source of your output page to know what im talking about). You can pass the ID either from .Net to JavaScript by means of myGridView.ClientID
or in ASP.NET 4 make the ClientIdMode="Static"
and thus use the exact same ID you use for the ASP control.
每个 GridView 生成的 HTML 基本上是一个表格,并且该表格有一个 ID(查看输出页面的源代码以了解我在说什么)。您可以通过myGridView.ClientID
或在 ASP.NET 4 make中将 ID 从 .Net 传递到 JavaScript ClientIdMode="Static"
,从而使用与用于 ASP 控件的完全相同的 ID。
Then in jquery (which is a client-side layer that is completely seperated from the GridView layer), grab that ID and count:
然后在 jquery(这是一个与 GridView 层完全分离的客户端层)中,获取该 ID 并计数:
$("#mygridviewid tr").length;
回答by MD Sayem Ahmed
You can assign a CSS class to your gridview using CssClass
(I don't remember the exact spelling) property, and then access it jquery's css class selectors.
您可以使用CssClass
(我不记得确切的拼写)属性为您的 gridview 分配一个 CSS 类,然后访问它 jquery 的 css 类选择器。
Suppose you have assigned gridviewclass
to that property, then when you write -
假设您已分配gridviewclass
给该属性,那么当您编写时-
$('table.gridviewclass')
$('table.gridviewclass')
in jquery, you will be able to access the table that is being generated in place of that gridview by ASP.NET. Now, to access all the rows, you will write -
在 jquery 中,您将能够访问由 ASP.NET 代替该 gridview 生成的表。现在,要访问所有行,您将编写 -
$('table.gridviewclass tr')
which will give you all the rows of that table inside a jquery array. To count the number of rows, you will then write -
这将为您提供 jquery 数组中该表的所有行。要计算行数,您将编写 -
var rowcount = $('table.gridviewclass tr').length
if(rowcount == 0)
{
// No rows found, do your stuff
}
else
{
// Rows found, do whatever you want to do in this case
}
To access the first row, you can use the following selector -
要访问第一行,您可以使用以下选择器 -
$('table.gridviewclass tr:first')
To access the last row, you will write -
要访问最后一行,您将编写 -
$('table.gridviewclass tr:last')
etc. You can find a whole list of jquery selectors here.
等等。您可以在此处找到完整的 jquery 选择器列表。
Hope that helps.
希望有帮助。
回答by Vin
I tried this
var totalRows = $("#<%=GridView1.ClientID %> tr").length;
and it failed
when I tried
我试过这个
var totalRows = $("#<%=GridView1.ClientID %> tr").length;
,当我尝试时它失败了
var count = $get("mygridviewclientid").rows.length
it gave the count of all the rows (th and tr)
I also made sure that the attribute ClientIDMode="Static"
它给出了所有行(th 和 tr)的计数我还确保该属性ClientIDMode="Static"