for循环递归树

for循环递归树

protected string _menu = string.Empty;
public void FirstAnsyData()
{
try
{
// List<object> lsNode = new List<object>();
string strsql = "select * from S_module";
DataSet dt = DbHelperSQL.Query(strsql);

// lsNode = getList(tab);
//// HttpContext.Current.Response.Write(ConvertJson.ListToJson(lsNode2));
// HttpContext.Current.Response.Write(JsonConvert.SerializeObject(lsNode));
//// Response.Write();//用到了Newtonsoft.dll 转化成Json格式
StringBuilder sb = new StringBuilder();

DataRow[] rows = dt.Tables[0].Select("PARENTID='1'");
foreach (DataRow dr in rows)
{
string id = dr["MODULEID"].ToString();
string name = dr["MODULENAME"].ToString();
sb.AppendFormat("<li MODULEID=\"{2}\"><a href=\"javascript:;\" onclick=\"gotoURL('{0}');\">{1}</a>\r\n", dr["LINK"].ToString(), name, id);//href可以写需要的链接地址
sb.Append(GetSubMenu(id, dt.Tables[0]));
sb.Append("</li>\r\n");
}
_menu = sb.ToString();

}
catch (Exception)
{

throw;
}

}

/// <summary>
/// 递归调用生成无限级别
/// </summary>
/// <param name="pid"></param>
/// <param name="dt"></param>
/// <returns></returns>
private string GetSubMenu(string pid, DataTable dt)
{
StringBuilder sb = new StringBuilder();
DataRow[] rows = dt.Select("PARENTID='" + pid + "'");
sb.Append("<ul>\r\n");
foreach (DataRow dr in rows)
{
string id = dr["MODULEID"].ToString();
string name = dr["MODULENAME"].ToString();
sb.AppendFormat("<li MODULEID=\"{2}\"><a href=\"javascript:;\" onclick=\"gotoURL('{0}');\">{1}</a>\r\n", dr["LINK"].ToString(), name, id);//href可以写需要的链接地址
sb.Append(GetSubMenu(id, dt)); //递归
sb.Append("</li>\r\n");
}
sb.Append("</ul>\r\n");
return sb.ToString();
}

for循环递归树

上一篇:SQL数据库中把一个表中的数据复制到另一个表中


下一篇:2016-2-1 Servlet细节