Binding Ajax Treeview to a database (1)

Source: Internet
Author: User
Tags connectionstrings

Treeview Control
The Treeview control displays the Hierarchy Structure of the Node object. Each Node object contains a label and an optional bitmap. The Treeview control is usually used to display document headers, entries in indexes, files and directories on disks, or various other information that can be displayed as hierarchical structures.

After creating the Treeview control, you can set the attributes of the Node object and call its method to add, delete, or manipulate the Node object. You can expand or contract nodes programmatically to show or hide all child nodes. The events collapse, expand, and nodeclick provideProgram.

You can use the root, parent, child, firstsibling, next, previous, and lastsibling attributes to check the references of the Node object.Code. At the root of the tree, select to jump to the tree header, and scroll the window if necessary.

The Treeview control has several options. A Node object can be expressed as one of the eight combinations of text, bitmap, line, and minus signs.

The Treeview control uses the imagelist control specified by the imagelist attribute to save the bitmap and icon used in the Node object. One Treeview control can only use one imagelist at a time. This means that when the style attribute of the Treeview control is set to display the image style, each member in the Treeview control displays an image of an equal size.
How to bind a Treeview to a database. The Code is as follows:
// Obtain the root node
Public void bindtree (INT tableid) // obtain the ID of a table from the database ,{
Dataset DS = new dataset ();
Using (sqlconnection conn = new sqlconnection (configurationmanager. connectionstrings ["treeviewconnectionstring"]. connectionstring) // connect to the database
{
Sqlcommand COM = new sqlcommand ("select * from table where tableid =" + tableid, Conn );
Sqldataadapter da = new sqldataadapter (COM );
Da. Fill (DS );
}
For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
{
Treenode node = new treenode ();
Node. populateondemand = false;
Node. Text = Ds. Tables [0]. Rows [I] ["tablename"]. tostring ();
Node. value = Ds. Tables [0]. Rows [I] ["ID"]. tostring ();
Node. Target = "frmright ";
Node. navigateurl = Ds. Tables [0]. Rows [I] ["tableurl"]. tostring ();
Treeview1.nodes. Add (node );


Node. Expanded = false;
Node. selectaction = treenodeselectaction. Expand;
Bindchildnodes (node); // call the subnode Method
}
}

// Obtain the subnode
Public void bindchildnodes (treenode rootnode ){
Dataset DS = new dataset ();
Using (sqlconnection conn = new sqlconnection (configurationmanager. connectionstrings ["treeviewconnectionstring"]. connectionstring ))
{
Sqlcommand COM = new sqlcommand ("select * from table where tableid =" + rootnode. Value, Conn );
Sqldataadapter da = new sqldataadapter (COM );
Da. Fill (DS );
}
For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
{
Treenode node = new treenode ();
Node. Text = Ds. Tables [0]. Rows [I] ["tablename"]. tostring ();
Node. value = Ds. Tables [0]. Rows [I] ["ID"]. tostring ();
Node. Target = "frmright ";
Node. navigateurl = Ds. Tables [0]. Rows [I] ["tableurl"]. tostring ();

Rootnode. childnodes. Add (node );
Bindchildnodes (node );
}

}

Finally, the method for obtaining the root node is called during page loading: bindtree (0 ).

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.