web| Program | Design my last article "tree structure in the development of the application" is mainly under the Windows Form implementation, below the Web Form implementation.
Overview
TreeView is an important control, whether in Vb.net,c# or VB, Delphi and other languages, have acted as a navigator role. In practice, you will need to connect the TreeView to the database in many cases to populate its nodes. In Windows Form and Web form, we can use the TreeView to display the tree structure, such as displaying a tree, displaying a region, classifying items, and so on. It can be said that in most software development, the TreeView is an indispensable display control. Therefore, the design of tree structure has become an eternal topic for software developers.
How to display the tree-shaped structure
The display of a tree structure generally has three ways:
1. Interface design to populate the TreeView control directly in the TreeView designer or code.
2. Create a tree structure from the XML file.
3. The data from the database, the establishment of a tree-shaped structure.
The first is the simplest one, which is used primarily for applications with no change in the tree structure, and a tree is fixed at design time. Of course, at design time fixed the structure of the tree, in the future to modify, add, delete the tree node, you must modify the source program. All are not conducive to expansion.
The second way to extract from the XML file, because XML itself is a tree structure, Microsoft provides the Document Object model DOM can easily read, manipulate and modify XML documents. In. NET, applying the System.Xml class makes it easy to load XML files into the TreeView control, and Microsoft MSDN also provides an example, which is no longer mentioned here.
The third Way, tree-structured data is obtained from the database. Generally speaking, most of our applications are database based. In this way, add, modify, delete a tree node is very convenient, as long as the operation of the data in the database can be. Moreover, this approach can be associated with other tables in the database, query and summary, through the design of views or stored procedures, it is easy to query the relevant data you want. Below, we mainly discuss the design and implementation of this approach.
Database design
First, we create a table tbtree in SQL SERVER 2000, which is structured as follows:
Column name data type description length primary key
Idint Node Number 4 is
Parentidint Parent Node Number 4
Contextnvarchar we want to display the node content 50
Download Address
Http://msdn.microsoft.com/downloads/samples/internet/ASP_DOT_NET_ServerControls/WebControls/default.asp
After installation, add the TreeView to the Toolbox through the "Custom Toolbox"-> ". NET Framework Components".
Create a new project, select the Visual Basic.NET engineering asp.net Web application, and drag a TreeView control over the page.
HTML page:
<%@ Register tagprefix= "IEWC" namespace= "Microsoft.Web.UI.WebControls" assembly=, version=1.0.2.226, Culture=neutral, publickeytoken=31bf3856ad364e35 "%>
<%@ Page language= "vb" autoeventwireup= "false" codebehind= "WebForm1.aspx.vb" inherits= "Tree.webform1"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name= "generator" content= "Microsoft Visual Studio. NET 7.0" >
<meta name= "Code_language" content= "Visual Basic 7.0" >
<meta name= "vs_defaultClientScript" content= "JavaScript" >
<meta name= "vs_targetschema" content= "http://schemas.microsoft.com/intellisense/ie5" >
</HEAD>
<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
<font face= "Song Body" >
<iewc:treeview id= "TreeView1" style= "Z-INDEX:101; left:39px; top:68px "runat=" Server ></iewc:TreeView></FONT>
</form>
</body>
</HTML>
Background code:
Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
Dim DS as New DataSet ()
Dim CN as New SqlConnection ()
Try
' Initialize connection string
CN. ConnectionString =
"Data source=pmserver;initial catalog=benchmark;persist security Info=false;user Id=sa; Password=sa; "
CN. Open ()
Dim ADP as SqlDataAdapter = New SqlDataAdapter ("select * from Tbtree", CN)
Adp. Fill (DS)
Me.viewstate ("ds") = ds
Catch ex as Exception
#If DEBUG Then
Session ("Error") = ex. ToString ()
Response.Redirect ("error.aspx") '? Jump program's Public error handling page
#End If
Finally
' Close the connection
CN. Close ()
End Try
' Call recursive function to complete the generation of tree-shaped structure
Addtree (0, nothing)
End Sub
' Recursive node to add tree
Private Sub Addtree (ByVal parentid as Integer, ByVal Pnode as TreeNode)
Dim DS as DataSet
ds = Me.viewstate ("ds")
Dim Dvtree as New DataView ()
Dvtree = New DataView (ds. Tables (0))
' Filter ParentID to get all the current child nodes
Dvtree.rowfilter = "ParentID =" + parentid.tostring
Dim Row as DataRowView
For each Row in Dvtree
Dim Node as New TreeNode ()
If Pnode is nothing Then ' determines whether the root node
' Add root node
Node.text = Row ("context"). ToString ()
TREEVIEW1.NODES.ADD (Node)
node.expanded = True
' Again recursive
Addtree (Int32.Parse (Row ("ID"). ToString ()), Node)
Else
'? Add child nodes of the current node
Node.text = Row ("context"). ToString ()
PNODE.NODES.ADD (Node)
node.expanded = True
' Again recursive
Addtree (Int32.Parse (Row ("ID"). ToString ()), Node)
End If
Next
End Sub
C # version:
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 Treecs
{
///
Summary description of WebForm1
///
public class WebForm1:System.Web.UI.Page
{
protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
private void Page_Load (object sender, System.EventArgs e)
{
Defining a database connection
SqlConnection CN = new SqlConnection ();
Try
{
Initializing the connection string
CN. connectionstring=
"Data source=pmserver;initial catalog=benchmark;persist security Info=false;user Id=sa; Password=sa; ";
CN. Open ();
SqlDataAdapter ADP = new SqlDataAdapter ("select * from Tbtree", CN);
DataSet ds=new DataSet ();
Adp. Fill (DS);
This. viewstate["DS"]=DS;
}
catch (Exception ex)
{
session["Error"] = ex. ToString ();
Response.Redirect ("error.aspx"); The public error handling page of the Jump program
}
Finally
{
CN. Close ();
}
Call recursive function to complete the generation of tree-shaped structure
Addtree (0, (TreeNode) null);
}
A node that recursively adds a tree
public void Addtree (int parentid,treenode pnode)
{
DataSet ds= (DataSet) this. Viewstate["DS"];
DataView dvtree = new DataView (ds. Tables[0]);
Filter ParentID to get all the current child nodes
Dvtree.rowfilter = "[ParentID] =" + ParentID;
foreach (DataRowView Row in Dvtree)
{
TreeNode node=new TreeNode ();
if (Pnode = null)
{//Add root node
Node.text = row["Context"]. ToString ();
TREEVIEW1.NODES.ADD (Node);
Node.expanded=true;
Addtree (Int32.Parse (row["ID"). ToString ()), Node); Recursion again
}
Else
{//? Add child nodes of the current node
Node.text = row["Context"]. ToString ();
PNODE.NODES.ADD (Node);
Node.expanded = true;
Addtree (Int32.Parse (row["ID"). ToString ()), Node); Recursion again
}
}
}
#region Web Form Designer generated code
Override protected void OnInit (EventArgs e)
{
//
CodeGen This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method
</summary>
private void InitializeComponent ()
{
This. Load + = new System.EventHandler (this. Page_Load);
}
#endregion
}
}
PostScript: Please the reader to modify the connection string settings in the program.
Attached: Related Microsoft MSDN documentation, including building a tree structure from XML in VB6 and. Net
http://support.microsoft.com/default.aspx?kbid=311318
http://support.microsoft.com/default.aspx?kbid=308063
http://support.microsoft.com/default.aspx?kbid=317597
http://support.microsoft.com/default.aspx?kbid=244954
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