2012-06-04 5 views
0

는 내가 datatble 다음과 같이이 바인딩 :HTML은

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

그래서 내가 널 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> 

내 코드입니다 : 다른 사람이 같은 자신의 parentid에 따라 하위 메뉴가 될 것

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 
+1

으로 시작하면 코드에 재귀가 없습니다. – YavgenyP

+0

메뉴의 트리 구조는 그것을 렌더링하는 코드에서 재귀를 의미한다고 가정합니다. –

+0

도움이되는지 아닌지 알려 주셔서 감사합니다. 답변을 수락/거부하는 것이 정말 어렵습니까? – YavgenyP

답변

0

순수한 지루함에서 벗어난 컨트롤 asp.net (각각의 컨트롤에는 Controls 속성이 있음)은 이미 트리이며 그런 식으로 렌더링됩니다 (즉, 실제 재귀가 필요 없다는 의미입니다) 및 인덱스 배열 생성 된 컨트롤에 쉽게 액세스 할 수 있도록

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; 

    }