从gridview更新时如何获取复选框

时间:2011-04-01 05:23:06

标签: c# asp.net

在我的项目中,我使用复选框列表...但是当我从gridview获取更新时,我无法获得复选框...任何人都可以帮助我

 SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ims"].ConnectionString);
 Conn.Open();
 DataSet ds1 = new DataSet();
 string query = "Select po_tax from purchase_order where po_id='" + Request.QueryString["po_id"] + "'";

 // ds1 = obj1.SelectQuery(query);
 SqlCommand cmd = new SqlCommand(query, Conn);
 cmd.ExecuteNonQuery();

 SqlDataReader rdr;

 rdr = cmd.ExecuteReader();
 if (rdr.Read() == false)
 {
      //No Records
      rdr.Close();
      Conn.Close();
      Label3.Text = "No record found";
      return;
 }
 else
 {
      CheckBoxList chkbx = (CheckBoxList)form1.FindControl("CheckBoxList1");
      rdr.NextResult();
      if (rdr.IsClosed == false)
      {
           while (rdr.Read() == true) [problem ocures here. it does not executes i.e does not go inside curly braces. why?]
           {
                ListItem currentCheckBox = chkbx.Items.FindByValue(rdr["po_id"].ToString());
                if (currentCheckBox != null)
                {
                     currentCheckBox.Selected = true;
                }
           }
      }
      rdr.Close();
      //string[] items = returned_value_from_db.Split(',');
      string[] items = sb.ToString().Split(',');
      for (int i = 0; i <= items.GetUpperBound(0); i++)
      {
           ListItem currentCheckBox = chkbx.Items.FindByValue(items[i].ToString());
           if (currentCheckBox != null)
           {
                currentCheckBox.Selected = true;
           }
      }
 }

 Conn.Close();

1 个答案:

答案 0 :(得分:1)

使用以下代码..希望它有效

SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ims"].ConnectionString);
 Conn.Open();
 DataSet ds1 = new DataSet();
 string query = "Select po_tax from purchase_order where po_id='" + Request.QueryString["po_id"] + "'";

 // ds1 = obj1.SelectQuery(query);
 SqlCommand cmd = new SqlCommand(query, Conn);
 cmd.ExecuteNonQuery();

 SqlDataReader rdr;

 rdr = cmd.ExecuteReader();
 if (!rdr.Read())
 {
      //No Records
      rdr.Close();
      Conn.Close();
      Label3.Text = "No record found";
      return;
 }
 else
 {
      CheckBoxList chkbx = (CheckBoxList)form1.FindControl("CheckBoxList1");
      rdr.NextResult();
      if (rdr.IsClosed == false)
      {
          do    //Dont place 'while' here coz u already used rdr.Read() before.
           {
                ListItem currentCheckBox = chkbx.Items.FindByValue(rdr["po_id"].ToString());
                if (currentCheckBox != null)
                {
                     currentCheckBox.Selected = true;
                }
           } while (rdr.Read())
      }
      rdr.Close();
      //string[] items = returned_value_from_db.Split(',');
      string[] items = sb.ToString().Split(',');
      for (int i = 0; i <= items.GetUpperBound(0); i++)
      {
           ListItem currentCheckBox = chkbx.Items.FindByValue(items[i].ToString());
           if (currentCheckBox != null)
           {
                currentCheckBox.Selected = true;
           }
      }
 }

 Conn.Close();