asp.net实现递归方法取出菜单并显示在DropDownList中(分栏形式)

时间:2022-09-13 07:31:06

本文实例讲述了asp.net实现递归方法取出菜单并显示在DropDownList中的方法。分享给大家供大家参考,具体如下:

树形栏目展示以DropDownList的形式显示出来。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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程序设计有所帮助。