如何在javascript中计算gridview中的行数?

时间:2021-09-04 09:12:16

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

正如问题所说,我希望通过JS计算gridview中的行数。我正在按照这里的方式进行,但是没有正确完成。

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?

更新:忘记提及:我的gridview在updatepanel内。那会有什么不同吗?什么是正确的陈述?

7 个解决方案

#1


6  

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.

也可以从代码隐藏中将数据发送到JavaScript。

#2


2  

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.

这似乎对这个论坛帖子中的某个人有用。

#3


1  

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

您可以为gridview设置RowStyle.CssClass属性,并使用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;

#4


1  

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

#5


0  

try this:

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

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

或者参见:如何使用jQuery计算asp.net中gridview中的行数

#6


0  

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

找到原因:因为网格包含在内容页面中,所以javascript必须包含在表单标记下。它运行良好!谢谢大家的投入!!

#7


0  

We can simplify that,

我们可以简化一下,

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

#1


6  

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.

也可以从代码隐藏中将数据发送到JavaScript。

#2


2  

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.

这似乎对这个论坛帖子中的某个人有用。

#3


1  

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

您可以为gridview设置RowStyle.CssClass属性,并使用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;

#4


1  

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

#5


0  

try this:

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

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

或者参见:如何使用jQuery计算asp.net中gridview中的行数

#6


0  

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

找到原因:因为网格包含在内容页面中,所以javascript必须包含在表单标记下。它运行良好!谢谢大家的投入!!

#7


0  

We can simplify that,

我们可以简化一下,

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