protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Label lab_web1_cs=(Label)GridView1.Rows[e.RowIndex].FindControl("Labnum_web1_cs");
        string txt_web1_cs = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNum_web1_cs")).Text.ToString();
      
            lab_web1_cs.Text = txt_web1_cs;//在运行到这一步的时候提示未将对象引用设置到对象的实例
        
    }
我更新时GRIDVIEW没有用数据库我只想改页面中的数据这个问题是什么原因呢?

解决方案 »

  1.   

    如果代码没有问题,那肯定是没有获取到值,取值为null了
      

  2.   

    if(lab_web1_cs!=null)lab_web1_cs.Text = txt_web1_cs;
    Labnum_web1_cs是否存在,调试看看是否为null
      

  3.   

    哎,呵呵楼主你可是手误啊,
    应该这样写
    Label lab_web1_cs.Text=(Label)GridView1.Rows[e.RowIndex].FindControl("Labnum_web1_cs"); 你写的的是
    Label lab_web1_cs=(Label)GridView1.Rows[e.RowIndex].FindControl("Labnum_web1_cs"); 
      

  4.   

    查找页面,id="Labnum_web1_cs" 写对了没
      

  5.   

    string txt_web1_cs = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNum_web1_cs")).Text.ToString(); 此处应该没有获取到值。
      

  6.   

    调试一下看看那个变量,或者那个操作引起了null
      

  7.   

      string txt_web1_cs = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNum_web1_cs")).Text.ToString();
            if (((Label)GridView1.Rows[e.RowIndex].FindControl("Labnum_web1_cs")).Text != null)
                ((Label)GridView1.Rows[e.RowIndex].FindControl("Labnum_web1_cs")).Text = txt_web1_cs;
      

  8.   

    ((Label)GridView1.Rows[e.RowIndex].FindControl("Labnum_web1_cs"))这个对象没有实例
      

  9.   

     lab_web1_cs.Text = txt_web1_cs;//
    你这句话就有问题了。
    lab_web1_cs  已经是string了。怎么还呢Text呢?
      

  10.   

    if(lab_web1_cs!=null)
    {
       lab_web1_cs.Text = txt_web1_cs;
    }
      

  11.   

    事件弄错了吧应该用GridView1_RowUpdated事件
      

  12.   

    Labnum_web1_cs是否存在,调试看看是否为null
    如果为NULL,那么你GridView里有没有取到这个Label呢??
    你把你的前台GridView的代码也贴点出来吧。。
      

  13.   

                 <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" DataKeyNames="ProductID" OnDataBound="GridView1_DataBound" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="696px" OnRowUpdating="GridView1_RowUpdating">
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:TemplateField HeaderText="产品ID">
                    <ItemTemplate>
                        <asp:Label ID="LabID_web1_cs" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="产品名称">
                    <ItemTemplate>
                        <asp:Label ID="LabName_web1_cs" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="产品代号">
                    <ItemTemplate>
                        <asp:Label ID="LabCNum_web1_cs" runat="server" Text='<%# Eval("ProductNum") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="产品价格">
                    <ItemTemplate>
                        <asp:Label ID="LabPrice_web1_cs" runat="server" Text='<%# Eval("ProductPrice") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="产品数量">
                    <ItemTemplate>
                        <asp:Label ID="Labnum_web1_cs" runat="server" Text='<%# Eval("ProductCardNumber") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtNum_web1_cs" runat="server" Width="92px" Text='<%# Eval("ProductCardNumber") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="产品金额">
                    <ItemTemplate>
                        <asp:Label ID="LabTotal_web1_cs" runat="server" Text='<%# Eval("Price") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="True" />
                <asp:TemplateField>
                    <ItemTemplate>
                        &nbsp;<asp:LinkButton ID="Lbtndel_web1_cs" runat="server" CausesValidation="False"
                            CommandName="Delete" OnClientClick="return confirm('真的要删除该记录吗?')" Text="删除"></asp:LinkButton>
                    </ItemTemplate>
                  
                </asp:TemplateField>
            </Columns>
            
        </asp:GridView>
      

  14.   

    rowupdating时好像不能取itemtemplate里的控件吧
      

  15.   

    咦,,你这里好像没有ShowEditButton呀,怎么激发了事件RowUpdating??
      

  16.   

    rowupdating时好像不能取itemtemplate里的控件?不能取吗?
     那只能用Updated事件吗?
    有ShowEditButton啊!     <asp:CommandField ShowEditButton="True" /> 
      

  17.   

    如ls所说,在rowupdating时好像不能取itemtemplate里的控件,我想可以这样解决,你试试:
    在你的 <asp:TemplateField HeaderText="产品ID"> 
                    <ItemTemplate> 
                        <asp:Label ID="LabID_web1_cs" runat="server" Text=' <%# Eval("ProductID") %>'> </asp:Label> 
                    </ItemTemplate> 
                </asp:TemplateField> 加上EditItemTemplate,即<asp:TemplateField HeaderText="产品ID"> 
                    <ItemTemplate> 
                        <asp:Label ID="LabID_web1_cs" runat="server" Text=' <%# Eval("ProductID") %>'> </asp:Label> 
                    </ItemTemplate> 
    <EditItemTemplate> 
                        <asp:Label ID="lab_web2_cs" runat="server" Width="92px" Text=' <%# Eval("ProductID") %>'> </asp:Label> 
                    </EditItemTemplate> 
                </asp:TemplateField> 后台可以这样取:Label lab_web1_cs=(Label)GridView1.Rows[e.RowIndex].Cells[0].Controls[1];
      

  18.   

    <asp:TemplateField HeaderText="产品数量"> 
                    <ItemTemplate> 
                        <asp:Label ID="Labnum_web1_cs" runat="server" Text=' <%# Eval("ProductCardNumber") %>'> </asp:Label> 
                    </ItemTemplate> 
                    <EditItemTemplate> 
                        <asp:TextBox ID="txtNum_web1_cs" runat="server" Width="92px" Text=' <%# Eval("ProductCardNumber") %>'> </asp:TextBox> 
                    </EditItemTemplate> 
                </asp:TemplateField> 
    楼上的很感谢你这么用心帮我哦
    我本来定义了一个LABLE来显示数量,
    然后我启用了编辑模式,加了一个TEXTBOX进去
    然后我想把修改后的TEXTBOX放入到LABLE中就这么简单
    可是搞了半天啊T.T  
    感觉自己代码没什么问题啊
      

  19.   

    哦,,我现在明白你的意思了
    你的数量这一列在显示的时候用的Label,编辑的时候用的TextBox。。
    这样的话你在Updating的时候,执行修改SQL语句Update,那么自然它Label显示出来后就成了修改后的数据。。而你的代码:
    在非编辑状态,是用Label显示的,这时取得的即一个Label控件,不能取得TextBox控件。
    而在编辑状态下,变成了TextBox,这时取得TextBox控件,不能取得Label控件。。
    所以会导致未应用实例的错误。。
      

  20.   

    那这样的话
     我怎么才能实现更新呢?  定义一个中间变量先保存下来吗?
    Request.QueryString?
    然后再传值吗?
      

  21.   

    你不是要修改吗??
    修改之后update到数据库,然后再从新绑定一次就可以了!~~