以前總想著搞這個無限分類,今天終于得空好好的看了下,發現實現的原理還是很簡單的,數據結構上,用兩列(分類編號,上級編號)就可以實現,可是為了聯合查詢的方便,一般都再增加一列(深度),在這個實例里,我只用了兩列,剩下的無非就是遞歸著對TreeView進行數據綁定而已~~。
代碼如下:
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
{
//當前節點的子節點
Node.Text = dr["ClassName"].ToString();
新聞熱點
疑難解答
圖片精選