当前位置:编程学习 > C#/ASP.NET >>

我下面一句代码 总是报错。对象无实例化

   protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            GridViewRow row = GridView1.Rows[e.RowIndex];
       
            string Area = ((TextBox)row.FindControl("TextBox1")).Text.ToString(); 这句报错
            string family = ((TextBox)row.FindControl("TextBox2")).Text.ToString();
        } --------------------编程问答--------------------  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                                                        DataKeyNames="sid" onrowcommand="GridView1_RowCommand" 
                                                        onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
                                                        <Columns>
                                                            <asp:BoundField DataField="StreetName" HeaderText="街道名称" ReadOnly="True" />
                                                            <asp:TemplateField HeaderText="总面积">
                                                                <EditItemTemplate>
                                                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Area") %>'></asp:TextBox>
                                                                </EditItemTemplate>
                                                                <ItemTemplate>
                                                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Area") %>'></asp:Label>
                                                                </ItemTemplate>
                                                            </asp:TemplateField>
                                                            <asp:TemplateField HeaderText="总户数">
                                                                <EditItemTemplate>
                                                                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("family") %>'></asp:TextBox>
                                                                </EditItemTemplate>
                                                                <ItemTemplate>
                                                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("family") %>'></asp:Label>
                                                                </ItemTemplate>
                                                            </asp:TemplateField>
                                                            <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                                                        </Columns>
                                                    </asp:GridView> --------------------编程问答-------------------- 上面这个是HTML 代码! --------------------编程问答-------------------- 我点击更新就报错~! --------------------编程问答-------------------- 报的什么错?? --------------------编程问答-------------------- --------------------编程问答-------------------- 索引超出范围。必须为非负值并小于集合大小。
参数名: index 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
参数名: index

源错误: 


行 391:            GridViewRow row = GridView1.Rows[e.RowIndex];
行 392:       
行 393:            string Area = ((TextBox)row.FindControl("TextBox1")).Text.ToString();
行 394:            string family = ((TextBox)row.FindControl("TextBox2")).Text.ToString();
行 395:            manage.StreetMappingUpdate(id, Area, family);
 
--------------------编程问答--------------------
引用 5 楼 net_lover 的回复:
试试
((TextBox)GridView1.Rows[GridView1.EditIndex].FindControl("TextBox1")).Text

  试试这个 --------------------编程问答--------------------
引用 5 楼 net_lover 的回复:
试试
((TextBox)GridView1.Rows[GridView1.EditIndex].FindControl("TextBox1")).Text


还是报那个错 --------------------编程问答-------------------- 你调试看下你e.RowIndex的值呢?有么得?
还有GridView1.Rows.Count是多少 --------------------编程问答-------------------- GridView 插入、删除、修改、分页的综合例子
参考完整的例子
http://dotnet.aspx.cc/file/Add-Delete-Update-Edit-Data-With-Paging-in-GridView.aspx

你的代码只有片段,无法更进一步查看 --------------------编程问答--------------------
引用 8 楼 zhang86817 的回复:
引用 5 楼 net_lover 的回复:
试试
((TextBox)GridView1.Rows[GridView1.EditIndex].FindControl("TextBox1")).Text


还是报那个错


添加列项,定位到单元格
------------------------
.Cells[index].FindControl --------------------编程问答-------------------- 你有写GridView的编辑事件吗?没有的话添加下面的代码:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
        }
让GridView处在编辑的状态,因为你的TextBox是在编辑模板中 --------------------编程问答-------------------- e.Item.FindControl("TextBox1") --------------------编程问答-------------------- GridViewRow row = GridView1.Rows[e.RowIndex];
应该是这句没有取到对象 --------------------编程问答-------------------- string Area = ((TextBox)row.FindControl("TextBox1")).Text.ToString();
string Area = ((TextBox)row.cells[第几个单元格].FindControl("TextBox1")).Text.ToString();
试一试 --------------------编程问答-------------------- 你这报错明摆着就是控件没找到。
建议调试一下看看row对不对。
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,