.Net中實現(xiàn)無限分類的2個例子
以前總想著搞這個無限分類,今天終于得空好好的看了下,發(fā)現(xiàn)實現(xiàn)的原理還是很簡單的,數(shù)據(jù)結構上,用兩列(分類編號,上級編號)就可以實現(xiàn),可是為了聯(lián)合查詢的方便,一般都再增加一列(深度),在這個實例里,我只用了兩列,剩下的無非就是遞歸著對TreeView進行數(shù)據(jù)綁定而已~~。
public partial class _Default : System.Web.UI.Page
{
BIL bil = new BIL();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind_tree("0",null);
}
}
protected void bind_tree(string ChildNode,TreeNode tn)
{
DataTable dt = bil.GetByClassPre(ChildNode).Tables[0];
foreach (DataRow dr in dt.Rows)
{
TreeNode Node = new TreeNode();
if (tn==null)
{
//根
Node.Text = dr["ClassName"].ToString();
this.TreeView1.Nodes.Add(Node);
bind_tree(dr["ClassId"].ToString(), Node);
}
else
{
//當前節(jié)點的子節(jié)點
Node.Text = dr["ClassName"].ToString();
tn.ChildNodes.Add(Node);
bind_tree(dr["ClassId"].ToString(),Node);
}
}
}
}
上次寫了使用TreeView控件進行無限分類綁定的方法,這回再寫個通用性更好的~~嘿嘿 綁定DropDownList~~思想跟上篇日志很接近,也是使用遞歸,當然,網絡上還有很多人給數(shù)據(jù)庫增加了一個“Depth(深度)”的字段,這樣進行綁定的時候還可以更簡單些哈~~當然,沒有必要的就不加了,還是遞歸使用起來簡單些哈~~不多說了,上代碼哈:
protected void bind_droplist(string ChildNode, string tmp)
{
DataTable dt = bil.GetByClassPre(ChildNode).Tables[0];
foreach (DataRow dr in dt.Rows)
{
if (dr["ClassPre"].ToString()=="0")
{
//如果是根節(jié)點
tmp = "";
DropDownList1.Items.Add(dr["ClassName"].ToString());
bind_droplist(dr["ClassId"].ToString(), tmp + " ");
}
else
{
//不是根節(jié)點
DropDownList1.Items.Add( tmp+"|-" + dr["ClassName"].ToString());
bind_droplist(dr["ClassId"].ToString(), tmp + " ");
}
}
}
相關文章
解析ASP.NET?Core中Options模式的使用及其源碼
這篇文章主要介紹了ASP.NET?Core中Options模式的使用及其源碼解析,在ASP.NET Core中引入了Options這一使用配置方式,其主要是為了解決依賴注入時需要傳遞指定數(shù)據(jù)問題(不是自行獲取,而是能集中配置),需要的朋友可以參考下2022-03-03
.NET中獲取Access新增記錄Id怪現(xiàn)象解決方法
寫了一個函數(shù)獲取Access表中指定用戶Id,要求當傳入的用戶名不存在時,則在表中新增一條記錄并返回Id2012-03-03
.NET/C#如何判斷某個類是否是泛型類型或泛型接口的子類型詳解
這篇文章主要給大家介紹了關于.NET/C#如何判斷某個類是否是泛型類型或泛型接口的子類型的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧2018-09-09

