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

Does anyone know how to count the number of rows in asp: GridView using jQuery. If no rows are found, I want to do something ...

+5
source share
4 answers

A GridView just displays as a standard HTML table, so just count the number of tr elements in a GridView:

 var totalRows = $("#<%=GridView1.ClientID %> tr").length; 
+10
source

Each GridView creates HTML, which is basically a table, and that table has an identifier (the source of the look at your output page to see what they are talking about). You can pass the identifier either from .Net to JavaScript using myGridView.ClientID , or in ASP.NET 4 do ClientIdMode="Static" and thus use the same identifier that you use for the ASP control.

Then in jquery (which is a client-side layer that is completely separate from the GridView level), take this id and counter:

 $("#mygridviewid tr").length; 
+5
source

You can assign a CSS class to your gridview using the CssClass property (I don't remember the exact spelling), and then access its selectors from the jquery css class.

Suppose you assign gridviewclass to this property, and then when you write -

$('table.gridviewclass')

in jquery, you can access the table that is created instead by gridview ASP.NET. Now, to access all the lines, you will write -

 $('table.gridviewclass tr') 

which will give you all the rows of this table inside the jquery array. To count the number of lines you will write -

 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 line you can use the following selector -

 $('table.gridviewclass tr:first') 

To access the last line you will write -

 $('table.gridviewclass tr:last') 

etc .. You can find a complete list of jquery selectors here .

Hope this helps.

+1
source

I tried this var totalRows = $("#<%=GridView1.ClientID %> tr").length; and it failed when I tried

 var count = $get("mygridviewclientid").rows.length 

he gave an account of all the lines (th and tr) I also made sure that the attribute ClientIDMode="Static"

+1
source

Source: https://habr.com/ru/post/895876/


All Articles