C # → use Treeview to implement the tree menu

Source: Internet
Author: User

The specific method is to create a database and design the tree chart information table.S_menu, IncludingNodeid,Parentid,Nodename,Address,IconField. Other fields are determined based on the actual business. The node nameNodenameIt will be displayed on the node of the tree control,NodeidThe unique ID of the node where the field is saved,ParentidIndicates the parent node number of the current node. the ID number forms a "linked list" and records the structure of the node on the tree. DesignWebPlace the form on itTreeviewControl, modify its propertiesIDIsTvmenu.

 

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] (50) Collate chinese_prc_ci_as null,
[Address] [nvarchar] (50) Collate chinese_prc_ci_as null,
[Icon] [nvarchar] (50) Collate chinese_prc_ci_as null
) On [primary]
Go

 

The database is as follows:
Nodeid parentid nodename address icon
100000 0 Public query Department Icon_document.gif
100001 100000 Daily RMB Query Public/a1.aspx icon_settings.gif
100002 100000 Foreign currency daily report Query Public/a2.aspx icon_settings.gif
100003 0 Branch Ministry of Science and Technology Icon_document.gif
100004 100003 Daily RMB Query Tech/a1.aspx icon_settings.gif
100005 100003 Foreign currency daily report Query Tech/a2.aspx icon_settings.gif
100006 0 Futian Branch Icon_document.gif
100007 100006 Monthly deposit schedule A1.aspx icon_settings.gif
100008 100006 Monthly deposit trend chart A2.aspx icon_settings.gif
100009 0 Luohu sub-branch Icon_document.gif
100010 100009 Monthly deposit schedule A1.aspx icon_settings.gif
100011 100009 Monthly deposit trend chart A2.aspx icon_settings.gif

Menu_left.aspx The file is as follows:
<% @ Register tagprefix = "iewc" namespace = "Microsoft. web. UI. webcontrols "assembly =" Microsoft. web. UI. webcontrols, 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>

 

CodebehindCodeAs 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>
/// 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 ");
}
// Create 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 building a tree is to recursively call the display subtree from the root node.
private void inittree (treenodecollection NDS, string parentid)
{< br> dataview DV = new dataview ();
treenode tmpnd;
string intid;
DV. table = Ds. tables ["Tree"];
DV. rowfilter = "parentid =" "+ parentid +";
foreach (datarowview DRV in DV)
{< br> tmpnd = new treenode ();
tmpnd. id = DRV ["nodeid"]. tostring ();
tmpnd. TEXT = DRV ["nodename"]. tostring ();
tmpnd. imageurl = ".. /webimages/"+ DRV [" icon "]. tostring ();
tmpnd. navigateurl = ".. /"+ DRV [" Address "]. tostring ();
NDS. add (tmpnd);
intid = DRV ["parentid"]. tostring ();
inittree (tmpnd. nodes, tmpnd. ID);
}< BR >}

 

# 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
}
}

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.