下拉框过滤

时间:2011-11-30 21:52:17

标签: c# asp.net drop-down-menu

我正在使用C#创建一个ASP.NET应用程序,我想要做的是创建一个过滤器。例如,我有2个下拉框

countryDropDownBox
stateDropDownBox

我想要做的是当我选择一个我想自动填充stateDropDownBox的国家时,我该怎么做?

6 个答案:

答案 0 :(得分:1)

您想要级联下拉。

AJAX Toolkit在这里有一个例子:

http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx

希望这有帮助,

答案 1 :(得分:1)

答案 2 :(得分:1)

我找到了一个很好的解决方案。

希望这也对你有帮助。

http://www.codeproject.com/KB/aspnet/CitiesDropDownList.aspx

答案 3 :(得分:1)

如果您不介意回发,只需将国家/地区下拉列表设置为AutoPostback = true,然后从国家/地区下拉列表的选定值重新绑定州下拉列表。绝对是最简单的方法。

如果您不想回复帖子,请使用javascript或javascript / ajax调用绑定它,就像其他人所说的那样。

答案 4 :(得分: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)
}

答案 5 :(得分:1)

你必须设置countryDropDownBox控件的autopostback属性,但是你可以发布一些代码来帮助你。