2011-11-30 109 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) 
}