asp. net implements a recursive method to fetch the menu and display it in DropDownList in of column form

  • 2021-07-24 10:49:24
  • OfStack

This article illustrates how asp. net implements a recursive method to fetch a menu and display it in DropDownList. Share it for your reference, as follows:

The tree column display is displayed in the form of DropDownList.


protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
      DataTable table = bllSite.GetChannelNames(CurrentUser.ParkID);
      BindChannelType(table, 0, 0);
    }
}
// Recursive method takes 
private void BindChannelType(DataTable dt, int categoryid, int layer)
{
    DataView dv = new DataView(dt);
    dv.RowFilter = " Pid= " + categoryid.ToString(); // Filter   
    if (categoryid != 0)
    {
      layer++; // Default to the 1 Layer 
    }
    foreach (DataRowView drv in dv)
    {
      string span = "";
      if (categoryid != 0)
      {
        for (int i = 0; i < layer; i++)// Parent menus are not indented 
        {
          span += "   ";
        }
        span += " Whatever ";
      }
      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);
    }
}

For more readers interested in asp. net, please check the topics on this site: "asp. net String Operation Skills Summary", "asp. net Operation XML Skills Summary", "asp. net File Operation Skills Summary", "asp. net ajax Skills Summary" and "asp. net Cache Operation Skills Summary".

I hope this paper is helpful to everyone's asp. net programming.


Related articles: