帮我看下gridview中更新数据时的错误

来源:百度知道 编辑:UC知道 时间:2024/05/25 06:47:17
string UID = User_List.DataKeys[e.RowIndex].Value.ToString();
string name = ((TextBox)(User_List.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
string pwd = ((TextBox)(User_List.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
string pwr = ((TextBox)(User_List.Rows[e.RowIndex].Cells[3].Controls[0])).Text;
编译时出现"无法将类型为“System.Web.UI.WebControls.DataControlLinkButton”的对象强制转换为类型“System.Web.UI.WebControls.TextBox”"。请问各位这是什么原因呢?急!谢谢!

建议用TableCell 类的 FindControl(string) 方法来获取控件,如

Control ctl = User_List.Rows[e.RowIndex].Cells[1].FindControl("txtName");

if(ctl != null)
{
TextBox txtUserName = ctl as TextBox;
string name = txtUserName.Text;
}

如果不确定对象是否为空,一定要进行检查。

你有一项转换的时候类型不对,应该是你的name、pwd和pwr之中的一个

他们在gridview里面是linkbutton型,但是你将它们强转成textbox,出错了

你看一下你的GridView里面的第二列、第三列、第四列里面的控件都是什么?是不是有LinkButton控件。