开发者

how to count number of rows in gridview in javascript?

开发者 https://www.devze.com 2023-03-30 22:40 出处:网络
As the question says, i wish to count the number of rows in gridview via JS. I am doing the way it is done here but that is not coming up 开发者_JAVA技巧correctly.

As the question says, i wish to count the number of rows in gridview via JS. I am doing the way it is done here but that is not coming up 开发者_JAVA技巧correctly.

I have also tried different ways as:

 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 updatepanel. Would that make any difference? What is the right statement?


If you want to get the number of rows from the server one way would be to use:

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

It is also possible to send the data to JavaScript from codebehind.


var GridId = "<%=Questionsedit.ClientID %>";            
var grid = document.getElementById(GridId);
rowscount = grid.rows.length;


DataTable dt = //configure datasource here 
GridView1.DataSource = dt;
GridView1.DataBind();
HiddenField1.value = GridView1.Rows.Count.ToString();

var  count = document.getElementById('HiddenField1');

alert(count.value);

This seems to have worked for someone in this forum post.


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

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

This will render the grid rows with the class specified.

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

Then you can count the rows using the class selector

var rowscount = $(".gridrow").length;


Found the reason: Because the grid is included in content page, the javascript had to be included under form tag. It runs well! Thanks all for inputs!!


We can simplify that,

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


try this:

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消