WebForm1.aspx.cs
上传用户:haiyoufeng
上传日期:2021-09-14
资源大小:194k
文件大小:5k
源码类别:

TreeView控件

开发平台:

C#

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Web;
  7. using System.Web.SessionState;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.HtmlControls;
  11. using Microsoft.Web.UI.WebControls;
  12. using System.Data.SqlClient;
  13. namespace vs03BindTreeview
  14. {
  15. /// <summary>
  16. /// WebForm1 的摘要说明。
  17. /// </summary>
  18. public class WebForm1 : System.Web.UI.Page
  19. {
  20. string conString=System.Configuration.ConfigurationSettings.AppSettings["conString"].ToString();
  21. protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
  22. protected System.Web.UI.WebControls.Button btnGetValue;
  23. protected System.Web.UI.HtmlControls.HtmlInputHidden checkedID;
  24. protected System.Web.UI.HtmlControls.HtmlInputHidden checkedNodeData;
  25. protected System.Web.UI.WebControls.ListBox ListBox1;
  26. protected System.Web.UI.WebControls.ListBox ListBox2;
  27. protected System.Web.UI.WebControls.Label Label3;
  28. protected System.Web.UI.WebControls.Label Label4;
  29. protected System.Web.UI.WebControls.Button Button1;
  30. private void Page_Load(object sender, System.EventArgs e)
  31. {
  32. // 在此处放置用户代码以初始化页面
  33. if (!this.Page.IsPostBack)
  34. {
  35. this.TreeView1.Attributes.Add("oncheck", "tree_oncheck(this)");
  36. }
  37. }
  38. #region Web 窗体设计器生成的代码
  39. override protected void OnInit(EventArgs e)
  40. {
  41. //
  42. // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
  43. //
  44. InitializeComponent();
  45. base.OnInit(e);
  46. }
  47. /// <summary>
  48. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  49. /// 此方法的内容。
  50. /// </summary>
  51. private void InitializeComponent()
  52. {    
  53. this.Button1.Click += new System.EventHandler(this.Button1_Click);
  54. this.btnGetValue.Click += new System.EventHandler(this.btnGetValue_Click);
  55. this.Load += new System.EventHandler(this.Page_Load);
  56. }
  57. #endregion
  58.         
  59. public DataSet GetDataSet(string sql, string tablename)
  60. {
  61. DataSet ds = new DataSet();
  62. SqlConnection con = new SqlConnection(conString);
  63. SqlDataAdapter da = new SqlDataAdapter(sql, con);
  64. try
  65. {
  66. da.Fill(ds, tablename);
  67. }
  68. catch (Exception ex)
  69. {
  70. throw new Exception(ex.ToString());
  71. }
  72. finally
  73. {
  74. con.Close();
  75. con.Dispose();
  76. da.Dispose();
  77. }
  78. return ds;
  79. }
  80. public SqlDataReader GetReader(string sql)   
  81. {
  82. SqlConnection con = new SqlConnection(conString);
  83. SqlCommand cmd = new SqlCommand(sql, con);
  84. SqlDataReader dr = null;
  85. try
  86. {
  87. con.Open();
  88. dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  89. }
  90. catch (Exception ex)
  91. {
  92. dr.Close();
  93. con.Dispose();
  94. cmd.Dispose();
  95. throw new Exception(ex.ToString());
  96. }
  97. return dr;
  98. }
  99. private void Button1_Click(object sender, System.EventArgs e)
  100. {
  101. this.TreeView1.Nodes.Clear();
  102.     this.AddTree("", (TreeNode)null);//只能遍历一个根节点,否则会进入死循环
  103. }
  104. private void AddTree(string id,TreeNode pNode)
  105. {
  106. try
  107. {
  108. DataSet dst=GetDataSet("select DeptID,DeptName,DeptParentID from Department order by DeptID ASC","Department");
  109. DataView dv=new DataView(dst.Tables["Department"]);
  110. dv.RowFilter="DeptParentID = '"+id +"'";
  111. foreach(DataRowView row in dv)
  112. {
  113. TreeNode deptNode=new TreeNode();
  114. deptNode.Text=row["DeptName"].ToString();
  115. deptNode.NodeData=row["DeptName"].ToString();
  116. deptNode.ID=row["DeptID"].ToString();
  117. deptNode.ImageUrl="images/Dept.gif";
  118. deptNode.CheckBox=true;//设置是否复选框
  119. if(pNode==null)//说明是根节点
  120. {
  121. this.TreeView1.Nodes.Add(deptNode);
  122. deptNode.Expanded=true;
  123. AddTree(row["DeptID"].ToString(),deptNode);
  124. }
  125. else
  126. {
  127. pNode.Nodes.Add(deptNode);
  128. AddTree(row["DeptID"].ToString(),deptNode);
  129. }
  130. }
  131. }
  132. catch
  133. {
  134. this.Response.Write("<script>alert('对不起!读取错误!')</script>");
  135. }
  136. }
  137. private void btnGetValue_Click(object sender, System.EventArgs e)
  138. {
  139. this.ListBox1.Items.Clear();
  140.     this.ListBox2.Items.Clear();
  141. string userID=this.checkedID.Value;
  142. string nodeData=this.checkedNodeData.Value;
  143. string[] arrUserID=userID.Split('|');
  144. string[] arrNodeData=nodeData.Split('|');
  145. for(int i=0;i<arrUserID.Length-1;i++)
  146. {
  147. ListItem liID=new ListItem(arrUserID[i]);
  148. ListItem liName=new ListItem(arrNodeData[i]);
  149. this.ListBox1.Items.Add(liID);
  150. this.ListBox2.Items.Add(liName);
  151. }
  152. }
  153. }
  154. }