Javascript 如何在javascript中计算gridview中的行数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7191633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to count number of rows in gridview in javascript?
提问by xorpower
As the question says, i wish to count the number of rows in gridview via JS. I am doing the way it is done herebut 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?
更新:忘记提及:我的网格视图在更新面板内。那会有什么不同吗?什么是正确的说法?
采纳答案by xorpower
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!!
找到原因:因为grid包含在content page中,javascript必须包含在form标签下。它运行良好!感谢大家的投入!!
回答by R?zvan Flavius Panda
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。
回答by Dot NET
回答by user2335127
var GridId = "<%=Questionsedit.ClientID %>";
var grid = document.getElementById(GridId);
rowscount = grid.rows.length;
回答by brheal
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;
回答by Shree
try this:
尝试这个:
var rowscount = $("#<%=GridView1.ClientID %> tr").length;
or see:
How to count the rows in a gridview in asp.net using jQuery
回答by kasim
We can simplify that,
我们可以简化一下,
var gridViewRowCount = document.getElementById("<%= GridView1.ClientID %>").rows.length;
alert(gridViewRowCount);