Special topics on Treeview controls

Source: Internet
Author: User

1.

(Source code is available for compilation)

Http://msdn.microsoft.com/archive/en-us/samples/internet/asp_dot_net_servercontrols/webcontrols/default.asp

MSI file:

Http://www.microsoft.com/china/msdn/archives/library/dnaspp/html/aspnet-usingtreeviewiewebcontrol.asp

After installation, add the Treeview to the Toolbox through "Custom toolbox"-> ". NET Framework component ".
2. Unable to display during running
This is generally the version of Treeview. It is best to download the English version of Automatic Installation and reinstall it. Before installation, add or delete it first.ProgramDetach the original version

3. Display format error (non-Tree display)
Treeview requires that the client browser version be ie5.5 or later. It is recommended that the client be upgraded to ie6.0

4. Use Treeview IN THE FRAMEWORK
Sets the navigateurl and target attributes to update other frames.

5. The treenode class cannot be found.
To use Treeview, it is best to add namespace: using Microsoft. Web. UI. webcontrols;

6. traverse the Treeview node (RecursionAlgorithm)
Private void page_load (Object sender, system. eventargs E)
{
Getallnodetext (treeview1.nodes );
}
Void getallnodetext (treenodecollection TNC)
{
Foreach (treenode node in TNC)
{
If (node. nodes. Count! = 0)
Getallnodetext (node. nodes );
Response. Write (node. Text + "");
}
}

7. Obtain the parent node of the node.
Treenode pnode;
If (node. parent is treenode)
Pnode = (treenode) node. parent;
Else
// Node is Root Node

8. Modify the Treeview style (example)
<Iewc: Treeview id = "treeview1" runat = "server" hoverstyle = "color: Blue; Background: #00 ffcc ;"

Defaultstyle = "Background: red; color: yellow;" selectedstyle = "color: red; Background: #00ff00;">
UseCode:
Treeview1.defaultstyle ["font-size"] = "20pt ";

9. Treeview unblinking Solution
Http://expert.csdn.net/Expert/topic/1300/1300576.xml

Client control Treeview
Http://expert.csdn.net/Expert/topic/1382/1382892.xml

1. Set the selected node. For example, select the second node.
Function setselnode ()
{
Treeview1.selectednodeindex = "1 ";
}

2. Get the text, ID, or nodedata of the selected node.
Function getattribute ()
{
Alert (treeview1.gettreenode (treeview1.selectednodeindex). getattribute ("text "));
}
Replace text with ID or nodedata to obtain the ID or nodedata of the selected node.

3. Modify node attributes, such as modifying the text of the first node
Function modifynode ()
{
VaR node = treeview1.gettreenode ("0 ");
Node. setattribute ("text", "hgknight ");
}

4. Click nodes.
Function treeview1.onclick ()
{
Alert (treeview1.gettreenode (treeview1.clickednodeindex). getattribute ("text "));
}

5. add nodes
Function addnode ()
{
VaR node = treeview1.createtreenode ();
Node. setattribute ("text", "hgknight ");
Treeview1.add (node );
}
6. js traverses all nodes
// Only for the root node. You can change it to recursive form.
VaR nodearray = new array ();
Nodearray = treeview1.getchildren ();
For (I = 0; I <nodearray. length; I ++)
{
CNode = nodearray [I];
Alert (cNode. getattribute ("text "));
}

1. Use code to add a node to the Treeview web control.

Table field: ID, parent number, name
Data:
1 0 People's Republic of China
2 1 Hunan
3 1 Hubei

Void creattree (string text, treenode TND, string PID)
{

Treenode t2 = new treenode ();
T2.text = text;

TND. nodes. Add (T2 );

Generate
String str1 = "parent number = '" + PID + "'";
Datarow [] dryl = dataset31.tables [0]. Select (str1 );

For (INT I = 0; I <= dryl. getupperbound (0); I ++)
{

Creattree (dryl [I] [2]. tostring (), T2, dryl [I] [0]. tostring ());
}
}

