本文实例讲述了asp.net实现递归方法取出菜单并显示在DropDownList中的方法。分享给大家供大家参考,具体如下:
树形栏目展示以DropDownList的形式显示出来。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable table = bllSite.GetChannelNames(CurrentUser.ParkID);
BindChannelType(table, 0, 0);
}
}
//递归方法取
private void BindChannelType(DataTable dt, int categoryid, int layer)
{
DataView dv = new DataView(dt);
dv.RowFilter = " Pid= " + categoryid.ToString(); //过滤
if (categoryid != 0)
{
layer++; //默认为第一层
}
foreach (DataRowView drv in dv)
{
string span = "";
if (categoryid != 0)
{
for (int i = 0; i < layer; i++)//父级菜单不缩进
{
span += " ";
}
span += "└";
}
ListItem li = new ListItem();
li.Text = span + drv["ChannelName"].ToString();
li.Value = drv["Id"].ToString();
this.ddlChannel.Items.Add(li);
BindChannelType(dt, Convert.ToInt32(drv["Id"]), layer);
}
}
希望本文所述对大家asp.net程序设计有所帮助。