您的位置:首页 > 其它

gridview取编辑时模板列表格数据:出现未将对象引用设置到对象的实例

2009-08-09 08:18 507 查看
原因:该列是模版列

要想取其值

需要这样,找到其对应列,用FindControl()方法将其找到,并转化成相应类型即可

例:

string riqi =((TextBox ) this.GridView1.Rows[e.RowIndex].Cells[4].FindControl("TextBox1")).Text.Trim();

或者

string riqi=this.GridView1.Rows[e.RowIndex].Cells[4].Text;这样取也可以

比数据库中有个日期类型的字段,通过GRIDVIEW来编辑,并且要用到AJAX的CalendarExtender日历扩展控件来实现更新

1.需要将日期类型字段转化成模板,并加上CalendarExtender日历扩展控件(红色)

<asp:TemplateField HeaderText="日期">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("anzhuangriqi") %>'></asp:TextBox>
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID ="TextBox1" PopupButtonID ="TextBox1">
</cc1:CalendarExtender>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("anzhuangriqi") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

2.对刚才的日期字段模板列取值

string riqi =((TextBox ) this.GridView1.Rows[e.RowIndex].Cells[4].FindControl("TextBox1")).Text.Trim();

用FindControl()方法将其找到,并转化成相应类型即可,否则取不到值

出现未将对象引用设置到对象的实例 就是这个原因引起的

3.更新时,用鼠标点日历控件,直接更新即可

4.先退出编辑状态,再绑定数据
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