获取属性'行时出错?在gridview中

时间:2016-03-15 09:04:53

标签: javascript asp.net gridview

我在评论中显示的代码中出现错误(请仔细阅读)

<script type="text/javascript" src="http://code.jquery.com/jquery- 2.2.1.min.js"></script>
<script type="text/javascript">
    var grid = document.getElementById("<%=gvCustomers.ClientID%>");//gvCustomers is the ID of a gridview
       val();     
    function val() {
        if (grid.rows.length > 0) {//Unable to get property 'rows' of null or 'undefined' value
            alert(grid.rows.length);
        }
    }
</script>

我哪里错了?即使我没有空的网格视图!

HTML代码也在这里:

  <form id="form1" runat="server">
    <div>
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" ShowHeaderWhenEmpty ="true"
    DataKeyNames="CustomerID" OnRowDataBound="OnRowDataBound">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID ="btn" Text ="+" runat="server" />
                <asp:Panel ID="pnlOrders" runat="server" Style="display: none">
                    <asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass = "ChildGrid">
                        <Columns>
                            <asp:BoundField ItemStyle-Width="150px" DataField="OrderId" HeaderText="Order Id" />
                            <asp:BoundField ItemStyle-Width="150px" DataField="OrderDate" HeaderText="Date" />
                        </Columns>
                    </asp:GridView>
                </asp:Panel>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField ItemStyle-Width="150px" DataField="ContactName" HeaderText="Contact Name" />
        <asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
    </Columns>
</asp:GridView>
        </div>
</form>

1 个答案:

答案 0 :(得分:0)

您没有选择正确的gridview。

试试这个

var grid = document.getElementById("gvCustomers");

<强>更新

使用Jquery,试试这个:

$(document).ready(function(){
var grid = document.getElementById("gvCustomers");
    function val() {
        if (grid.rows.length > 0) {//Unable to get property 'rows' of null or 'undefined' value
            alert(grid.rows.length);
        }
    }
  val();
});