递归构建HTML

<ul class="role"> <li class="roleTitle"><h2>组织人员选择:</h2></li> <li class="close"><a class="home" href="#" mce_href="#">上海XX服饰有限公司</a> <!--First Menu Begin--> <ul> <li><a href="#" mce_href="#">XX集团</a></li> <li><a href="#" mce_href="#">董事长室</a></li> <li><a href="#" mce_href="#">总经理室</a></li> <li class="open"><a href="#" mce_href="#">行政中心</a> <!--Second Menu Begin--> <ul> <li><a href="#" mce_href="#">信息部</a></li> <li><a href="#" mce_href="#">党委工会</a></li> <li class="open"><a href="#" mce_href="#">人事行政部</a> <!--Third Menu Begin--> <ul> <li><a href="#" mce_href="#">人力资源科</a></li> <li><a href="#" mce_href="#">行政科</a></li> </ul> <!--Third Menu End--> </li> <li><a href="#" mce_href="#">财务部</a></li> <li><a href="#" mce_href="#">物流采购部</a></li> <li><a href="#" mce_href="#">企业管理部</a></li> </ul> <!--Second Menu End--> </li> </ul> <!--First Menu End--> </li> </ul>

 

 

如何递归构建HTML 看如下代码

 

protected string DoRecursiveHTML(DataSet ds, string nodeID) { DataView dv = ds.Tables[0].DefaultView; dv.RowFilter = " ParentID = '" + nodeID + "'"; if (dv.Count > 0) { //string retStr = "<ul>"; StringBuilder sb = new StringBuilder(); sb.Append("<ul>"); foreach (DataRowView row in dv) { //retStr = retStr + "<li><a href="/" mce_href="/""javascript:alert('"; //retStr = retStr + row["NodeID"].ToString(); //retStr = retStr + "')/">"; //retStr = retStr + row["NodeName"].ToString(); //retStr = retStr + "</a>"; //retStr = retStr + DoRecursiveHTML(ds, row["NodeID"].ToString()); //retStr = retStr + "</li>"; DataView dvChd = ds.Tables[0].DefaultView; dvChd.RowFilter = " ParentID = '" + row["NodeID"].ToString() + "'"; sb.AppendFormat("<li {2}><a href="/" mce_href="/""javascript:alert('{0}')/">{1}</a>{3}</li>", row["NodeID"].ToString(), row["NodeName"].ToString(), (dvChd.Count > 0 ? "class='open'" : ""), DoRecursiveHTML(ds, row["NodeID"].ToString())); } sb.Append("</ul>"); ; return sb.ToString(); } else { return ""; } } protected void Page_Load(object sender, EventArgs e) { DataSet ds = 你的数据方法(); Response.Write(DoRecursiveHTML(ds,"-1")); }

你可能感兴趣的:(C#,asp.net)