首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

javascript获取gridview值失败!解决方法

2013-04-20 
javascript获取gridview值失败!我要用javascript获取girdview内的值,报错:0x800a138f - JavaScript 运行时

javascript获取gridview值失败!
我要用javascript获取girdview内的值,报错:0x800a138f - JavaScript 运行时错误: 无法获取未定义或 null 引用的属性“rows”
代码如下:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ChooseCustomerPage.aspx.cs" Inherits="ChooseCustomerPage" %>
<%@ Register Assembly="MyControls" Namespace="MyControls" TagPrefix="cc1" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script type="text/javascript">
        function save() {
            var table = document.getElementById(<%=GridView1.ClientID %>);
            var rowindex = 0;
            for(var i=1;i<table.rows.length;i++) 
            { 
                var input = table.rows[i].cells[0].getElementsByTagName("input")[0].checked; 
                if (input == true) 
                { 
                    rowIndex = i; 
                    return rowindex;
                } 
            }             
        }
        function closeShow() {
            window.close();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="选择客户:"></asp:Label>
        <cc1:MyGridView ID="GridView1" runat="server" AllowPaging="True" 
            AutoGenerateColumns="False" CellPadding="4" 
            ForeColor="#333333" 
            OnPageIndexChanging="GridView1_PageIndexChanging" 
            PageSize="8" 
            hoverBackColor=""
            hoverTextColor="">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:TemplateField HeaderText="选择">


                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <input onclick="select" id="Checkbox1" type="checkbox" />
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" Width="50px" />
                </asp:TemplateField>
                <asp:BoundField DataField="customerID" HeaderText="ID">
                <ItemStyle Width="50px" />
                </asp:BoundField>
                <asp:BoundField DataField="name" HeaderText="客户名" >
                <ItemStyle Width="300px" />
                </asp:BoundField>
                <asp:BoundField DataField="phone" HeaderText="电话" >
                <HeaderStyle Width="100px" />
                </asp:BoundField>
                <asp:BoundField DataField="address" HeaderText="详细地址" >
                <HeaderStyle Width="150px" />
                <ItemStyle Width="150px" />
                </asp:BoundField>
                <asp:BoundField DataField="EstablishName" HeaderText="创建人" >
                <ItemStyle Width="80px" />
                </asp:BoundField>
                <asp:BoundField DataField="email" HeaderText="邮箱" >
                <HeaderStyle Width="150px" />


                <ItemStyle Width="200px" />
                </asp:BoundField>
                <asp:BoundField DataField="AsignName" HeaderText="负责人" >
                <ItemStyle Width="80px" />
                </asp:BoundField>
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        </cc1:MyGridView>
    </form>
    <p>
        <input id="btnSave" type="button" value="保存退出" onclick="save()" /><input id="btnCancel" 
            type="button" value="退出" onclick="closeShow()" /></p>
</body>
</html>


这是说明无法获取table的行吗? 但是很多资料上的都是这样的,我用的IE10
[解决办法]
var?table?=?document.getElementById(<%=GridView1.ClientID?%>);
在客户端浏览器运行后的结果是
var?table?=?document.getElementById(GridView1);
少引号了

改成
var?table?=?document.getElementById('<%=GridView1.ClientID?%>');
在客户端浏览器运行后的结果是
var?table?=?document.getElementById('GridView1');
alert("table.id:"table.id); //应该可以看到 table.id:GridView1 就对了
alert(table.rows) //这里alert 出来 [object] 就对了

热点排行