gridview的boundfield怎么获取字段
时间:2011-07-17
来源:互联网
无法将类型为“System.Web.UI.LiteralControl”的对象强制转换为类型“System.Web.UI.WebControls.TextBox”。
这种问题怎么解决啊?我是在gridview里用boundfield直接绑定数据源了,所以没有id名的。而且开头也有写!=ispostback 就重新绑定数据了。但是问题还是存在啊。
这种问题怎么解决啊?我是在gridview里用boundfield直接绑定数据源了,所以没有id名的。而且开头也有写!=ispostback 就重新绑定数据了。但是问题还是存在啊。
作者: godhelpmea 发布时间: 2011-07-17
前台代码: <asp:GridView ID="gv" runat="server" AllowPaging="True"
AutoGenerateColumns="False" onpageindexchanging="GridView1_PageIndexChanging"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdatabound="GridView1_RowDataBound" onrowdeleting="GridView1_RowDeleting"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
PageSize="8">
<Columns>
<asp:BoundField DataField="name" HeaderText="姓名" />
<asp:BoundField DataField="psw" HeaderText="密码" />
<asp:TemplateField ItemStyle-Width="80px" HeaderText="密码强度">
<ItemTemplate>
<asp:Label ID="pswinfo" runat="server" ></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="sex" HeaderText="性别" />
<asp:BoundField DataField="pro" HeaderText="省份" />
<asp:BoundField DataField="love" HeaderText="爱好" />
<asp:BoundField DataField="selfinfo" HeaderText="个人说明">
<ItemStyle Width="120px" />
</asp:BoundField>
<asp:CommandField ShowDeleteButton="True"/>
<asp:CommandField ShowEditButton="True" />
</Columns>
<EmptyDataTemplate>暂无数据</EmptyDataTemplate>
</asp:GridView>
后台编辑代码: protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int cid = Convert.ToInt32(gv.DataKeys[e.RowIndex].Value);
GridViewRow row = gv.Rows[e.RowIndex];
string str = ((TextBox)row.Cells[0].Controls[0]).Text;
string stt = ((TextBox)row.Cells[1].Controls[0]).Text;
string std = ((TextBox)row.Cells[2].Controls[0]).Text;
string sth = ((TextBox)row.Cells[3].Controls[0]).Text;
string sto = ((TextBox)row.Cells[4].Controls[0]).Text;
string sti = ((TextBox)row.Cells[5].Controls[0]).Text;
SqlConnection con = new SqlConnection(conn);
string sql = "update cto set name=str,psw=stt,sex=std,pro=sth,love=sto,selfinfo=sti where id=@id";
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@id", cid);
cmd.ExecuteNonQuery();
gv.EditIndex = -1;
band();
con.Close();
}
AutoGenerateColumns="False" onpageindexchanging="GridView1_PageIndexChanging"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdatabound="GridView1_RowDataBound" onrowdeleting="GridView1_RowDeleting"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
PageSize="8">
<Columns>
<asp:BoundField DataField="name" HeaderText="姓名" />
<asp:BoundField DataField="psw" HeaderText="密码" />
<asp:TemplateField ItemStyle-Width="80px" HeaderText="密码强度">
<ItemTemplate>
<asp:Label ID="pswinfo" runat="server" ></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="sex" HeaderText="性别" />
<asp:BoundField DataField="pro" HeaderText="省份" />
<asp:BoundField DataField="love" HeaderText="爱好" />
<asp:BoundField DataField="selfinfo" HeaderText="个人说明">
<ItemStyle Width="120px" />
</asp:BoundField>
<asp:CommandField ShowDeleteButton="True"/>
<asp:CommandField ShowEditButton="True" />
</Columns>
<EmptyDataTemplate>暂无数据</EmptyDataTemplate>
</asp:GridView>
后台编辑代码: protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int cid = Convert.ToInt32(gv.DataKeys[e.RowIndex].Value);
GridViewRow row = gv.Rows[e.RowIndex];
string str = ((TextBox)row.Cells[0].Controls[0]).Text;
string stt = ((TextBox)row.Cells[1].Controls[0]).Text;
string std = ((TextBox)row.Cells[2].Controls[0]).Text;
string sth = ((TextBox)row.Cells[3].Controls[0]).Text;
string sto = ((TextBox)row.Cells[4].Controls[0]).Text;
string sti = ((TextBox)row.Cells[5].Controls[0]).Text;
SqlConnection con = new SqlConnection(conn);
string sql = "update cto set name=str,psw=stt,sex=std,pro=sth,love=sto,selfinfo=sti where id=@id";
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@id", cid);
cmd.ExecuteNonQuery();
gv.EditIndex = -1;
band();
con.Close();
}
作者: godhelpmea 发布时间: 2011-07-17
你用TemplateField不要用BoundField,然后再在TemplateField里添加TextBox
作者: beyond_me21 发布时间: 2011-07-17
你用TemplateField不要用BoundField,然后再在TemplateField里添加TextBox
作者: sam365143159 发布时间: 2011-07-17
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28