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
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 (),
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.