[.NET]用字符串绑定无限级类别
表 主要字段
category 类别表 ID,Name,Flag(标记),Fatherid(父类ID),Order(排序)
news 信息表 ID,Title,Content,...,ParentID(所属类别ID)
前台: <%=htmlr%>
后台:
EJ.BLL.HelpType htbll = new EJ.BLL.HelpType();
EJ.DAL.HelpType htdal = new EJ.DAL.HelpType();
public string htmlr = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
blTreeDataTable();
}
}
//绑定大类
private void blTreeDataTable()
{
string strwhere = " H_Fatherid=0";
string sort = " H_Order asc";
DataSet ds = htbll.GetAllList(strwhere, sort);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)//循环判断首节点
{
string H_Name = ds.Tables[0].Rows[i]["H_Name"].ToString();
int H_ID = Convert.ToInt32(ds.Tables[0].Rows[i]["H_ID"].ToString());
int H_Order = Convert.ToInt32(ds.Tables[0].Rows[i]["H_Order"].ToString());
int H_Fatherid = Convert.ToInt32(ds.Tables[0].Rows[i]["H_Fatherid"].ToString());
int H_Flag = Convert.ToInt32(ds.Tables[0].Rows[i]["H_Flag"].ToString());
htmlr += " <div style='padding-right: 12px; padding-left: 18px; padding-bottom: 5px; padding-top: 12px;'>";
htmlr += "<h3>";
htmlr += H_Name;
htmlr += "</h3>";
htmlr += "<p>";
blSonTreeDataTable(H_ID);
htmlr += "</p>";
htmlr += "</div><br style='clear:both;'/>";
}
}
//绑定小类
private void blSonTreeDataTable(int ParentID)
{
string strwhere = " H_Fatherid="+ParentID ;
string sort = " H_Order asc";
DataSet ds = htbll.GetAllList(strwhere, sort);
if (ds.Tables[0].Rows.Count > 0)
{
bindhelp(ParentID);
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)//循环判断子节点
{
string H_Name = ds.Tables[0].Rows[j]["H_Name"].ToString();
int H_ID = Convert.ToInt32(ds.Tables[0].Rows[j]["H_ID"].ToString());
int H_Order = Convert.ToInt32(ds.Tables[0].Rows[j]["H_Order"].ToString());
int H_Fatherid = Convert.ToInt32(ds.Tables[0].Rows[j]["H_Fatherid"].ToString());
int H_Flag=Convert.ToInt32(ds.Tables[0].Rows[j]["H_Flag"].ToString());
htmlr += "<div style='width:160px; float:left;'><IMG src='images/down.jpg' />";
htmlr += "<span style='color:#000;'>";
htmlr += H_Name;
htmlr += "</span>";
htmlr += "<br/>";
bindhelp_son(H_ID);
htmlr +="</div> ";
if ((j+1)%4==0)
{
htmlr += "<div style='clear:both; display: block;margin:0px; padding:0px;'></div> ";
}
}
}
else
{
bindhelp(ParentID);
}
}
//绑定帮助信息
private void bindhelp(int HL_HelpType)
{
string strwhere = " HL_HelpType=" + HL_HelpType;
string sort = " HL_Order asc";
EJ.BLL.HelpList bll = new EJ.BLL.HelpList();
DataSet ds = bll.GetAllList(strwhere, sort);
if (ds.Tables[0].Rows.Count > 0)
{
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)//循环判断子节点
{
string HL_Title = ds.Tables[0].Rows[j]["HL_Title"].ToString();
int HL_ID = Convert.ToInt32(ds.Tables[0].Rows[j]["HL_ID"].ToString());
htmlr += "<a href='help.aspx?id=" + HL_ID + "'" + ">" + HL_Title + "</a> | ";
}
htmlr = htmlr.Substring(0, htmlr.LastIndexOf("|"));
}
}
//绑定帮助信息
private void bindhelp_son(int HL_HelpType)
{
string strwhere = " HL_HelpType=" + HL_HelpType;
string sort = " HL_Order asc";
EJ.BLL.HelpList bll = new EJ.BLL.HelpList();
DataSet ds = bll.GetAllList(strwhere, sort);
if (ds.Tables[0].Rows.Count > 0)
{
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)//循环判断子节点
{
string HL_Title = ds.Tables[0].Rows[j]["HL_Title"].ToString();
int HL_ID = Convert.ToInt32(ds.Tables[0].Rows[j]["HL_ID"].ToString());
htmlr += " <p> <IMG src='images/left.jpg' /><a href='help.aspx?id="+HL_ID+"'"+">" + HL_Title + "</a></p>";
}
}
}