0

我有一个数据表如下:

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
........
........

所以我想根据上面的数据表将菜单项生成到一个无序列表中,这样具有 null parentid 的项目应该是第一级菜单,而其他项目将是基于它们的 parentid 的子菜单,如下所示:

<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
4

1 回答 1

0

出于纯粹的无聊,使用 asp.net 控件的 Compositin(每个控件都有一个 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;

   }
于 2012-06-04T19:24:45.847 回答