Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count the rows in a gridview in asp.net using jQuery

Tags:

jquery

asp.net

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

like image 904
chugh97 Avatar asked Aug 11 '10 08:08

chugh97


2 Answers

A GridView is just rendered as a standard HTML table, so just count the number of tr elements under the GridView:

var totalRows = $("#<%=GridView1.ClientID %> tr").length;
like image 115
djdd87 Avatar answered Nov 15 '22 04:11

djdd87


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.

Then in jquery (which is a client-side layer that is completely seperated from the GridView layer), grab that ID and count:

$("#mygridviewid tr").length;
like image 34
Ayyash Avatar answered Nov 15 '22 05:11

Ayyash