Implement tree menu with TreeView

Source: Internet
Author: User
Tags implement visual studio
Treeview| Menu Specific method is: Create a database, design tree map information table S_menu, including Nodeid, ParentID, nodename, Address, icon fields, other fields according to the actual business, The node name nodename is displayed on the node of the tree control, the Nodeid field holds the unique identification number of the node, ParentID represents the parent node number of the current node, and the identification number consists of a "linked list" that records the structure of the nodes on the tree. Design a Web Form to place the TreeView control on it, modifying its property ID to Tvmenu.



The data structure is as follows:
CREATE TABLE [dbo]. [S_menu] (
[NodeId] [Char] (6) COLLATE chinese_prc_ci_as NULL,
[ParentID] [Char] (6) COLLATE chinese_prc_ci_as NULL,
[NodeName] [nvarchar] (m) COLLATE chinese_prc_ci_as NULL,
[Address] [nvarchar] (m) COLLATE chinese_prc_ci_as NULL,
[Icon] [nvarchar] (m) COLLATE chinese_prc_ci_as NULL
) on [PRIMARY]
Go



The database is as follows:
NodeId parentid nodename Address Icon
100000 0 Public Enquiry Department icon_document.gif
100001 100000 RMB daily inquiry public/a1.aspx Icon_settings.gif
100002 100000 foreign currency daily enquiries public/a2.aspx icon_settings.gif
100003 0 Branch Science and technology Department Icon_document.gif
100004 100003 RMB daily inquiry tech/a1.aspx Icon_settings.gif
100005 100003 Foreign currency daily enquiries tech/a2.aspx icon_settings.gif
100006 0 Futian Branch Icon_document.gif
100007 100006-month deposit schedule a1.aspx Icon_settings.gif
100008 100006-month deposit trend chart A2.aspx icon_settings.gif
100009 0 Lo Wu Sub-branch Icon_document.gif
100010 100009-month deposit schedule a1.aspx Icon_settings.gif
100011 100009-month deposit trend chart A2.aspx icon_settings.gif

menu_left.aspx files are as follows:
<%@ Register tagprefix= "IEWC" namespace= "Microsoft.Web.UI.WebControls" assembly=, version=1.0.2.226, Culture=neutral, publickeytoken=31bf3856ad364e35 "%>
<%@ Page language= "C #" codebehind= "Menu_Left.aspx.cs" autoeventwireup= "false" inherits= "Hzquery.menu.menu_Left"% >
<HTML>
<HEAD>
<title>menu_Left</title>
<meta name= "generator" content= "Microsoft Visual Studio 7.0" >
<meta name= "Code_language" content= "C #" >
<meta name= "vs_defaultClientScript" content= "JavaScript" >
<meta name= "vs_targetschema" content= "http://schemas.microsoft.com/intellisense/ie5" >
</HEAD>
<body ms_positioning= "GridLayout" >
<form id= "Menu_left" method= "POST" runat= "Server" >
<iewc:treeview id= "Tvmenu" runat= "Server" ></iewc:TreeView>
</form>
</body>
</HTML>



The codebehind code is as follows:
Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Using Microsoft.Web.UI.WebControls;
Using System.Data.SqlClient;



Namespace Hzquery.menu
{
<summary>
Summary description of the menu_left.
</summary>
public class Menu_Left:System.Web.UI.Page
{
protected Microsoft.Web.UI.WebControls.TreeView Tvmenu;
SqlConnection Conn;
SqlDataAdapter myCMD;
DataSet ds;
String Cmdselect;



private void Page_Load (object sender, System.EventArgs e)
{
Conn=new SqlConnection (application["ConnString"). ToString ());
CreateDataSet ();
Inittree (Tvmenu.nodes, "0");
}
Set up a dataset
Private DataSet CreateDataSet ()
{
Cmdselect= "SELECT * from S_menu";
Mycmd=new SqlDataAdapter (Cmdselect,conn);
Ds=new DataSet ();
Mycmd.fill (ds, "tree");
return DS;
}
The basic idea of achievement is: To start a recursive call from the root node to display the subtree
private void Inittree (TreeNodeCollection nds,string parentid)
{
DataView dv=new DataView ();
TreeNode TMPND;
String intId;
Dv. Table=ds. Tables["Tree"];
Dv. Rowfilter= "parentid=" "+ ParentID +" "";
foreach (DataRowView DRV in DV)
{
Tmpnd=new TreeNode ();
tmpnd.id=drv["NodeId"]. ToString ();
tmpnd.text=drv["NodeName"]. ToString ();
Tmpnd.imageurl= ". /images/"+drv[" "Icon"]. ToString ();
Tmpnd.navigateurl= ". /"+drv[" address "]. ToString ();
Nds.add (TMPND);
intid=drv["ParentID"]. ToString ();
Inittree (tmpnd.nodes,tmpnd.id);
}
}



#region Web Form Designer generated code
Override protected void OnInit (EventArgs e)
{
InitializeComponent ();
Base. OnInit (e);
}
private void InitializeComponent ()
{
This. Load + = new System.EventHandler (this. Page_Load);



}
#endregion
}
}

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.