//
Treenode T1 = new treenode ();
T1.text = "region information ";
Treeview2.nodes. Add (T1 );

Creattree ("People's Republic of China", T1, "1 ");

2. How do I add events to a Treeview?

Private sub treeview1_indexchanged (byval sender as object, byval e
Microsoft. Web. UI. webcontrols. treeviewselecteventargs) handles
Treeview1.selectedindexchange
Dim A as integer
A = treeview1.selectednodeindex
Textbox1.text = CSTR ()
End sub
However, if autopostback is set to true, the Treeview needs to be refreshed and flashed. My Treeview is large.
What can I do ??? In distress...

3. Why does selectedindexchange in Treeview fail to be executed ???

If the autopostback attribute is true, selectedindexchange can be executed.
However, when you choose this method, you will feel refreshed to make you suffer. You can use the flexible
If possible, do not use the selectedindexchange event.

The selectedindexchange event is mounted to the control, no?
Check in initializecomponent Method

4. How can I determine whether a node under a Treeview has a subnode ???

If (selnode. nodes. Count = 0 ){
// The node does not have a byte
}

5. How can I get the current selected node of the Treeview ???

1. Tree. getnodefromindex (tree. selectednodeindex. tostring ())
2. Private void treeview1_selectedindexchange (Object sender,
Microsoft. Web. UI. webcontrols. treeviewselecteventargs E)
E. newnode is the selected node.
6. There is no problem in implementing two-layer nodes in Treeview. If it is extended to the infinite Layer, what will happen?

My practice is not complete enough. This can be done when the number of nodes is small, and the number of nodes is large.
You can't do this. You should load only the first level. When you click to expand the node, load its next level subsection.
Point.
The database structure is as follows:
Table Name: Table1
Parentid varchar 20: parent node
Id varchar 20: Node
Name varchar 50: Name

Come and come here. Here is a routine for you. Let's look at treeviwe on my web page. It's okay for 10 thousand subnodes.
Http://dev1.eraserver.net/czc888/webform1.aspx
As for the meaning of the three images, you can see my webpage. Indicates one page (no subnode), one
Closed book (with subnodes), an opened book (the current node is expanded)

Void creat_node (string
Name, Microsoft. Web. UI. webcontrols. treenode T1, string number)
{
// Define a new node

Microsoft. Web. UI. webcontrols. treenode t2 = new
Microsoft. Web. UI. webcontrols. treenode ();
T2.id = number;
T2.text = Name;

// Determine whether the node is the root node
Treeview1.expandedimageurl = "images/42403.bmp ";
If (t1.id. Trim () = "###")
{
Treeview1.nodes. Add (T2 );
T2.imageurl = "images/depart01.bmp ";
}
Else
{
T1.nodes. Add (T2 );
T2.imageurl = "images/depart01.bmp ";
T1.imageurl = "images/depart02.bmp ";
}

// Obtain all the child nodes of this node and generate them automatically using recursion
String myselectquery = "select parentid, ID, name from Table1
Where parentid = '"+ number + "'";
String myconnstring = "provider = Microsoft. Jet. oledb.4.0; Data
Source = "+ server. mappath (". ") +"/DT/mydata. mdb ";
Oledbconnection myconnection = new
Oledbconnection (myconnstring );
Oledbcommand mycommand = new
Oledbcommand (myselectquery, myconnection );
Myconnection. open ();
Oledbdatareader myreader;
Myreader = mycommand. executereader ();
While (myreader. Read ())
{
Creat_node (myreader. getstring (2), T2, myreader. getstring (1 ));

}
Myreader. Close ();
Myconnection. Close ();

}

