ASP. NET Recursive Algorithms generate N-level (cascade) tree Treeview

Source: Internet
Author: User

 

HtmlCode:

View plaincopy to clipboardprint?
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: Treeview id = "treeview1" runat = "server" Height = "257px" imageset = "windowshelp"
Width = "142px">
<Parentnodestyle font-bold = "false"/>
<Hovernodestyle font-underline = "true" forecolor = "# 6666aa"/>
<Selectednodestyle backcolor = "# b5b5b5" font-underline = "false" horizontalpadding = "0px"
Verticalpadding = "0px"/>
<Nodestyle font-names = "tahoma" font-size = "8pt" forecolor = "black" horizontalpadding = "5px"
Nodespacing = "0px" verticalpadding = "1px"/>
</ASP: Treeview>


</Div>
</Form>
</Body>
</Html>
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: Treeview id = "treeview1" runat = "server" Height = "257px" imageset = "windowshelp"
Width = "142px">
<Parentnodestyle font-bold = "false"/>
<Hovernodestyle font-underline = "true" forecolor = "# 6666aa"/>
<Selectednodestyle backcolor = "# b5b5b5" font-underline = "false" horizontalpadding = "0px"
Verticalpadding = "0px"/>
<Nodestyle font-names = "tahoma" font-size = "8pt" forecolor = "black" horizontalpadding = "5px"
Nodespacing = "0px" verticalpadding = "1px"/>
</ASP: Treeview>


</Div>
</Form>
</Body>
</Html>

Background code:

View plaincopy to clipboardprint?
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

Public partial class _ default: system. Web. UI. Page
{
Dataview DV;
Datatable DT;

Protected void page_load (Object sender, eventargs E)
{

If (! Page. ispostback)
{
// The database will not write the query method.
Operator op = new operator ();
// Select * From tabmenu
Dt = op. Query ();
// Call the method to pass Parameters during the first loading
Createtree (0, null, DT, this. treeview1 );
This. treeview1.expandall ();
}
}

/// <Summary>
/// Create a tree
/// </Summary>
/// <Param name = "parentid"> parent id </param>
/// <Param name = "Node"> node </param>
/// <Param name = "DT"> datatable </param>
/// <Param name = "Treeview"> name of the Treeview </param>
Public void createtree (INT parentid, treenode node, datatable DT, Treeview)
{
// Instantiate a dataview dt = input datatable
DV = new dataview (DT );
// Filter (equivalent to select * From tabmenu where menuparentid = input parentid)
DV. rowfilter = "[menuparentid] =" + parentid;

// Use foreach to traverse dv
Foreach (datarowview row in DV)
{
// Empty during the first loading
If (node = NULL)
{
// Create the root node
Treenode root = new treenode ();
// It must correspond to the database
Root. Text = row ["menuname"]. tostring ();
Root. value = row ["menuid"]. tostring ();
// Add the root node
This. treeview1.nodes. Add (Root );
// Recursively call the method itself
Createtree (Int. parse (row ["menuid"]. tostring (), root, DT, Treeview );

}
Else
{
// Add a subnode
Treenode childnode = new treenode ();
Childnode. Text = row ["menuname"]. tostring ();
Childnode. value = row ["menuid"]. tostring ();
Node. childnodes. Add (childnode );
Createtree (Int. parse (row ["menuid"]. tostring (), childnode, DT, Treeview );

}
}

}
}
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

Public partial class _ default: system. Web. UI. Page
{
Dataview DV;
Datatable DT;

Protected void page_load (Object sender, eventargs E)
{

If (! Page. ispostback)
{< br> // The database query method is left empty.
operator op = new operator ();
// select * From tabmenu
dt = op. query ();
// when loading for the first time, call the method to pass the parameter
createtree (0, null, DT, this. treeview1);
This. treeview1.expandall ();
}< BR >}

/// <Summary>
/// Create a tree
/// </Summary>
/// <Param name = "parentid"> parent id </param>
/// <Param name = "Node"> node </param>
/// <Param name = "DT"> datatable </param>
/// <Param name = "Treeview"> name of the Treeview </param>
Public void createtree (INT parentid, treenode node, datatable DT, Treeview)
{
// Instantiate a dataview dt = input datatable
DV = new dataview (DT );
// Filter (equivalent to select * From tabmenu where menuparentid = input parentid)
DV. rowfilter = "[menuparentid] =" + parentid;

// Use foreach to traverse dv
Foreach (datarowview row in DV)
{
// Empty during the first loading
If (node = NULL)
{
// Create the root node
Treenode root = new treenode ();
// It must correspond to the database
Root. Text = row ["menuname"]. tostring ();
Root. value = row ["menuid"]. tostring ();
// Add the root node
This. treeview1.nodes. Add (Root );
// Recursively call the method itself
Createtree (Int. parse (row ["menuid"]. tostring (), root, DT, Treeview );

}
Else
{
// Add a subnode
Treenode childnode = new treenode ();
Childnode. Text = row ["menuname"]. tostring ();
Childnode. value = row ["menuid"]. tostring ();
Node. childnodes. Add (childnode );
Createtree (Int. parse (row ["menuid"]. tostring (), childnode, DT, Treeview );

}
}

}
}
 

Database code:

View plaincopy to clipboardprint?
Create Table tabmenu
(
Menuid int identity () primary key, -- automatic number
Menuparentid int not null, -- number of the upper menu
Menuname varchar (20) not null, -- menu name
Menuurl varchar (20) not null, -- menu address
Menuimage varchar (20) not null, -- image address
Menutarget varchar (20) not null, -- Open Mode
)
Go
-- 0 indicates the root node.
Insert into tabmenu
Values (0, 'xx backend Manager ','','','')

-- 1 subnode
Insert into tabmenu
Values (1, 'student management ','','','')

Insert into tabmenu
Values (1, 'class management ','','','')
-- Level 2 node
Insert into tabmenu
Values (2, 's1 ','','','')

Insert into tabmenu
Values (2, 's2 ','','','')

Insert into tabmenu
Values (2, 's3 ','','','')
-- Level 3 Node

Insert into tabmenu
Values (3, 's1 ','','','')

Insert into tabmenu
Values (3, 's2 ','','','')

Insert into tabmenu
Values (3, 's3 ','','','')

Insert into tabmenu
Values (4, 'ccc ','','','')

Insert into tabmenu
Values (5, 'bbb ,'','','')

Insert into tabmenu
Values (6, 'fff ','','','')
--
Insert into tabmenu
Values (7, 'stu1 ','','','')

Insert into tabmenu
Values (8, 'stu2 ','','','')

Insert into tabmenu
Values (9, 'stu1 ','','','')
Select * From tabmenu
Go

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.