using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, TreeNodeEventArgs e){if (!IsPostBack == true){switch (e.Node.Depth){case 0:PopulateCategories(e.Node);break;}}}void PopulateCategories(TreeNode node){
// Query for the product categories. These are the values
// for the second-level nodes.
DataSet ResultSet = RunQuery("Select row_id, name From Categories ");
// Create the second-level nodes.
if (ResultSet.Tables.Count > 0){
// Iterate through and create a new node for each row in the query results.
// Notice that the query results are stored in the table of the DataSet.
foreach (DataRow row in ResultSet.Tables[0].Rows){
// Create the new node. Notice that the CategoryId is stored in the Value property
// of the node. This will make querying for items in a specific category easier when
// the third-level nodes are created.
TreeNode newNode =
new TreeNode
();newNode.
Text = row
["name"].
ToString();newNode.
Value = row
["row_id"].
ToString();
// Set the PopulateOnDemand property to true so that the child nodes can be
// dynamically populated.
newNode.PopulateOnDemand = true;
// Set additional properties for the node.
newNode.SelectAction = TreeNodeSelectAction.Select;
// Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(newNode); } } }void PopulateProducts(TreeNode node){
// Query for the products of the current category. These are the values
// for the third-level nodes.
DataSet ResultSet = RunQuery("Select row_id,name From Categories Where row_id=" + node.Value);
// Create the third-level nodes.
if (ResultSet.Tables.Count > 0){
// Iterate through and create a new node for each row in the query results.
// Notice that the query results are stored in the table of the DataSet.
foreach (DataRow row in ResultSet.Tables[0].Rows){
// Create the new node.
TreeNode NewNode =
new TreeNode
();NewNode.
Text = row
["name"].
ToString();NewNode.
Value = row
["row_id"].
ToString();
// Set the PopulateOnDemand property to false, because these are leaf nodes and do not need to be populated.
NewNode.PopulateOnDemand = false;
// Set additional properties for the node.
NewNode.SelectAction = TreeNodeSelectAction.None;
// Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(NewNode); } }}DataSet RunQuery(String QueryString){
// Declare the connection string.
String ConnectionString =
"server=servername;database=database name;user id=user id;password=password";SqlConnection DBConnection =
new SqlConnection
(ConnectionString
);SqlDataAdapter DBAdapter;DataSet ResultsDataSet =
new DataSet
();try
{
// Run the query and create a DataSet.
DBAdapter =
new SqlDataAdapter
(QueryString, DBConnection
);DBAdapter.
Fill(ResultsDataSet
);
// Close the database connection.
DBConnection.Close();}catch (Exception ex){
// Close the database connection if it is still open.
if (DBConnection.State == ConnectionState.Open){DBConnection.Close();} string str = "Unable to connect to the database.";}return ResultsDataSet;}protected void Page_Load(object sender, EventArgs e){}protected void LinksTreeView_SelectedNodeChanged(object sender, EventArgs e){TextBox1.Text = LinksTreeView.SelectedNode.Value;TextBox2.Text = LinksTreeView.SelectedNode.Text;}}