Use the TreeView control in the ASP.NET page

xiaoxiao2021-03-06  15

First, download the source code

http://www.asp.net/iewebcontrols/iewebControls.exe

Second, installation and compilation

1. After performing the installation file, find the "build.bat" file in the installation directory, and open it with Notepad. Change "CSC.exe" to the absolute path "C: /Winnt/Microsoft.Net/framework/v1.1.4322/csc.exe". After saving (remember to remove the "read-only" property).

2. After the compilation is completed, you can see the "build" binder on the installation directory, copy all the files in the "runtime" subdirectory in this directory to "c: / inetpub / wwwroot / webctrl_client / 1_0" (if not It is created yourself. Note that in the new site in IIS, you must copy the "WebCTRL_CLIENT" directory to the site root directory)

Third, use in vs.net

1. Open vs.net → Right-click "Toolbox" → Add / Remove Project → Browse → Select the above-described file "Microsoft.WebControls.dll" → After confirmation, you can see the added "TreeView" control

2, drag the "TreeView" control into the page → Properties → Node (Node) → Add a root node (or add a child node)

Fourth, read node information from the database

The Table basic structure of the saving node information is

1, all read and demonstrate

Private Void Page_Load (Object Sender, System.EventArgs E)

{

IF (ISPostback)

{

Return;

}

Dbsystem.executesql tmp = new dbsystem.executesql ("t");

DataTable DT = Tmp.GetDataTable ("Select * from treeView"); // Caught data from the database

This.AddNodes (DT, NULL, "0"); // Add node

this.treeView1.selectedNodeIndex = ""; // No node is not selected.

/ * The next section describes how to modify the TreeView.cs source code to cancel the method of default the node * /

}

///

/// Add a node and the child's node

///

/// DataTable captured from the database

/// The parent of the child will be added

/// The ID of the parent node, "0" is root node

Private Void AddNodes (DataTable DT, Treenode Node, String ID)

{

DataRow [] rows = dt.select ("ParentId =" ID); // Filter sub-node collection belonging to the parent node "Node"

Foreach (DataRow Dr In Rows) // Cycling subtitle collection

{

Treenode nd = new treenode ();

nd.navigateURL = DR ["URL"]. TOSTRING (); // Set the browse URL

nd.nodedata = DR ["nodeid"]. TOSTRING (); // Storage node idnd.text = DR ["nodename"]. TOSTRING (); // Setting the node name

THIS.ADDNODES (DT, ND, ND.NODEDATA); // Regeneration, add the child node of this node

IF (node ​​== null || ID == "0")

{

this.treeView1.nodes.add (nd); // Add to root node

}

Else

{

Node.Nodes.Add (ND); // Add sub-node

}

}

2. Read the data of the node when expanding the node

TreeView Control Properties → AutoStBack → True

TreeView Control Properties → Event → Double-click Expand (Expand)

Private Void Page_Load (Object Sender, System.EventArgs E)

{

IF (! ispostback)

{

This.Addsinglenode (NULL, "0"); // to find root node

}

this.treeView1.SelectedNodeIndex = ""; // Do not select any node

}

///

/// When expanding the node, the event triggered

///

/// TreeView control

///

Private void TreeView1_expand (object sender, microsoft.web.ui.webcontrols.treeviewclickeventargs e)

{

Treenode Nd = this.treeView1.GetNodeFromindex (E.NODE); / / Find the node of the trigger event, that is, the node being exposed

IF (nd.nodes [0] .nodedata == "") // The first load, that is, "being loaded ..."

{

This.Addsinglenode (Nd, nd.nodedata); // Add subtosis

}

}

///

// only look for, add child nodes under a single node

///

/// TREENODE you need to add a child node>

/// NodeId of this node

Private Void AddSinglenode (Treenode Node, String ID)

{

IF (node! = null && id! = "0")

{

Node.nodes.clear (); // Empty the node, mainly removing "loading ..."

}

Dbsystem.executesql tmp = new dbsystem.executesql ("t");

DataTable DT = Tmp.GetDataTable ("Select * from treeView where parentid =" id); // grab data from the database

Foreach (DATAROW DR IN DT.ROWS) // Caver all child node {

Treenode nd = new treenode ();

nd.navigateURL = DR ["URL"]. TOSTRING (); // Set the browse URL

nd.nodedata = DR ["NodeID"]. TOSTRING (); // Storage Node ID

nd.text = DR ["nodename"]. TOSTRING (); // Set the node name

Treenode Child = new Treenode (); // This node is only the basis for "ND", and there is no intention

Child.Text = "Loading ...";

nd.nodes.add (child);

IF (node ​​== null || ID == "0")

{

this.treeView1.nodes.add (nd); // Add to root node

}

Else

{

Node.Nodes.Add (ND); // Add sub-node

}

}

}

5. Modify the "TreeView.cs" source, cancel the TreeView default selection node

1, TreeView.cs file

In the article, find "Protected Override Void Onload" event

"Protected Override Void OnPrender (EventArgs E)" event, modify the following code

IF ((SELECTEDNODEX == "|| SELECTEDNODEINDEX == String.empty) && nos.count> 0)

// SELECTEDNODEINDEX = "0"; // before modify

SELECTEDNODEINDEX = ""; // After the modification

2, TreeView.htc file

Find "Function OnDocumentReady () in" c: / inetpub / wwwroot / webctrl_client / 1_0 / treeview.htc "comment out the following code

// Verify SelectedNodeIndex

/ ***************** Change by Jerry on 2005-01-25 ***************** /

/ *

IF (getNodeFromindex) == null) {if (GetNodeFromindex ("0")! = null) SelectedNodeIndex = "0"; else selectedNodeIndex = ""

* /

/ ***************** Change by Jerry on 2005-01-25 ***************** /

Modify the following code in "Function TrytobuildTreeFromROOT ()"

IF (SELECTEDNODEINDEX.LENGTH == 0)

{

// Prop_SelectedNodeIndex = "0"; // before modify

Prop_selectedNodeIndex = ""; //

}

3, use

After modifying "TreeView.cs", recompile once, generate new "Microsoft.Web.ui.WebControls.dll". I only need to set "SELECTEDNodeIndex" "" "," "", "" "," 即 = = ""; // is not selected any node.

6. Create TreeView using XML documentation

1, XML documentation requirements

a) Describe the node name is fixed " " and " "

b) Although the XML text file strictly distinguishes on the case of the letter, it is ignored when binding

c) " " node in the node must correspond to the TREENODE attribute in the TreeView control

d) " " Node cannot be written within the node, can only appear in the "text" attribute, such as " Node01 " Do not pass, should be "

"

The following is an example of a "TreeView" control binding source:

2, binding code

THIS.TREEVIEW1.TREENODESRC = "treexml.xml";

THIS.TREEVIEW1.DATABIND ();

转载请注明原文地址:https://www.9cbs.com/read-44858.html

New Post(0)