Private void createprovidertreebtn_click (Object sender,
System. eventargs E)
{
Microsoft. Web. UI. webcontrols. treenode T1 = new
Microsoft. Web. UI. webcontrols. treenode ();
Creat_node ("", T1 ,"###");

7. How to Use Treeview ???

// Initialize the Treeview data
Private void inittv (INT intvorderlen)
{
Sqldatareader Dr;
Sqlcommand mycommand;
String strsel = "select * From bas_enterprise where
Len (vieworder) = "+ intvorderlen +" order by vieworder ";
Mycommand = new sqlcommand (strsel, myconnection );
Dr = mycommand. executereader ();
While (dr. Read ())
{
Treenode pnode = new treenode ();
Pnode. Text = Dr ["entpname"]. tostring ();
Pnode. ID = Dr ["vieworder"]. tostring ();
Treeview1.nodes. Add (pnode );

}
Dr. Close ();

}

Private void treeview1_selectedindexchange (Object sender,
Microsoft. Web. UI. webcontrols. treeviewselecteventargs E)
{
Treenode newnode = treeview1.getnodefromindex
(Treeview1.selectednodeindex );
String strnodeid = newnode. Id. tostring ();
Sqldatareader Dr;
Sqlcommand mycommand;
Int intvorderlen = strnodeid. Length + 2;
String strsel = "select * From bas_enterprise where
Len (vieworder) = "+ intvorderlen +" and vieworder like '% "+ strnodeid +" __%'
Order by vieworder ";
Mycommand = new sqlcommand (strsel, myconnection );
Dr = mycommand. executereader ();
Newnode. nodes. Clear ();
While (dr. Read ())
{
Treenode pnode = new treenode ();
Pnode. Text = Dr ["entpname"]. tostring ();
Pnode. ID = Dr ["vieworder"]. tostring ();
Newnode. nodes. Add (pnode );
}
Dr. Close ();
Treeview1.expandlevel = 1;
}

8. How can I find a value in Treeview and select it?

Problem:
Ladies and gentlemen, how do I find a text or treenodes ID in the created tree and select it?

Answer:
For (INT I = 0; I <treeview1.nodes. Count; I ++)
If (treeview1.nodes [I] = textbox1.text)
{
String index1 = treeview1.nodes [I]. getnodeindex (); // obtain the index
Treeview1.selectednodeindex = index1;
}

9. How do I obtain the values of each item in the Treeview ??

Private void treeview1_selectedindexchange (Object sender,
Microsoft. Web. UI. webcontrols. treeviewselecteventargs E)
{
Microsoft. Web. UI. webcontrols. treenode
Newnode = treeview1.getnodefromindex (treeview1.selectednodeindex );
Label1.text = newno

If you want to add a child node to the parent node through code, how can the code be implemented?
The code of the parent node is as follows:
Treenode pnode = new treenode ();
Pnode. ID = "root ";
Pnode. Text = strnodename;
Treeview1.nodes. Add (pnode );
If you want to add a child node under the root node "root", how do you write code?
Treeview1.nodes. addat (pnode); this method does not seem to work.
Treeview1.nodes [0]. nodes. Add (pnewnode );

/*************. Code ***************************** in CS ****************************

string strscript = " ";
If (! This. isclientscriptblockregistered ("myjs")
{< br> This. registerclientscriptblock ("myjs", strscript);
}

**************************************** ***********//

Now I want to click the checkbox of the parent node on the client, and the lower-level is also selected, but the following code can only be selected on the surface, but not on the server side. What other good methods can I do?
<Script language = \ "javascript \">
Function getnode ()
{
VaR choose;
VaR indexid;
Indexid = treeview1.clickednodeindex;
If (treeview1.gettreenode (indexid). getattribute ('checked '))
{
Choose = false;
}
Else
{
Choose = true;
}
Treeview1.gettreenode (indexid). setattribute ('checked', choose );
VaR nodearray = new array ();
Nodearray = treeview1.gettreenode (indexid). getchildren ();
For (I = 0; I <nodearray. length; I ++)
{
VaR tempindex = indexid + "." + I;
Treeview1.gettreenode (indexid). setattribute ('checked', choose );
}
}
// -->
</SCRIPT>

XML as data source
http://renyu732.cnblogs.com/archive/2005/06/28/182553.html
SQL Server as data source
winform
http://www.microsoft.com/china/community/Column/21.mspx
webform
http://www.microsoft.com/china/community/Column/30.mspx

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.