Note: This document uses dataset. If you want to see the datareader filling scheme, see:
Http://blog.csdn.net/mail_ricklee/archive/2005/02/17/290515.aspx
Control: Microsoft. Web. UI. webcontrols. Treeview
It can be applied to all filling methods of the control and extract data from the database.
Very, very simple, fast, and Efficient Node Filling Solution
Using system;
Using Microsoft. Web. UI. webcontrols;
Using system. Data;
Namespace webapplication1_del1
{
/// <Summary>
/// Summary of menufill.
/// </Summary>
Public class treeclass
{
Public treeclass ()
{
//
// Todo: add the constructor logic here
//
}
# Region tree structure Filling
Public bool buildtree (Microsoft. Web. UI. webcontrols. Treeview treeviewname, dataset DS)
{
// Clear all nodes
Treeviewname. nodes. Clear ();
// If it is the highest level, modify it to adapt to the relationship in the relation of DS.
Foreach (datarow dbrow in DS. Tables [0]. Rows)
{
If (dbrow ["parentmenu"]. tostring () = "0 ")
{
Dbrow ["parentmenu"] = dbnull. value;
}
}
DS. relations. Add ("noderelation", DS. Tables [0]. Columns ["childmenuid"], DS. Tables [0]. Columns ["parentmenu"]);
// Cyclically bind the parent node
Foreach (datarow dbrow in DS. Tables [0]. Rows)
{
If (dbrow. isnull ("parentmenu "))
{
Microsoft. Web. UI. webcontrols. treenode newnode = createnode (dbrow ["menuname"]. tostring (), dbrow ["navigateurl"]. tostring ());
Treeviewname. nodes. Add (newnode );
Populatesubtree (dbrow, newnode );
}
}
Return true;
}
/// <Summary>
/// Bind a subnode cyclically
/// </Summary>
/// <Param name = "dbrow"> dataset row </param>
/// <Param name = "Node"> nodes to be added </param>
Private void populatesubtree (datarow dbrow, Microsoft. Web. UI. webcontrols. treenode node)
{
Foreach (datarow childrow in dbrow. getchildrows ("noderelation "))
{
Microsoft. Web. UI. webcontrols. treenode childnode = createnode (childrow ["menuname"]. tostring (), childrow ["navigateurl"]. tostring ());
Node. nodes. Add (childnode );
Populatesubtree (childrow, childnode );
}
}
/// <Summary>
/// Add attributes to all nodes
/// </Summary>
/// <Param name = "text"> render text </param>
/// <Param name = "imgurl"> URL </param>
/// <Returns> return node </returns>
Private Microsoft. Web. UI. webcontrols. treenode createnode (string text, string navigateurl)
{
Microsoft. Web. UI. webcontrols. treenode node = new Microsoft. Web. UI. webcontrols. treenode ();
/*
* Add the other node attributes by yourself. Currently, only two node attributes are added.
Node. ID;
Node. imageurl;
Node. target;
Node. expanded;
Node. checkbox
**/
Node. Text = text;
Node. navigateurl = navigateurl;
Return node;
}
# Endregion
}
}
Database Design Method:
If exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [treetemp] ') and objectproperty (ID, n'isusertable') = 1)
Drop table [DBO]. [treetemp]
Go
Create Table [DBO]. [treetemp] (
[Childmenuid] [int] identity (1, 1) not null,
[Childmenu] [varchar] (50) Collate chinese_prc_ci_as null,
[Navigateurl] [varchar] (100) Collate chinese_prc_ci_as null,
[Parentmenu] [int] not null
) On [primary]
Go
Note: The primary key is "childmenuid" and the default value is 0.
Query Method:
Select * Form treetemp
Call the above class library.