2012-03-20 34 views
0

Hy all ..我想問你怎麼可能當一個複選框被選中基於該複選框顯示錶中的數據。所以我有2個表:國家和城市。我顯示的CheckBoxList內的所有國家所以每個國家都有一個checkbox.Now我想,當我選中一個複選框,就全國城市檢查appear.This是我的代碼顯示國家:使用c#在asp.net中的複選框#

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["erp"].ConnectionString); 
    con.Open(); 
    string intero = "Select * from judete"; 
    SqlCommand cmd = new SqlCommand(intero, con); 

    SqlDataReader rdr; 

    rdr = cmd.ExecuteReader(); 

    while (rdr.Read()) 
    { 


     CheckBoxList check = new CheckBoxList(); 
     check.Visible = true; 

     check.Items.Add(new ListItem(rdr[1].ToString())); 
     Panel1.Controls.Add(check); 

     foreach (ListItem item in check.Items) 
     { 

      item.Text = rdr.GetString(1); 
     } 

    } 

我的問題是:我怎樣才能檢索基於檢查複選框的城市?在此先感謝並抱歉重複,但我還沒弄明白。

回答

2

你應該做的是:

  1. 讓你的CheckBoxList的AutoPostBack屬性設置爲true
  2. 對事件的SelectedIndexChanged的CheckBoxList的編寫代碼來檢索checkeditem的城市和whereveer你想顯示他們。
0

使用此代碼以獲取選定的國家

protected void CBCountries_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    string result = Request.Form["__EVENTTARGET"]; 
    string[] checkedBox = result.Split('$'); 
    int index = int.Parse(checkedBox[checkedBox.Length - 1]); 

    if (CBCountries.Items[index].Selected) 
    { 
     String Country = CBCountries.Items[index].Value; 
     //query your cities table based on selected Country 
     BindCities(Country);  
    } 
    else 
    { 
    } 
}