Datalist中使用findcontrol找TEXTBOX,结果却说没赋值。。
在DataList中实现一个回复留言功能。replys是TEXTBOX的ID部分代码:
if (e.CommandName == "reply")
{
DataSet ds = new DataSet();
TextBox replys; foreach (DataListItem i in DataList2.Items)
{
if (i.ItemType == ListItemType.Item)
{
TextBox re = (TextBox)i.FindControl("replys");
break;
} }
string strhf = replys.Text ;
string strcon = "Data Source=HYLI\\TY;Initial Catalog=guestbook;Integrated Security=True";
SqlConnection sqlcon = new System.Data.SqlClient.SqlConnection(strcon);
SqlCommand cmd = new SqlCommand(); cmd.Connection = sqlcon;
string bh = DataList2.DataKeys[e.Item.ItemIndex].ToString();//获取id
cmd.CommandText = "update ly set hf = '" + replys.Text + "' where bh =" + bh;
sqlcon.Open();// 打开连接
cmd.ExecuteNonQuery(); sqlcon.Close();
}
出错提示:CS0165: 使用了未赋值的局部变量“replys”
大虾们帮忙解决一下噢
在DataList中实现一个回复留言功能。replys是TEXTBOX的ID部分代码:
if (e.CommandName == "reply")
{
DataSet ds = new DataSet();
TextBox replys; foreach (DataListItem i in DataList2.Items)
{
if (i.ItemType == ListItemType.Item)
{
TextBox re = (TextBox)i.FindControl("replys");
break;
} }
string strhf = replys.Text ;
string strcon = "Data Source=HYLI\\TY;Initial Catalog=guestbook;Integrated Security=True";
SqlConnection sqlcon = new System.Data.SqlClient.SqlConnection(strcon);
SqlCommand cmd = new SqlCommand(); cmd.Connection = sqlcon;
string bh = DataList2.DataKeys[e.Item.ItemIndex].ToString();//获取id
cmd.CommandText = "update ly set hf = '" + replys.Text + "' where bh =" + bh;
sqlcon.Open();// 打开连接
cmd.ExecuteNonQuery(); sqlcon.Close();
}
出错提示:CS0165: 使用了未赋值的局部变量“replys”
大虾们帮忙解决一下噢
==>
replys = (TextBox)i.FindControl("replys");
==>
cmd.CommandText = "update ly set hf = '" + re.Text + "' where bh =" + bh;
{
DataSet ds = new DataSet();
string s= string.Empty ; foreach (DataListItem i in DataList2.Items)
{
if (i.ItemType == ListItemType.Item)
{
TextBox re = (TextBox)i.FindControl("replys");
s = re.Text;
break;
} }
string strhf = s;
string strcon = "Data Source=HYLI\\TY;Initial Catalog=guestbook;Integrated Security=True";
SqlConnection sqlcon = new System.Data.SqlClient.SqlConnection(strcon);
SqlCommand cmd = new SqlCommand(); cmd.Connection = sqlcon;
string bh = DataList2.DataKeys[e.Item.ItemIndex].ToString();//获取id
cmd.CommandText = "update ly set hf = '" + s + "' where bh =" + bh;
sqlcon.Open();// 打开连接
cmd.ExecuteNonQuery(); sqlcon.Close();
}
那么应该是 DataList2.Items[行].FindControl("id"):
{
if (i.ItemType == ListItemType.Item)
{
replys = (TextBox)i.FindControl("replys");
break;
} }
你改成 replys = (TextBox)i.FindControl("replys");试试
想请教一下,string.Empty的 用法?
------------
字符串初始化一下