ASP. NET Treeview Problems

Source: Internet
Author: User

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 [2]. tostring (), T2, dryl [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's a routine for you. Let's look at treeviwe on my web page. There's no problem with 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 ,"###");

To tell the truth, I have never earned two hundred points at a time ......

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 to respond to Treeview control events?

Problem:
I write code in the selectedchange event. Every time I click a node, the interface (1) is refreshed and flashes
After the flash is complete, it is still at the first node and does not return to the corresponding node. The prompt value is also fixed at the first knot.
How to solve the problem.

Answer:
In the selectedchange event, you can find the parameter E, which contains the old and new nodes.
It is represented by a string. For example, if it is the first subnode under the first node, it is represented by 0.0 and converted
You can operate the event in the appropriate form, so that the automatic response event should be changed to "true". Otherwise, the event cannot be responded,
Although it is flickering, and every time it is back to the first point

9. Click a subnode of the Treeview To open a connection control target window. Is there a solution?

The node of Treeview has the navigateurl and target attributes.

10. The Treeview browsing in Microsoft. Web. UI. webcontrols does not display a tree structure.

Problem:
I downloaded and installed Microsoft. Web. UI. webcontrols in English, and added it to the nodes attribute of Treeview.
Some nodes are added, but the tree structure is not displayed during ie browsing, only some text. I read all about this on the forum.
Some people do not display the tree on the server, but some text. I do not display it on the local machine.
Tree, just some text. Thank you!

Answer:
Download package: Automatic Installation and manual Installation
You should install the package automatically!

11. 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 = textbox1.text)
{
String index1 = treeview1.nodes. getnodeindex (); // obtain the index
Treeview1.selectednodeindex = index1;
}

12. 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
 

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.