QueryString动态填充下拉框

时间:2011-08-11 16:53:04

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

如果我使用QueryString返回存储在URL中的某些内容的值,然后使用下面的值填充下拉列表:

industry = Request.QueryString["ind"].ToString();
industrydropdown.SelectedValue = industry;

category = Request.QueryString["cat"].ToString();
CatDropDown.SelectedValue = category

CatDropDown会自动使用SelectedIndexChanged上的代码自动填充,并在industrydropdown上启用AutoPost Back。

    protected void industrydropdown_SelectedIndexChanged(object sender, EventArgs e)
    {
        string value = industrydropdown.SelectedValue;
        switch (value)
        {
            case "Ind1":
                CatDropDown.Items.Clear();
                CatDropDown.Items.Add("Categories for Ind1");
                break;

            case "Ind2":
                CatDropDown.Items.Clear();
                CatDropDown.Items.Add("Categories for Ind2");
                break;
        }

当我还使用on SelectedIndexChanged来填充第二个下拉列表时,如何从QueryString中填充我的CatDropDownList。这可能吗?

2 个答案:

答案 0 :(得分:1)

您可以将代码放在填充Category Dropdownlist的单独方法中。 e.g。

industry = Request.QueryString["ind"].ToString();
industrydropdown.SelectedValue = industry;

fillCatDropDownList(); // Fill the category Dropdown before selection

category = Request.QueryString["cat"].ToString();
CatDropDown.SelectedValue = category

private void fillCatDropDownList()
{
string value = industrydropdown.SelectedValue;
    switch (value)
    {
        case "Ind1":
            CatDropDown.Items.Clear();
            CatDropDown.Items.Add("Categories for Ind1");
            break;

        case "Ind2":
            CatDropDown.Items.Clear();
            CatDropDown.Items.Add("Categories for Ind2");
            break;
    }
 }

然后

protected void industrydropdown_SelectedIndexChanged(object sender, EventArgs e)
{
  fillCatDropDownList();
}

答案 1 :(得分:0)

为您的行业提供有一个默认值,并将catdropdown绑定到它。

相关问题