Use treeview to bind data

Source: Internet
Author: User
~ Today, the website is finally launched and in a good mood. It has been tested in the community for a long time at night.
Every day, we hope to have time to learn new technologies.
Today is the treeview control. I was confused before learning it. I thought it was so mysterious,
The principle is: assume a two-level treeview, you only need to bind the depth = 0 node for it, and then bind the depth = 1 node according to the id of the depth = 0 node,
The principle is very simple. It is only a level-1 binding.
I feel that I want to learn a good technology and want to know its functions and display first, so I should first give the code displayed on the page,

<Asp: TreeView ID = "TreeView1" EnableClientScript = "false" runat = "server">
<Nodes>
<Asp: TreeNode SelectAction = "Expand" Expanded = "false" PopulateOnDemand = "true" Text = "website development"> </asp: TreeNode>
</Nodes>
</Asp: TreeView>

The SelectAction attribute is Expand, indicating that the node selection event is expanded, and PopulateOnDemand = true indicates dynamic data binding.
The database is


First, write a database binding method. Private DataSet RunQuery (string command)
{
String strconn = ConfigurationManager. ConnectionStrings ["test"]. ConnectionString;
Using (SqlConnection conn = new SqlConnection (strconn ))
{
Using (SqlCommand cmd = new SqlCommand (command, conn ))
{
SqlDataAdapter da = new SqlDataAdapter (cmd );
DataSet ds = new DataSet ();
Da. Fill (ds );
Return ds;
}
}
}

Set it in webconfig and add using system. data. sqlclient.
Then bind the most advanced node in the legend Private void BindParent (TreeNode node)
{
DataSet ds = RunQuery ("select f_id, f_content from dbo. father ");
If (ds. Tables. Count> 0)
{
Foreach (DataRow row in ds. Tables [0]. Rows)
{
TreeNode newnode = new TreeNode (row ["f_content"]. ToString (), row ["f_id"]. ToString ());
Newnode. SelectAction = TreeNodeSelectAction. Expand;
Newnode. PopulateOnDemand = true;
Node. ChildNodes. Add (newnode );
}
}
}

Second, bind the next level Private void BindChild (TreeNode node)
{
DataSet ds = RunQuery ("select c_content, c_id from dbo. child where c_fid =" + node. Value );
If (ds. Tables. Count> 0)
{
Foreach (DataRow row in ds. Tables [0]. Rows)
{
TreeNode newnode = new TreeNode (row ["c_content"]. ToString (), row ["c_id"]. ToString ());
Newnode. SelectAction = TreeNodeSelectAction. Expand;
Newnode. PopulateOnDemand = true;
Node. ChildNodes. Add (newnode );
}
}
}

Bind the next-level data according to the ID of the previous-level node. This is also true for multiple layers.
Of course, another step is to bind data, that is TreeView1.TreeNodePopulate + = new TreeNodeEventHandler (treeviewappstreenodepopulate );

Protected void treeviewinclutreenodepopulate (object sender, TreeNodeEventArgs e)
{
Switch (e. Node. Depth)
{
Case 0:
BindParent (e. Node );
Break;
Case 1:
BindChild (e. Node );
Break;
}
}

It is actually quite simple to run and generate, right? The running result is as follows (I wrote three layers ),

OK, everything is done. A widget learns the basics and can be expanded slowly. If it is not successful, you can ask for the source code.

Please kindly advise !!!

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.