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 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);
}
}
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.