本文實(shí)例講述了asp.net實(shí)現(xiàn)遞歸方法取出菜單并顯示在DropDownList中的方法。分享給大家供大家參考,具體如下:
樹(shù)形欄目展示以DropDownList的形式顯示出來(lái)。
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(); //過(guò)濾
if (categoryid != 0)
{
layer++; //默認(rèn)為第一層
}
foreach (DataRowView drv in dv)
{
string span = "";
if (categoryid != 0)
{
for (int i = 0; i < layer; i++)//父級(jí)菜單不縮進(jìn)
{
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);
}
}
希望本文所述對(duì)大家asp.net程序設(shè)計(jì)有所幫助。