How to count the number of rows in gridview in javascript?

As the question says, I want to count the number of rows in a gridview via JS. I do the way it is done here , but it does not fit correctly.

I also tried different ways:

1. var rowscount = document.getElementByID('<%=Gridview1.ClientID%>').rows.length; 2. var rowscount = document.getElementByID("<%=Gridview1.ClientID%>").rows.length; 3. var rowscount = document.getElementByID('<%#Gridview1.ClientID%>').rows.length; 4. var rowscount = document.getElementByID("<%#Gridview1.ClientID%>").rows.length; 5. var rowscount = document.getElementByID("Gridview1.ClientID").rows.length; 6. var rowscount = document.getElementByID("Gridview1").rows.length; 

UPDATE : Forgot to mention: my gridview is inside the update panel. Does it really matter? What is the correct statement?

+6
source share
7 answers

Found a reason: since the grid is included in the content page, javascript should have been included in the form tag. It works well! Thank you all for your contribution!

0
source

If you want to get the number of rows from the server, one way:

 var rowsCount = <%=GridView1.Rows.Count %> 

You can also send JavaScript data from codebehind.

+6
source
 DataTable dt = //configure datasource here GridView1.DataSource = dt; GridView1.DataBind(); HiddenField1.value = GridView1.Rows.Count.ToString(); var count = document.getElementById('HiddenField1'); alert(count.value); 

It seems to have worked for someone in this forum.

+2
source

You can set the RowStyle.CssClass property for gridview and count them using jQuery.

 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" ...> <RowStyle CssClass="gridrow" /> </asp:GridView> 

This will display the grid lines with the specified class.

 <tr class="gridrow"> <td>row data here</td> </tr> 

Then you can read the rows using the class selector

 var rowscount = $(".gridrow").length; 
+1
source
 var GridId = "<%=Questionsedit.ClientID %>"; var grid = document.getElementById(GridId); rowscount = grid.rows.length; 
+1
source

try the following:

 var rowscount = $("#<%=GridView1.ClientID %> tr").length; 

or see:
How to count rows in gridview in asp.net using jQuery

0
source

We can simplify that

 var gridViewRowCount = document.getElementById("<%= GridView1.ClientID %>").rows.length; alert(gridViewRowCount); 
0
source

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


All Articles