本文介绍了在ASP.net的父列表项下创建子列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨!
如何在不使用数据库的情况下将子列表项添加到ASP.net下拉列表中的父列表项中?
Hi!
How can I add child list items to a parent list item in a dropdown list in ASP.net without using a database?
Thanks in advance!
推荐答案
Parent1
Child 1
Child 2
Parent 2
Child 3
Child 4
Child 5
Child 6
下拉菜单无法像这样工作-它仅支持一个级别:
Drop down just doesn''t work like that - it only supports a single level:
Item 1
Item 2
Item 3
相反,考虑使用TreeView(如果需要真正的层次结构列表),或者如果仅需要一个级别,则使用两个下拉列表.当第一个更改时,它将更改第二个的内容.
Instead, consider a TreeView (if you need a truly hierarchical list) or use two drop downs if you only need one level. When the first changes, it alters the content of the second.
public partial class Default : System.Web.UI.Page
{
// First DropdownList items
protected string[] Movies ={ "select a movie", "Movie_1", "Movie_2", "Movie_3" };
// Second DropdownList items. One among these to be choosen.
protected string[] Movie_1={"Theatre_1","Theatre_2","Theatre_3"};
protected string[] Movie_2 ={ "Theatre_4", "Theatre_5" };
protected string[] Movie_3 ={ "Theatre_5", "Theatre_6", "Theatre_7" };
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.DataSource = Movies;
DropDownList1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex == 1)
{
DropDownList2.DataSource = Movie_1;
DropDownList2.DataBind();
}
if (DropDownList1.SelectedIndex == 2)
{
DropDownList2.DataSource = Movie_2;
DropDownList2.DataBind();
}
if (DropDownList1.SelectedIndex == 3)
{
DropDownList2.DataSource = Movie_3;
DropDownList2.DataBind();
}
}
}
这篇关于在ASP.net的父列表项下创建子列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!