2014-04-07 59 views
0

我從sql查詢生成一個CheckBoxList,並且所有工作正常,即CheckBoxList是依賴的,因此,當我檢查一個唯一的選項時,它會生成另一個CheckBoxList,它也可以正常工作。我想檢查多個選項並獲取所有數據,但查詢僅在最後一個chekbox檢查時運行。檢查依賴複選框列表中的倍數選項C#

如何實現接受CheckBoxList中選中的所有選項並顯示所有數據的查詢?

這裏我的代碼:

protected void cblList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    foreach (ListItem list1 in cblList1.Items) 
    { 
     if (list1 .Selected == true) 
     { 
      LoadCheckBoxListList2(list1); 
     } 
    } 
} 

private void LoadCheckBoxListList2(ListItem itemList1) 
{ 
    SqlCommand cmd = new SqlCommand("SELECT DISTINCT tbl_information.route AS ROUTE FROM tbl_information INNER JOIN tbl_regional ON tbl_information.region = tbl_regional.id_regional WHERE tbl_information.supervisor = " + "'" + itemList1 + "'", conn); 
    SqlDataAdapter da = new SqlDataAdapter(cmd); 
    DataSet ds = new DataSet(); 
    da.Fill(ds); 

    cblRutas.DataSource = ds; 
    cblRutas.DataValueField = "ROUTE"; 
    cblRutas.DataBind(); 
} 

回答

0

我得到了答案,這裏是代碼:

protected void cblList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    List<string> list1 = new List<string>(); 
    foreach (ListItem itemList1 in cblList1.Items) 
    { 
     if (itemList1 .Selected == true) 
     { 
      list1.Add(itemList1.Text) 
     } 
    } 
    LoadCheckBoxListList2(list1); 
} 

private void LoadCheckBoxListList2(List<string> list1) 
{ 
    DataTable dt = new DataTable("R"); 
    dt.Columns.Add("Route", typeof(string)); 

     foreach (string li in list1) 
     { 
      string query = @"SELECT DISTINCT tbl_information.route AS ROUTE FROM tbl_information INNER JOIN tbl_regional ON tbl_information.region = tbl_regional.id_regional WHERE tbl_information.supervisor = '" + li + "'"; 

      SqlCommand cmd = new SqlCommand(query, conn); 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      DataSet ds = new DataSet(); 
      da.Fill(ds); 

      bool b = false; 

      foreach (DataRow item in ds.Tables[0].Rows) 
      { 
       if (!b) 
       { 
        dt.Rows.Add(item[0] + " (" + li + ")"); 
        b = true; 
        continue; 
       } 

       dt.Rows.Add(item.ItemArray); 
      } 
     } 

     cblRutas.DataSource = dt; 
     cblRutas.DataValueField = "ROUTE"; 
     cblRutas.DataBind(); 
    } 

感謝您aswers!

0
 for (int i = 0; i < checkedListBox1.Items.Count; i++) 
     { 
      if (checkedListBox1.GetItemChecked(i)) 
      { 
       string str = (string)checkedListBox1.Items[i]; 
       //Do your function call here 
      } 
     } 
0

如果我正確地得到了你的問題,你SelectedIndexChanged事件處理程序,你必須收集所有的檢查項目列表(不僅是最後一個)和查詢沒有where [email protected]過濾器,但是where id in (@id1, @id2, etc)

所以,你只需要改變你的代碼一點點:

protected void cblList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    List<string> ids = new List<string>(); 
    foreach (ListItem list1 in cblList1.Items) 
    { 
     if (list1 .Selected == true) 
     { 
      ids.Add(list1.ToString());    
     } 
    } 

    LoadCheckBoxListList2(ids); 
} 

private void LoadCheckBoxListList2(List<string> ids) 
{ 
    string idsString = string.Join(", ", ids);// WARNING: carefull, SQL-injection is possible here, better to filter the input 
    SqlCommand cmd = new SqlCommand("SELECT DISTINCT tbl_information.route AS ROUTE FROM tbl_information INNER JOIN tbl_regional ON tbl_information.region = tbl_regional.id_regional WHERE tbl_information.supervisor IN (" + idsString + ")", conn); 

    SqlDataAdapter da = new SqlDataAdapter(cmd); 
    DataSet ds = new DataSet(); 
    da.Fill(ds); 

    cblRutas.DataSource = ds; 
    cblRutas.DataValueField = "ROUTE"; 
    cblRutas.DataBind(); 
} 
+0

謝謝aleksey.berezan。 查詢時出現錯誤,因爲當我運行該應用程序時,在「複選框的文本」附近出現語法錯誤。 – Gio11

+0

@ Gio11,你可以請你發佈你的查詢嗎? –

相關問題