Asp.net 2.0 Treeview controls "simple" operations

Source: Internet
Author: User
Tags connectionstrings

I have been learning ASP. net2.0 over the past few days. Although it is only a test version, I have heard that it has come out very early. It was just a month ago.

Shame. Her changes are too big, and there is a tree-like control Treeview, which needs to be downloaded and installed by herself in 1.0.

2.0 is improved. Because it is a control, static filling is very easy to use. It is OK to set some properties,

I had to write the code myself if I had to add the code dynamically. The method was similar to 1.0. I studied it for two days and finally got some results. Hey, it feels too good.

Dynamic ....
--------------------------------------------------------------------------------
The procedure is as follows:
1. Drag a Treeview control from the toolbox to set the following attributes:
Ontreenodepopulate = "node_populate" // call a method in the background
Id = "tree"
>


Value = "0"/>

2. Create a database to store node information. The database table is as follows:

3. dynamically read data. Implemented in the Aspx. CS File
Public void node_populate (Object sender, system. Web. UI. webcontrols. treenodeeventargs E)
{
If (E. node. childnodes. Count = 0) // checks whether the node is the first node.
{
Switch (E. node. Depth) // obtain the node depth
{
Case 0:
Fill_fathers (E. node); // trigger an event and pass the node that triggers the event!
Break;
Case 1:
Fill_childs (E. node );
Break;
}
}
}

Void fill_fathers (treenode node) // list the parent node
{// Create a database connection and cache the data to the dataset tree_father table
String connstring = configurationmanager. connectionstrings

["Englishconnectionstring"]. connectionstring;
Sqlconnection connection = new sqlconnection (connstring );
Sqlcommand command = new sqlcommand ("select father_name from tree_father where

Father_id is null ", connection );
Sqldataadapter adapter = new sqldataadapter (command );
Dataset tree_father = new dataset ();
Adapter. Fill (tree_father );
If (tree_father.tables.count> 0)
{
Foreach (datarow row in tree_father.tables [0]. Rows) // list parent nodes cyclically
{
Treenode newnode = new treenode (row ["father_name"]. tostring ());
// + "" +
// Row ["father_name"]. tostring (),
// Row ["father_id"]. tostring ());
Newnode. populateondemand = true;
Newnode. selectaction = treenodeselectaction. Expand;
Node. childnodes. Add (newnode );
}
}
}

Void fill_childs (treenode node)
{
String author = node. value. tostring ();
String connstring = configurationmanager. connectionstrings

["Englishconnectionstring"]. connectionstring;
Sqlconnection connection = new sqlconnection (connstring); // pay attention to the Data Type !!! Use

Like !!
Sqlcommand command = new sqlcommand ("select father_name, ID from tree_father where

Father_id like '"+ author +"' ", connection );
Sqldataadapter adapter = new sqldataadapter (command );
Dataset titlesforauthors = new dataset ();
Adapter. Fill (titlesforauthors );
If (titlesforauthors. Tables. Count> 0)
{
Foreach (datarow row in titlesforauthors. Tables [0]. Rows)
{
Treenode newnode = new treenode (row ["father_name"]. tostring ());
Newnode. populateondemand = true;
Newnode. navigateurl = ".../admin/switch. aspx? Classid = "+ row [" ID "]. tostring ();
// Newnode. Target = "mainfram ";
Newnode. selectaction = treenodeselectaction. Select;
Node. childnodes. Add (newnode );
}
}
}
The above example is successfully tested.

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.