我正在尝试使用javascript获取网格视图的行数。 Javascript被调用,但有些我怎么没有得到行数。这是我的javascript的代码
<script language="javascript" type="text/javascript">
function CheckSites() {
var Grid = document.getElementByID(<%=grdSiteInformation.ClientID%>);
var row = Grid.rows.length;
alert(grid);
if(length=0)
alert('Enter atleast one site');
}
</script>
这就是我调用该功能的方式......
<asp:Button ID="btnAdd" runat="server" Text="Save" OnClick="btnAdd_Click" OnClientClick="CheckSites()"/>
这是我的gridview
的定义<asp:GridView ID="grdSiteInformation" runat="server" AutoGenerateColumns="False"
OnRowCommand="grdSiteInformation_RowCommand">
<Columns>
<asp:BoundField DataField="TableRowIndex" HeaderText="Sr.No" />
<asp:BoundField DataField="SiteID" HeaderText="SiteID" />
<asp:BoundField DataField="POrderID" HeaderText="POrderID" />
<asp:BoundField DataField="SiteName" HeaderText="SiteName" />
<asp:BoundField DataField="Location" HeaderText="Location" />
<asp:BoundField DataField="SiteAddress" HeaderText="SiteAddress" />
<asp:BoundField DataField="Cluster" HeaderText="Cluster" />
<asp:BoundField DataField="SubVendorName" HeaderText="SubVendorName" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" CommandArgument='<%# Eval("SiteID") %>' CommandName="Edt"
Text="Edit" />
</ItemTemplate>
<HeaderTemplate>
Edit
</HeaderTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
请指导我在哪里/我犯了错误的错误......
由于
答案 0 :(得分:1)
尝试
var rowCount = document.getElementById('<%=grdSiteInformation.ClientID%>')
.getElementsByTagName("TR").length;
alert(rowCount);
修改强>
尝试jQuery
var totalRows = $("#<%=GridView1.ClientID %> tr").length;
答案 1 :(得分:1)
如果您使用的是Asp.Net的GridView而您只想要行计数,那么您可以将js函数更改为:
function CheckSites()
{
var row = <%=grdSiteInformation.Rows.Count %>
if(row=0)
alert('Enter at least one site');
}