Treeview displays the data table tree structure.

Source: Internet
Author: User
The specific method is to create a database and design the tree map information table s_menu, which contains the nodeid, parentid, nodename, address, and Icon fields. Other fields are determined based on the actual business, the node name nodename is displayed on the node of the tree control. The nodeid field stores the unique ID of the node. parentid indicates the parent node number of the current node. the ID number forms a "linked list ", the structure of the nodes on the tree is recorded. Design a web form, place the Treeview control on it, and modify its attribute 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] (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 RMB daily REPORT query public/a1.aspx icon_settings.gif
100002 100000 foreign currency daily REPORT query public/a2.aspx tips
100003 0 Branch Ministry of Science and Technology icon_document.gif
100004 RMB daily REPORT query tech/a1.aspx icon_settings.gif
100003 100005 foreign currency daily REPORT query tech/a2.aspx icon_settings.gif
100003 Futian Branch icon_document.gif
100007 month deposit schedule a1.aspx RMB
100006 month deposit trend chart a2.aspx RMB
100008 0 Luohu sub-branch icon_document.gif
100006 month deposit schedule a1.aspx RMB
deposit trend chart for 100011 month a2.aspx icon_settings.gif

the menu_left.aspx 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 "%>


menu_left










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>
/// Summary of 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 tree creation is to recursively call the display subtree from the root node.
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.