0

我的一个网页中有两个下拉列表。第二个依赖第一个加载。例如,如果第一个是“Melbourne”,则第二个列出墨尔本的所有郊区。

代码运行良好,但是当我第一次加载页面时,第二个 dropdownlist2 没有填充。我需要再次选择“墨尔本”来填充第二个下拉列表。

这是我的页面加载代码

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        if (Session["Username"] == null)
        {
            Response.Redirect("LoginPage.aspx");
        }

        if (getAccess(Session["Username"].ToString()) == false)
        {
            Response.Redirect("Unauthorized.aspx");
        }

        DataSet ds = GetAllCategory();
        if (ds.Tables.Count > 0)
        {
            DropDownList1.DataTextField = "identifier";
            DropDownList1.DataValueField = "OS_ID"; //Change field to one you want.
            DropDownList1.DataSource = ds.Tables[0];
            DropDownList1.DataBind();
        }
    }
}

这是选定的索引更改代码

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    DataSet ds = softwareType(Convert.ToInt32(DropDownList1.SelectedValue));
    if (ds.Tables.Count > 0)
    {
        DropDownList2.DataTextField = "identifier";
        DropDownList2.DataValueField = "ST_ID"; //Change field to one you want.
        DropDownList2.DataSource = ds.Tables[0];
        DropDownList2.DataBind();
    }
}

我不确定如何解决这个简单的问题?

4

7 回答 7

1

一个想法可能是从页面加载时GetAllCategory
绑定第二个值中获取第一个值。dropdown list

public void PopulateDropdownList2(int selectedValue)
{
    DropDownList2.Items.Clear();
    DataSet ds = softwareType(selectedValue);
    if (ds.Tables.Count > 0)
    {
           DropDownList2.DataTextField = "identifier";
           DropDownList2.DataValueField = "ST_ID"; //Change field to one you want.
           DropDownList2.DataSource = ds.Tables[0];
           DropDownList2.DataBind();
    }
}

调用上面的函数

if (!Page.IsPostBack)
{

}

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        if (Session["Username"] == null)
        {
            Response.Redirect("LoginPage.aspx");
        }

        if (getAccess(Session["Username"].ToString()) == false)
        {
            Response.Redirect("Unauthorized.aspx");
        }

        DataSet ds = GetAllCategory();
        if (ds.Tables.Count > 0)
        {
            DropDownList1.DataTextField = "identifier";
            DropDownList1.DataValueField = "OS_ID"; //Change field to one you want.
            DropDownList1.DataSource = ds.Tables[0];
            DropDownList1.DataBind();
            PopulateDropdownList2(Convert.ToInt32(ds.Tables[0].Rows[0]["identifier"].ToString()));
        }

    }
}
于 2013-06-20T05:59:08.980 回答
1

尝试这个

    DataSet ds = GetAllCategory();
    if (ds.Tables.Count > 0)
    {
        DropDownList1.DataTextField = "identifier";
        DropDownList1.DataValueField = "OS_ID"; //Change field to one you want.
        DropDownList1.DataSource = ds.Tables[0];
        DropDownList1.DataBind();
    }

   if(DropDownList1.Items.Count > 0)
   { 
       DropDownList1.SelectedIndex = 0;
       DropDownList1_SelectedIndexChanged(this,null);
   }
于 2013-06-20T06:04:53.457 回答
0

使用您的标记进行更新,您是否已设置Autopost="true"或者您可以使用Ajaxtoolkit 的级联下拉菜单

于 2013-06-20T05:59:17.203 回答
0

尝试在 DropDownList1.DataBind() 下方添加这一行;

DropDownList1.SelectedIndex = 0;
于 2013-06-20T06:00:22.457 回答
0

在页面加载 if(!ispostback) 以及选择第一个 ddl 的索引更改时调用它

private void fillSecondDdl()
{
DataSet ds = softwareType(Convert.ToInt32(DropDownList1.SelectedValue));
    if (ds.Tables.Count > 0)
    {
        DropDownList2.DataTextField = "identifier";
        DropDownList2.DataValueField = "ST_ID"; //Change field to one you want.
        DropDownList2.DataSource = ds.Tables[0];
        DropDownList2.DataBind();
    }
}
于 2013-06-20T06:01:17.073 回答
0

最后在pageload中调用这个函数

fillSecondDropdown(Convert.ToInt32(DropDownList1.SelectedValue));



 public void  fillSecondDropdown(int firstdropdownValue)
    {
    DataSet ds = softwareType(firstdropdownValue);
        if (ds.Tables.Count > 0)
        {
            DropDownList2.DataTextField = "identifier";
            DropDownList2.DataValueField = "ST_ID"; //Change field to one you want.
            DropDownList2.DataSource = ds.Tables[0];
            DropDownList2.DataBind();
        }
    }
于 2013-06-20T06:01:52.137 回答
0

我将从 DropDownList1_SelectedIndexChanged() 中重构该函数并将其放入一个新函数中:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
   ddl1Changed(DropDownList1.SelectedValue);
}

public void ddl1Changed(string selectedValue)
{
    DataSet ds = softwareType(Convert.ToInt32(selectedValue));
    if (ds.Tables.Count > 0)
    {
        DropDownList2.DataTextField = "identifier";
        DropDownList2.DataValueField = "ST_ID"; //Change field to one you want.
        DropDownList2.DataSource = ds.Tables[0];
        DropDownList2.DataBind();
    }
}

然后

在您的 Page_Load 上:

public void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        if (Session["Username"] == null)
        {
            Response.Redirect("LoginPage.aspx");
        }

        if (getAccess(Session["Username"].ToString()) == false)
        {
            Response.Redirect("Unauthorized.aspx");
        }

        DataSet ds = GetAllCategory();
        if (ds.Tables.Count > 0)
        {
            DropDownList1.DataTextField = "identifier";
            DropDownList1.DataValueField = "OS_ID"; //Change field to one you want.
            DropDownList1.DataSource = ds.Tables[0];
            DropDownList1.DataBind();
        }

        ddl1Changed("Melbourne");
    }


}
于 2013-06-20T06:03:07.690 回答