你的位置:首页 > ASP.net教程

[ASP.net教程]asp.net TreeView


希望有实力的朋友了以后能指点我改一下选中和不选中的递归

 

前台代码

 <div style="font-size:14px;">            <asp:TreeView ShowCheckBoxes="All" ID="usertree" runat="server" OnTreeNodeCheckChanged="MyTreeView_TreeNodeCheckChanged" OnSelectedNodeChanged="MyTreeView_SelectedNodeChanged" >            </asp:TreeView>          </div>              <div style="float:left;">      <span style="display:block">要参加人员:</span>      <asp:ListBox ID="NeedExamStaff" AutoPostBack="true" runat="server" Height="262px" Width="116px" SelectionMode="Multiple">      <asp:ListItem Value="张三"></asp:ListItem>      <asp:ListItem Value="李四"></asp:ListItem>      <asp:ListItem>王五</asp:ListItem>      </asp:ListBox>        <hr />       <span style="margin-bottom:0px">总 人 员:<asp:TextBox ID="NeedStaffNumber" runat="server" Text="" Enabled="false" Height="16px" Width="56px"></asp:TextBox><br /><span style="color:red;font-size:small" id="VNeedStaffNumber" runat="server"></span></span>       </div>        <asp:Button ID="Button1" runat="server" Text="确定" OnClick="Button1_Click" Height="21px" />

View Code

 

后台代码

    DataTable dt = new DataTable();    protected void Page_Load(object sender, EventArgs e)    {      if (!IsPostBack)      {        SqlConnection conn = new SqlConnection();        conn.ConnectionString = "User ID=sa;Initial Catalog=newtest;Data Source=JJ-PC;Password=123";        // 打开连接        if (conn.State == ConnectionState.Closed)        {          conn.Open();        }        var sql = "select * from city";        // 初始化命令        SqlCommand cmd = new SqlCommand();        cmd.Connection = conn;        cmd.CommandType = CommandType.Text;        cmd.CommandText = sql;        // 用于执行数据插入、更新和删除的操作;返回被影响的行数。        int i = cmd.ExecuteNonQuery();        // 如果想获取数据集合的话我们经常使用到的是数据适配器        SqlDataAdapter adapter = new SqlDataAdapter();        adapter.SelectCommand = cmd;        adapter.Fill(dt);        this.usertree.Nodes.Clear();        usertree.Attributes.Add("onclick", " postBackByObject()");        AddTree(110000, (TreeNode)null);      }    }    //递归添加树的节点    public void AddTree(int ParentID, TreeNode pNode)    {      DataTable ds = dt;      DataView dvTree = new DataView(ds);      //过滤ParentID,得到当前的所有子节点      dvTree.RowFilter = "[fatherID] = " + ParentID;      foreach (DataRowView Row in dvTree)      {        TreeNode Node = new TreeNode();        if (pNode == null)        {          //添加根节点          Node.Text = Row[2].ToString();          Node.Value = Row[1].ToString();          this.usertree.Nodes.Add(Node);          Node.Expanded = true;          Node.ShowCheckBox = true;          //再次递归          AddTree(Int32.Parse(Row[1].ToString()), Node);        }        else        {          //添加当前节点的子节点          Node.Text = Row[2].ToString();          Node.Value = Row[1].ToString();          pNode.ChildNodes.Add(Node);          Node.Expanded = true;          Node.ShowCheckBox = true;          //再次递归          AddTree(Int32.Parse(Row[1].ToString()), Node);        }      }    }    protected void MyTreeView_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)    {      TreeNode node = e.Node;      var nodeCount = node.ChildNodes.Count;      if (node.Checked == true)      {        if (nodeCount > 0)        {          for (var i = 0; i < nodeCount; i++)          {            node.ChildNodes[i].Checked = true;            var node1Count = node.ChildNodes[i].ChildNodes.Count;            if (node1Count > 0)            {              for (var k = 0; k < node1Count; k++)              {                node.ChildNodes[i].ChildNodes[k].Checked = true;                var node2Count = node.ChildNodes[i].ChildNodes[k].ChildNodes.Count;                if (node2Count > 0)                {                  for (var j = 0; j < node2Count; j++)                  {                    node.ChildNodes[i].ChildNodes[k].ChildNodes[j].Checked = true;                  }                }              }            }          }        }      }      else      {        if (nodeCount > 0)        {          for (var i = 0; i < nodeCount; i++)          {            node.ChildNodes[i].Checked = false;            var node1Count = node.ChildNodes[i].ChildNodes.Count;            if (node1Count > 0)            {              for (var k = 0; k < node1Count; k++)              {                node.ChildNodes[i].ChildNodes[k].Checked = false;                var node2Count = node.ChildNodes[i].ChildNodes[k].ChildNodes.Count;                if (node2Count > 0)                {                  for (var j = 0; j < node2Count; j++)                  {                    node.ChildNodes[i].ChildNodes[k].ChildNodes[j].Checked = false;                  }                }              }            }          }        }      }    }    protected void MyTreeView_SelectedNodeChanged(object sender, EventArgs e)    {      var tt = usertree.SelectedNode.Checked;      var dd = usertree.SelectedValue;      databind();    }    public void databind()    {      if (usertree.SelectedNode.Checked == false)      {        if (usertree.SelectedNode.ChildNodes.Count > 0)        {          foreach (TreeNode subNode in usertree.SelectedNode.ChildNodes)          {            usertree.SelectedNode.Checked = true;            subNode.Checked = true;            if (subNode.ChildNodes.Count > 0)            {              foreach (TreeNode subNode2 in subNode.ChildNodes)              {                subNode2.Checked = true;                if (subNode2.ChildNodes.Count > 0)                {                  foreach (TreeNode subNode3 in subNode2.ChildNodes)                  {                    subNode3.Checked = true;                  }                }              }            }          }        }        else        {          usertree.SelectedNode.Checked = true;        }      }      else      {        if (usertree.SelectedNode.ChildNodes.Count > 0)        {          foreach (TreeNode subNode in usertree.SelectedNode.ChildNodes)          {            usertree.SelectedNode.Checked = false;            subNode.Checked = false;            if (subNode.ChildNodes.Count > 0)            {              foreach (TreeNode subNode2 in subNode.ChildNodes)              {                subNode2.Checked = false;                if (subNode2.ChildNodes.Count > 0)                {                  foreach (TreeNode subNode3 in subNode2.ChildNodes)                  {                    subNode3.Checked = false;                  }                }              }            }          }        }        else        {          usertree.SelectedNode.Checked = false;        }      }      usertree.SelectedNode.Selected = false;    }    protected void Button1_Click(object sender, EventArgs e)    {      foreach (TreeNode tn in usertree.CheckedNodes)      {        try        {          string userid = tn.Value.ToString();                     ListItem li = new ListItem();            li.Text = tn.Value;            li.Value = tn.Text;            var tt = 0;            for (var i = 0; i < NeedExamStaff.Items.Count; i++)            {              if (NeedExamStaff.Items[i].Value == li.Value)              {                tt = 1;              }            }            if (tt == 0)            {              this.NeedExamStaff.Items.Add(li);              if (this.NeedStaffNumber.Text != "")              {                this.NeedStaffNumber.Text = (Convert.ToInt32(this.NeedStaffNumber.Text) + 1).ToString();                this.VNeedStaffNumber.InnerText = "";              }              else              {                this.NeedStaffNumber.Text = "1";                this.VNeedStaffNumber.InnerText = "";              }            }          }                catch        {        }      }    }

View Code