HTML 递归绑定

本文关键字:绑定 递归 HTML | 更新日期: 2023-09-27 18:36:22

我有一个数据如下:

id      menuname    url                  parentid
1       Home        ~/Home.aspx          NULL   
2       Product     ~/products.aspx      NULL 
3       Services    ~/services.aspx      NULL   
4       ERP     ~/erp.aspx           2
5       HRM         ~/hrm.aspx           4
7       Payroll     ~/payroll.aspx       4
8       Programming ~/programming.aspx   3
9       Advertising ~/advert.aspx        3
10      Television Advert ~/tvadvert.aspx 9
11      Radio Advert ~/radioadvert.aspx  9
........
........

所以我想根据上面的数据表将菜单项生成到无序列表中,以便具有空父级 id 的项目应该是第一级菜单其他人将根据其父 ID 成为子菜单,如下所示:

<ul class="menu">
    <li><a href="home.aspx">Home</a></li>
    <li><a href="produc.aspx">Product</a>
            <ul>
                <li>
                <a href="erp.aspx">ERP</a>  
                    <ul>
                        <li><a href="hrm.aspx">HRM</a></li>
                        <li><a href="payroll.aspx">Payroll</a></li>
                    </ul>
                </li>
            </ul>
    </li>
    <li><a href="services.aspx">Services</a>
            <ul>
                <li><a href="programming.aspx">Advertising</a></li>
                <li><a href="advert.aspx">Programming</a></li>
            </ul>
    </li>
    .....etc
</ul>

下面是我的代码,看起来不完整:

public static String AddToList() 
    {

        DataTable table = new DataTable();
        table = GetMenus();
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        foreach (DataRow row in table.Rows())
        {
            string parentId = row["parentmenuId"].ToString();
            //string url = Server.MapPath(m.Url);
            if (string.IsNullOrEmpty(parentId))
            {
                sb.Append(String.Format("<ul class='"menu'"><li><a href='"{0}'">{1}</a></li></ul>", row["Url"].ToString(), row["Description"].ToString()));
            }
        }
        return sb.ToString();
    }


This gets all top menu but all other effort to get submenu doesnt work. Pls help me out.
Thanks in advance

HTML 递归绑定

出于纯粹的无聊,usnig 控件的复合 asp.net(每个控件都有一个 Controls 属性),它已经是一棵树并呈现为这样(这意味着我不需要真正的递归),以及一个索引数组,以便轻松按 id 访问创建的控件

  public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Form.Controls.Add(AddToList()); 
    }
   public static HtmlGenericControl AddToList()      
   {
       HtmlGenericControl menu = new HtmlGenericControl("ul");
       DataTable table = GetMenus();
       //NOTE  i initialized this to 7, because this is the max rows id, and
       // im basing my builder on indexes to work. This can be easily replaced to
       // a dictionary<int,HtmlGenericControl> or any othre suitable collection
       HtmlGenericControl[] arrayOfLists = new HtmlGenericControl[7];
       foreach (DataRow row in table.Rows)
       {
          //assume control has no children, unless proved otherwise
           HtmlGenericControl temp  = new HtmlGenericControl("li");
           //add the control to its indexes place in the array and init it
           arrayOfLists[(int)row["id"] - 1] = temp;
           HtmlAnchor link = new HtmlAnchor();
           link.HRef = row["url"].ToString();
           link.InnerText = row["menuname"].ToString();
           temp.Controls.Add(link);
           int? parentId = string.IsNullOrEmpty(row["parentmenuId"].ToString()) ? null : (int?)int.Parse(row["parentmenuId"].ToString());
           if (parentId.HasValue)
           {
               // if a control has a parent - make its parent a ul insead of li
              // and add it to the parents collection
               arrayOfLists[parentId.Value - 1].TagName = "ul";
               arrayOfLists[parentId.Value - 1].Controls.Add(arrayOfLists[(int)row["id"] - 1]);
           }
           else
           {
                // no parent = add to the first created ul menu
               menu.Controls.Add(temp);
           }
       }
       return menu;     
   }
   public static DataTable GetMenus()
   {
       DataTable dt = new DataTable ();
       dt.Columns.Add("id", typeof(int));
       dt.Columns.Add("menuname", typeof(string));
       dt.Columns.Add("url", typeof(string));
       dt.Columns.Add("parentmenuId", typeof(string));
       dt.Rows.Add(1,"Home","~/Home.aspx",        null  );
       dt.Rows.Add( 2,"Product","~/products.aspx",    null);
       dt.Rows.Add(3,"services", "~/services.aspx",null);
       dt.Rows.Add(4, "ERP",  "~/erp.aspx",           "2" );
       dt.Rows.Add( 5  ,"HRM" ,"~/hrm.aspx",           "4" );
       dt.Rows.Add(7, " Payroll", "~/payroll.aspx", "4");
       return dt;
   }