2011-11-30 98 views
0

我正在使用C#創建一個ASP.NET應用程序,我想要做的是創建一個過濾器。舉例來說,我有2個下拉框下拉框過濾

countryDropDownBox 
stateDropDownBox 

當我選擇一個國家,我想自動填充stateDropDownBox我想要做的是,我該怎麼辦呢?

回答

1

你必須設置countryDropDownBox控制的AutoPostBack屬性,但你可以張貼一些代碼來幫助你。

1

如果你不介意回傳,剛剛成立的國家下拉列表的AutoPostBack =真和重新綁定狀態,來自全國的設定值的下拉列表下拉列表。絕對是最簡單的方法。

如果您不想回發帖子,請將其綁定爲使用javascript或javascript/ajax調用,就像其他人說的那樣。

1

在aspx文件

<asp:DropDownList ID="countryDropDownBox" runat="server" AutoPostBack="True" OnSelectedIndexChanged="countryDropDownBox_SelectedIndexChanged"/> 

<asp:DropDownList ID="stateDropDownBox" runat="server"/> 

在aspx.cs文件

private void countryDropDownBox_SelectedIndexChanged() 
{ 
    string selectedCountry = countryDropDownBox.SelectedItem.Text; 
    FillStateDropwDownList(selectedCountry); 

} 
// I must say that you don't have to use a parameter for that 
// you can basically take countryDropDownBox.SelectedItem.Text in the below method 
// but I prefer to do that in countryDropDownBox_SelectedIndexChanged because 
// selectedCountry actually belongs to countryDropDownBox 
private void FillStateDropwDownList(string selectedCountry) 
{ 
stateDropDownBox.Items.Clear(); 
//use selectedCountry for filtering query from database tale or something else 
//use a foreach(I'd prefe for) loop to add the items in the returning string list which contains the states for selected country by stateDropDownBox.Items.Add(item) 
}