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没有用数据库我只想改页面中的数据这个问题是什么原因呢?
{
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没有用数据库我只想改页面中的数据这个问题是什么原因呢?
Labnum_web1_cs是否存在,调试看看是否为null
应该这样写
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");
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;
你这句话就有问题了。
lab_web1_cs 已经是string了。怎么还呢Text呢?
{
lab_web1_cs.Text = txt_web1_cs;
}
如果为NULL,那么你GridView里有没有取到这个Label呢??
你把你的前台GridView的代码也贴点出来吧。。
<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>
<asp:LinkButton ID="Lbtndel_web1_cs" runat="server" CausesValidation="False"
CommandName="Delete" OnClientClick="return confirm('真的要删除该记录吗?')" Text="删除"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
那只能用Updated事件吗?
有ShowEditButton啊! <asp:CommandField ShowEditButton="True" />
在你的 <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];
<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
感觉自己代码没什么问题啊
你的数量这一列在显示的时候用的Label,编辑的时候用的TextBox。。
这样的话你在Updating的时候,执行修改SQL语句Update,那么自然它Label显示出来后就成了修改后的数据。。而你的代码:
在非编辑状态,是用Label显示的,这时取得的即一个Label控件,不能取得TextBox控件。
而在编辑状态下,变成了TextBox,这时取得TextBox控件,不能取得Label控件。。
所以会导致未应用实例的错误。。
我怎么才能实现更新呢? 定义一个中间变量先保存下来吗?
Request.QueryString?
然后再传值吗?
修改之后update到数据库,然后再从新绑定一次就可以了!~~