2012-07-21 84 views
1

我正在使用2個下拉列表。國家第一,國家第二。如果我從1英尺的下拉列表中選擇印度,則第二個將自動從數據庫中自動綁定印度的所有州。來自數據庫的ASP dropdownlist數據綁定

我已經使用country_tbl的國家,並與第一個下拉列表和india_tblus_tblsri_tbl綁定綁定有關這些國家的狀態。

請幫幫我。我該怎麼辦?

我的代碼如下:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
     method1(); 
    } 
} 

protected void method1() 
{ 
    string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true"; 
    SqlConnection con = new SqlConnection(s1); 
    string s2 = "select * from country"; 
    SqlCommand cmd = new SqlCommand(s2, con); 
    con.Open(); 
    SqlDataReader dr = cmd.ExecuteReader(); 
    DropDownList1.DataTextField = "name"; 
    DropDownList1.DataValueField = "name"; 
    DropDownList1.DataSource = dr; 
    DropDownList1.DataBind(); 
    con.Close(); 
    dr.Close(); 
} 

protected void methodInd() 
{ 
    string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true"; 
    SqlConnection con = new SqlConnection(s1); 
    string s2 = "select * from india"; 
    SqlCommand cmd = new SqlCommand(s2, con); 
    con.Open(); 
    SqlDataReader dr = cmd.ExecuteReader(); 
    DropDownList2.DataTextField = "name"; 
    DropDownList2.DataValueField = "name"; 
    DropDownList2.DataSource = dr; 
    DropDownList2.DataBind(); 
    con.Close(); 
    dr.Close(); 
} 

protected void methodpak() 
{ 
    string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true"; 
    SqlConnection con = new SqlConnection(s1); 
    string s2 = "select * from pakistan"; 
    SqlCommand cmd = new SqlCommand(s2, con); 
    con.Open(); 
    SqlDataReader dr = cmd.ExecuteReader(); 
    DropDownList2.DataTextField = "name"; 
    DropDownList2.DataValueField = "name"; 
    DropDownList2.DataSource = dr; 
    DropDownList2.DataBind(); 
    con.Close(); 
    dr.Close(); 
} 

protected void methodsri() 
{ 
    string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true"; 
    SqlConnection con = new SqlConnection(s1); 
    string s2 = "select * from srilanka"; 
    SqlCommand cmd = new SqlCommand(s2, con); 
    con.Open(); 
    SqlDataReader dr = cmd.ExecuteReader(); 
    DropDownList2.DataTextField = "name"; 
    DropDownList2.DataValueField = "name"; 
    DropDownList2.DataSource = dr; 
    DropDownList2.DataBind(); 
    con.Close(); 
    dr.Close(); 
} 

protected void submit_Click(object sender, EventArgs e) 
{ 

} 

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (DropDownList1.SelectedItem.Text=="india") 
    { 
     methodInd(); 
    } 
    else if (DropDownList1.SelectedItem.Text=="pakistan") 
    { 
     methodpak(); 
    } 
    else if (DropDownList1.SelectedItem.Text=="srilanka") 
    { 
     methodsri(); 
    } 
} 
+1

有什麼問題? – codingbiz 2012-07-21 12:51:17

+0

你對每個國家都有不同的表格嗎?你確定你做對了嗎?您可以將所有州保存在一張表中,並且有一個CountryID列來標識「這些州屬於哪個國家」。你可以像查詢它'SELECT ID,狀態的名稱哪裏COUNTRY_ID = theCountryId' – Shyju 2012-07-21 15:02:34

回答

1

你的做法是錯誤的,你不應該有每個國家的國家單獨的表,所以你可以有一個查詢和一個方法綁定到你的狀態列表

您的模式更改爲

CREATE TABLE Country 
(
    id int, 
    country_name, 
    primary key(id) 
) 

Country_State 
(
    id int, 
    state_name, 
    country_id, 
    primary key(id) 
) 

的COUNTRY_ID是鏈接回到鄉下

0的外鍵
country 
------------------------ 
id Name 
------------------------ 
1 India 
2 Pakistan 


country_state 
----------------------------------- 
id Name    country_id 
------------------------------------ 
1 Delhi    1 
2 Bangladesh  1 
3 Some Indians  1 
4 S1_Pakistan  2 
5 S2_Pakistan  2 

您查詢

Select Id, Name from Country 

Select Id, Name From country_states Where country_id = @id 

呼叫只是一個綁定所有國家

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    string country_id = DropDownList1.SelectedValue; 
    BindStatesByCountry(country_id); 
} 

protected void methodsri(string countryId) //change this to BindStatesByCountry 
{ 
    string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true"; 
    SqlConnection con = new SqlConnection(s1); 
    con.Open(); 

    string s2 = "select * from country_states where [email protected]ryId"; 
    SqlCommand cmd = new SqlCommand(s2, con); 
    cmd.Parameters.AddWithValue("@countryId", countryId); 
    SqlDataReader dr = cmd.ExecuteReader(); 
    DropDownList2.DataTextField = "name"; 
    DropDownList2.DataValueField = "name"; 
    DropDownList2.DataSource = dr; 
    DropDownList2.DataBind(); 
    con.Close(); 
    dr.Close(); 
} 

希望這有助於

+0

非常感謝... – 2012-07-21 17:27:30

+0

非常感謝你 – Mahyar 2013-12-18 08:39:35