WebForm1.aspx.cs
上传用户:haiyoufeng
上传日期:2021-09-14
资源大小:194k
文件大小:5k
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Web;
- using System.Web.SessionState;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.HtmlControls;
- using Microsoft.Web.UI.WebControls;
- using System.Data.SqlClient;
- namespace vs03BindTreeview
- {
- /// <summary>
- /// WebForm1 的摘要说明。
- /// </summary>
- public class WebForm1 : System.Web.UI.Page
- {
- string conString=System.Configuration.ConfigurationSettings.AppSettings["conString"].ToString();
- protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
- protected System.Web.UI.WebControls.Button btnGetValue;
- protected System.Web.UI.HtmlControls.HtmlInputHidden checkedID;
- protected System.Web.UI.HtmlControls.HtmlInputHidden checkedNodeData;
- protected System.Web.UI.WebControls.ListBox ListBox1;
- protected System.Web.UI.WebControls.ListBox ListBox2;
- protected System.Web.UI.WebControls.Label Label3;
- protected System.Web.UI.WebControls.Label Label4;
- protected System.Web.UI.WebControls.Button Button1;
-
- private void Page_Load(object sender, System.EventArgs e)
- {
- // 在此处放置用户代码以初始化页面
- if (!this.Page.IsPostBack)
- {
- this.TreeView1.Attributes.Add("oncheck", "tree_oncheck(this)");
- }
- }
- #region Web 窗体设计器生成的代码
- override protected void OnInit(EventArgs e)
- {
- //
- // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
- //
- InitializeComponent();
- base.OnInit(e);
- }
-
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.Button1.Click += new System.EventHandler(this.Button1_Click);
- this.btnGetValue.Click += new System.EventHandler(this.btnGetValue_Click);
- this.Load += new System.EventHandler(this.Page_Load);
- }
- #endregion
-
- public DataSet GetDataSet(string sql, string tablename)
- {
- DataSet ds = new DataSet();
- SqlConnection con = new SqlConnection(conString);
-
- SqlDataAdapter da = new SqlDataAdapter(sql, con);
- try
- {
- da.Fill(ds, tablename);
- }
- catch (Exception ex)
- {
- throw new Exception(ex.ToString());
- }
- finally
- {
- con.Close();
- con.Dispose();
- da.Dispose();
- }
- return ds;
- }
- public SqlDataReader GetReader(string sql)
- {
- SqlConnection con = new SqlConnection(conString);
- SqlCommand cmd = new SqlCommand(sql, con);
- SqlDataReader dr = null;
- try
- {
- con.Open();
- dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
- }
- catch (Exception ex)
- {
- dr.Close();
- con.Dispose();
- cmd.Dispose();
- throw new Exception(ex.ToString());
- }
- return dr;
- }
- private void Button1_Click(object sender, System.EventArgs e)
- {
- this.TreeView1.Nodes.Clear();
-
- this.AddTree("", (TreeNode)null);//只能遍历一个根节点,否则会进入死循环
- }
- private void AddTree(string id,TreeNode pNode)
- {
- try
- {
- DataSet dst=GetDataSet("select DeptID,DeptName,DeptParentID from Department order by DeptID ASC","Department");
- DataView dv=new DataView(dst.Tables["Department"]);
-
- dv.RowFilter="DeptParentID = '"+id +"'";
- foreach(DataRowView row in dv)
- {
- TreeNode deptNode=new TreeNode();
- deptNode.Text=row["DeptName"].ToString();
- deptNode.NodeData=row["DeptName"].ToString();
- deptNode.ID=row["DeptID"].ToString();
- deptNode.ImageUrl="images/Dept.gif";
- deptNode.CheckBox=true;//设置是否复选框
-
-
- if(pNode==null)//说明是根节点
- {
- this.TreeView1.Nodes.Add(deptNode);
- deptNode.Expanded=true;
- AddTree(row["DeptID"].ToString(),deptNode);
- }
- else
- {
-
- pNode.Nodes.Add(deptNode);
- AddTree(row["DeptID"].ToString(),deptNode);
- }
- }
- }
- catch
- {
- this.Response.Write("<script>alert('对不起!读取错误!')</script>");
- }
- }
- private void btnGetValue_Click(object sender, System.EventArgs e)
- {
- this.ListBox1.Items.Clear();
- this.ListBox2.Items.Clear();
- string userID=this.checkedID.Value;
- string nodeData=this.checkedNodeData.Value;
-
- string[] arrUserID=userID.Split('|');
- string[] arrNodeData=nodeData.Split('|');
- for(int i=0;i<arrUserID.Length-1;i++)
- {
- ListItem liID=new ListItem(arrUserID[i]);
- ListItem liName=new ListItem(arrNodeData[i]);
- this.ListBox1.Items.Add(liID);
- this.ListBox2.Items.Add(liName);
- }
- }
- }
- }