我的网页上有两个下拉列表。第二个依赖于第一个加载。 例如,如果第一个是“墨尔本”,第二个列出墨尔本的所有郊区。
代码完全正常工作,但是当我第一次加载页面时,第二个dropdownlist2没有填充。 我需要再次选择“墨尔本”以填充第二个下拉列表。
这是我的Page load
代码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();
}
}
我不确定如何解决这个简单的问题?
答案 0 :(得分: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()));
}
}
}
答案 1 :(得分: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);
}
答案 2 :(得分:0)
使用您的标记更新,您是否设置了Autopost="true"
,或者您可以使用Ajaxtoolkit's cascading dropdowns
答案 3 :(得分:0)
尝试在DropDownList1.DataBind();
下添加此行DropDownList1.SelectedIndex = 0;
答案 4 :(得分: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();
}
}
答案 5 :(得分:0)
最后在页面加载中调用此函数
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();
}
}
答案 6 :(得分: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");
}
}