下拉框过滤
问题描述:
我正在使用C#创建一个ASP.NET应用程序,我想要做的是创建一个过滤器。举例来说,我有2个下拉框下拉框过滤
countryDropDownBox
stateDropDownBox
当我选择一个国家,我想自动填充stateDropDownBox我想要做的是,我该怎么办呢?
答
你必须设置countryDropDownBox控制的AutoPostBack属性,但你可以张贴一些代码来帮助你。
答
您想要级联下拉。
的AJAX工具包在这里有一个例子:
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx
希望这有助于,可在线教程
答
如果你不介意回传,刚刚成立的国家下拉列表的AutoPostBack =真和重新绑定状态,来自全国的设定值的下拉列表下拉列表。绝对是最简单的方法。
如果您不想回发帖子,请将其绑定为使用javascript或javascript/ajax调用,就像其他人说的那样。
答
在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)
}