treeview之动态绑定(部门--员工)

 

细心地朋友可能看到我用了两种方法,一个是msdn上的。。(msdn上的前台还要添加node节点)

代码如下:

前台;

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="treeview.aspx.cs" Inherits="treeview" Debug="true"%>

 

 

 

 

 

 

后台:

 

public partial class treeview : System.Web.UI.Page { private string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["netmeetingConnectionString"].ToString(); public DataSet connectDatabase() { SqlConnection sqlCon = new SqlConnection(conStr); SqlCommand sqlCmd = new SqlCommand(); string cmdStr = "select * from Department"; sqlCmd.Connection = sqlCon; sqlCmd.CommandText = cmdStr; SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand =sqlCmd; DataSet ds = new DataSet(); adapter.Fill(ds); return ds; } public DataSet connectDatabase1(string department) { SqlConnection sqlCon = new SqlConnection(conStr); SqlCommand sqlCmd = new SqlCommand(); string cmdStr = "select * from Employee where Department="+department; sqlCmd.Connection = sqlCon; sqlCmd.CommandText = cmdStr; DataSet ds = new DataSet(); SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = sqlCmd; adapter.Fill(ds); return ds; } DataSet RunQuery(string Querystring) { SqlConnection sqlCon=new SqlConnection(conStr); SqlDataAdapter Adapter; DataSet ResultDataset=new DataSet(); try { Adapter=new SqlDataAdapter(Querystring,sqlCon); Adapter.Fill(ResultDataset); sqlCon.Close(); } catch(Exception ex) { if(sqlCon.State==ConnectionState.Open) sqlCon.Close(); } return ResultDataset; } public void PopulateCategories(TreeNode node) { DataSet ResultSet = RunQuery("select id,Department from Department"); if (ResultSet.Tables.Count > 0) { foreach (DataRow row in ResultSet.Tables[0].Rows) { TreeNode newNode = new TreeNode(); newNode.Text = row[1].ToString(); newNode.Value = row[0].ToString(); newNode.PopulateOnDemand = true; newNode.SelectAction = TreeNodeSelectAction.Expand; node.ChildNodes.Add(newNode); } } } public void PopulateProducts(TreeNode node) { DataSet ResultSet = RunQuery("select * from Employee where Department="+node.Value); if (ResultSet.Tables.Count > 0) { foreach (DataRow row in ResultSet.Tables[0].Rows) { TreeNode newNode = new TreeNode(row[1].ToString()); newNode.PopulateOnDemand = false; newNode.SelectAction = TreeNodeSelectAction.None; node.ChildNodes.Add(newNode); } } } public void testBind() { TreeNode root = new TreeNode(); root.Text = "视频会议"; root.ImageToolTip = "这是视频会议。"; root.ImageUrl = "1.jpg"; root.ShowCheckBox = true; root.NavigateUrl = ""; DataSet ds = new DataSet(); ds = connectDatabase(); foreach (DataRow dr in ds.Tables[0].Rows) { TreeNode tn = new TreeNode(); tn.Text = dr[1].ToString(); tn.NavigateUrl = ""; root.ChildNodes.Add(tn); DataSet ds1 = new DataSet(); ds1=connectDatabase1(dr[0].ToString()); foreach (DataRow dr1 in ds1.Tables[0].Rows) { TreeNode tn1 = new TreeNode(); tn1.Text = dr1[1].ToString(); tn1.NavigateUrl = ""; tn.ChildNodes.Add(tn1); } } TreeView3.Nodes.Add(root); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //testBind(); } } protected void TreeView4_TreeNodePopulate(object sender, TreeNodeEventArgs e) { switch (e.Node.Depth) { case 0: PopulateCategories(e.Node); break; case 1: PopulateProducts(e.Node); break; default: break; } } } 

你可能感兴趣的:(asp.net)