注册 登录
编程论坛 ASP.NET技术论坛

两天一个问题没搞定,求救

hnchina 发布于 2007-01-31 18:17, 814 次点击

报错:

无法将类型为“System.Web.UI.LiteralControl”的对象强制转换为类型“System.Web.UI.WebControls.TextBox”

我在网上看到可以用这个 findcontrol,但是我不知道用




html:



<asp:TemplateField HeaderText="状态">
<ItemTemplate>
<%# Eval("status")%>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField ID="HDFXueli" runat="server" Value='<%# Eval("status") %>' />
<asp:DropDownList ID="DDLXueli" runat="server" Width="90px" />
</EditItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>




aspx.cs是这样的写的.




protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{


String strConnection = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection myConnection = new SqlConnection(strConnection);

SqlCommand cm = new SqlCommand("updatelogin", myConnection);
cm.CommandType = CommandType.StoredProcedure;
cm.Connection.Open();

int id;
string username, userkind,wentilist,wenti,prio,status;
id = int.Parse(GridView1.Rows[e.RowIndex].
Cells[0].Text);
username = ((TextBox)GridView1.Rows[e.RowIndex].
Cells[1].Controls[0]).Text;
userkind = ((TextBox)GridView1.Rows[e.RowIndex].
Cells[2].Controls[0]).Text;
wenti= ((TextBox)GridView1.Rows[e.RowIndex].
Cells[3].Controls[0]).Text;
wentilist= ((TextBox)GridView1.Rows[e.RowIndex].
Cells[4].Controls[0]).Text;

status= ((TextBox)GridView1.Rows[e.RowIndex].
Cells[5].Controls[0]).Text;

cm.Parameters.Add(new SqlParameter("@id", id));
cm.Parameters.Add(new SqlParameter("@username", username));
cm.Parameters.Add(new SqlParameter("@userkind", userkind));
cm.Parameters.Add(new SqlParameter("@wenti", wenti));
cm.Parameters.Add(new SqlParameter("@wentilist", wentilist));
cm.Parameters.Add(new SqlParameter("@status", status ));
cm.ExecuteNonQuery();


GridView1.EditIndex = -1;
Bindgridview();

}

8 回复
#2
冰镇柠檬汁儿2007-01-31 19:35
把Controls[0]改成Controls[1]试试
#3
mgh19862007-01-31 19:38

我知道你说的什么意思,但我不会搞,我是新手,刚学ASP。NET
只知道(int)(int32)(int64) .ToString(); 是类型强制转换,你说的我没用过那
不过你的代码中有我需要的,谢谢拉!

#4
myruo2007-02-01 09:13

要实现什么目的

#5
myruo2007-02-01 09:27
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox mytext=(TextBox) GridView1.Rows[e.RowIndex].FindControl("你要找的文本控件名");

}
#6
陈天2007-02-06 16:19

好多人都直接把SQLScript直接放在Code-Behind中去?寒!!!

#7
陈天2007-04-06 11:37
as TextBox
#8
cyyu_ryh2007-04-06 12:38
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox ck = (CheckBox)gr.Cells[6].FindControl("你要找的文本控件ID");
//执行操作。

}
#9
tel19822007-04-07 11:56
2005有点不熟悉,不好意思。
1