+ -
当前位置:首页 → 问答吧 → gridview的boundfield怎么获取字段

gridview的boundfield怎么获取字段

时间:2011-07-17

来源:互联网

无法将类型为“System.Web.UI.LiteralControl”的对象强制转换为类型“System.Web.UI.WebControls.TextBox”。 
这种问题怎么解决啊?我是在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();
  }

作者: godhelpmea   发布时间: 2011-07-17

你用TemplateField不要用BoundField,然后再在TemplateField里添加TextBox

作者: beyond_me21   发布时间: 2011-07-17

你用TemplateField不要用BoundField,然后再在TemplateField里添加TextBox 

作者: sam365143159   发布时间: 2011-07-17