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”
大虾们帮忙解决一下噢

解决方案 »

  1.   

    TextBox replys;TextBox re = (TextBox)i.FindControl("replys");
    ==>
    replys = (TextBox)i.FindControl("replys");
      

  2.   

    或者cmd.CommandText = "update ly set hf = '" + replys.Text + "' where bh =" + bh;
    ==>
    cmd.CommandText = "update ly set hf = '" + re.Text + "' where bh =" + bh;
      

  3.   

    try:if (e.CommandName == "reply")
            {
                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();
            }
      

  4.   

    思路 应该是找到指定的行的列的值吧?
    那么应该是  DataList2.Items[行].FindControl("id"):
      

  5.   

    TextBox replys = new TextBox();            foreach (DataListItem i in DataList2.Items)
                {
                    if (i.ItemType == ListItemType.Item)
                    {
                        replys  = (TextBox)i.FindControl("replys");
                        break;
                    }            }
      

  6.   

    显然未付值呃
    你改成 replys = (TextBox)i.FindControl("replys");试试
      

  7.   

    用haonanernet(与时俱进)的方法实现了谢谢
    想请教一下,string.Empty的 用法?
      

  8.   

    想请教一下,string.Empty的 用法?
    ------------
    字符串初始化一下