2011-04-01 65 views
1

在我的項目更新我使用複選框列表...但同時從GridView的米獲得更新我不能讓檢查複選框...誰能幫我如何獲得選中的複選框,同時從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(); 
+2

可能會更好,如果你發佈你使用的代碼。謝謝。 – abramlimpin 2011-04-01 05:29:32

+0

你可以試着澄清一下嗎? 「我無法得到chekbox」是什麼意思? – Zannjaminderson 2011-04-01 05:31:07

+0

在項目中,我使用chekboxlist在我的形式... BT當我從gridvie的pricouse插入值不能得到.......更新wtevver我在赤框列表赤男子不能得到作爲chekeked同時更新.. ..所以我的問題是我不能讓cheked chekboxlist而更新 – r12 2011-04-01 05:42:26

回答

1

使用下面code..hope它的工作原理

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(); 
+0

標記爲答案,如果是有益的... – Marshal 2011-04-01 06:45:42

+0

此代碼不能正常工作 – r12 2011-04-01 08:21:44

+0

是否是不言而喻的,如果(rdr.IsClosed == FALSE)語句中? – Marshal 2011-04-01 10:54:48

相關問題