无法将类型'string'隐式转换为'System.Web.UI.WebControls.TextBox'

时间:2015-08-10 13:56:34

标签: c# asp.net

    int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());


TextBox textname=          ((System.Web.UI.WebControls.TextBox(GridView1.Rows[e.RowIndex].Cells[1].Controls[1])).Text;

            TextBox textdob = (((System.Web.UI.WebControls.TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[1]))).Text;

            TextBox textrole = ((System.Web.UI.WebControls.ListControl)(((System.Web.UI.WebControls.DropDownList)(((System.Web.UI.Control)(GridView1.Rows[e.RowIndex].Cells[3].Controls[1])))))).SelectedIndex.ToString();

获取该错误TextBox textname                    文本框textdob                    文本框文本

2 个答案:

答案 0 :(得分:0)

textname.Text = ((System.Web.UI.WebControls.ListControl)(((System.Web.UI.WebControls.DropDownList)(((System.Web.UI.Control)(GridView1.Rows[e.RowIndex].Cells[3].Controls[1])))))).SelectedIndex.ToString();

你不能在文本框本身添加一个字符串

答案 1 :(得分:0)

查看以下代码行

TextBox textname = ((System.Web.UI.WebControls.TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[1])).Text;

((System.Web.UI.WebControls.TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[1])).Text是一个字符串,但您要将其分配给textname TextBox,这就是您收到错误的原因。

将上述代码更改为

TextBox textname = (System.Web.UI.WebControls.TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[1]);

然后你可以这样得到文本

string name = txtname.Text;

同样的事情适用于第二个文本框

TextBox textdob = (System.Web.UI.WebControls.TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[1]);

// get the dob
string dbo = textdob.Text;

对于下拉列表,您必须将其分配给DropDownList而不是TextBox

DropDownList ddlRole = (System.Web.UI.WebControls.DropDownList)(GridView1.Rows[e.RowIndex].Cells[3].Controls[1]);

// get the selected index
int selectedRoleIndex = ddlRole.SelectedIndex;