在页面加载上填充两个下拉列表

本文关键字:两个 下拉列表 填充 加载 | 更新日期: 2023-09-27 18:19:13

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

代码是完美的工作,但当我第一次加载页面,第二个dropdownlist2没有填充。我需要再次选择"Melbourne"来填充第二个下拉列表。

这是我的代码页面加载
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();
    }
}

我不知道如何解决这个简单的问题?

在页面加载上填充两个下拉列表

试试这个

    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);
   }

可以从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()));
        }
    }
}

更新你的标记,你是否设置了Autopost="true"或者你可以使用Ajaxtoolkit的级联下拉

尝试在DropDownList1.DataBind();

DropDownList1.SelectedIndex = 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();
    }
}

在页面加载结束时调用此函数

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();
        }
    }

我将从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();
    }
}
然后

on your 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");
    }

}