Dynamically increasing nodes in the TreeView of ASP.net 2.0

Source: Internet
Author: User
Tags foreach count tostring connectionstrings
asp.net|treeview| dynamic in asp.net 2.0, to dynamically extract content from the database, dynamically add nodes, it is not difficult, for example, in the pubs database of SQL SERVER 2000 as an example, to the tree-type list, to take out the author, as the root node, And then take out what each author has written, as a child node, you can

<%@ Page language= "C #"%>
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.SqlClient"%>
<%@ Import namespace= "System.Configuration"%>

<! DOCTYPE htmlpublic "-//w3c//dtd XHTML 1.1//en" "Http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" >

<title>dynamic Population of the TreeView control</title>
<script runat=server>
void Node_populate (object sender,
System.Web.UI.WebControls.TreeNodeEventArgs e)
{
if (e.node.childnodes.count = 0)
{
Switch (e.node.depth)
{
Case 0:
Fillauthors (E.node);
Break
Case 1:
Filltitlesforauthors (E.node);
Break
}
}
}

void Fillauthors (TreeNode node)
{
String connstring = System.Configuration.ConfigurationSettings.
connectionstrings["Northwindconnnection"]. ConnectionString;
SqlConnection connection = new SqlConnection (connstring);
SqlCommand command = new SqlCommand ("SELECT * from"
Authors ", connection);
SqlDataAdapter adapter = new SqlDataAdapter (command);
DataSet authors = new DataSet ();
Adapter. Fill (authors);
if (authors. Tables.count > 0)
{
foreach (DataRow row in authors. Tables[0]. Rows)
{
TreeNode NewNode = new
TreeNode (row["au_fname"). ToString () + "" +
row["au_lname"]. ToString (),
row["au_id"]. ToString ());
Newnode.populateondemand = true;
Newnode.selectaction = Treenodeselectaction.expand;
Node. Childnodes.add (NewNode);
}
}
}

void Filltitlesforauthors (TreeNode node)
{
String Authorid = node. Value;
String connstring = System.Configuration.ConfigurationSettings.
connectionstrings["Northwindconnnection"]. ConnectionString;
SqlConnection connection = new SqlConnection (connstring);
SqlCommand command = new SqlCommand ("Select T.title,
T.TITLE_ID from titles T "+
"Inner Join titleauthor TA on
t.title_id = ta.title_id "+
"Where ta.au_id = '" + Authorid + "'", 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["title"]. ToString (), row["title_id"]. ToString ());
Newnode.populateondemand = false;
Newnode.selectaction = Treenodeselectaction.none;
Node. Childnodes.add (NewNode);
}
}
}

</script>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:treeviewrunat= "Server" expandimageurl= "Images/closed.gif"
Collapseimageurl= "Images/open.gif"
Ontreenodepopulate= "Node_populate" id= "Tvwauthors" >
<Nodes>
<asp:treenodetext= "Authors" populateondemand=true
value= "0"/>
</Nodes>
</asp:TreeView>
</div>
</form>
</body>
Note that the Ontreenodepopulate event occurs when the node of the tree is expanded, where the custom node_populate is defined, and in node_populate, the depth of the current node is checked, and if 0 is the root node, So call the fillauthors process, take out all the authors, if the depth is 1, is the leaf node, call the filltitlesforauthors process. Among them, we should pay attention to the dynamic establishment of tree nodes in their process, such as:
TreeNode NewNode = new TreeNode (row["au_fname"). ToString () + "" +
row["au_lname"]. ToString (),

row["au_id"]. ToString ());

Newnode.populateondemand = true;

Newnode.selectaction = Treenodeselectaction.expand;

Node. Childnodes.add (NewNode);
The Popluateondemand property indicates that the node is dynamically expanded.




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.