标题:急 求帮忙解答如何不利用treeview
只看楼主
汐汐
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-3-27
结帖率:66.67%
已结贴  问题点数:20 回复次数:4 
急 求帮忙解答如何不利用treeview
如何使选中山西省就可以把下面所有的太原市,大同市等等都选中
【北京市】
【天津市】
【山西省】
    |-【太原市】
    |-【大同市】
    |-【阳泉市】
    |-【长治市】
       while(document.form1.Audit.selectedIndex!=-1)
        {
            var newoption=document.createElement("option");//括号中的option可以变成option[]么?
            newoption.text=document.form1.Audit.options[document.form1.Audit.selectedIndex].text;
            newoption.value=document.form1.Audit.options[document.form1.Audit.selectedIndex].value;
            
            if(IsNotExits(newoption.value,newoption.text))
            {
                document.form1.selected_Audit.add(newoption);
                document.form1.Audit.remove(document.form1.Audit.selectedIndex);
            }
            else
            {
                document.form1.Audit.options[document.form1.Audit.selectedIndex].selected=false;
            }
        }使用这种方法怎么使选中父节点,子节点也全部选中

[ 本帖最后由 汐汐 于 2012-4-10 14:11 编辑 ]
搜索更多相关主题的帖子: 天津市 北京市 大同市 山西省 
2012-04-10 14:07
yms123
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:209
帖 子:12488
专家分:19042
注 册:2004-7-17
得分:7 
https://hi.bccn.net/space-1949-do-blog-id-34265.html
推荐LZ看看我自制的这个TreeView
2012-04-11 10:20
Issac_abc
Rank: 4
来 自:河南
等 级:业余侠客
威 望:2
帖 子:155
专家分:218
注 册:2010-6-17
得分:7 
程序代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 DAL;
using System.Collections.Generic;
using DAL.SysUser;
using DAL.Main;

public partial class Main_AddRole : System.Web.UI.Page
{
      DicDepartment DDept = new DicDepartment();
      ProjectModel PMain = new ProjectModel();
      SysRole role = new SysRole();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            Bind();

            IList<Model.ProjectModel> DicDModel = PMain.GetProjectModel("0");
            for (int i = 0; i < DicDModel.Count; i++)
            {
                string dicName=DicDModel[i].ModelName.ToString();
                string dicId=DicDModel[i].ModelId.ToString();

                TreeNode treefather = CreateTreeNode(dicName, dicId);

                CreateChileNode(dicId, treefather);

                TreeView1.Nodes.Add(treefather);

                TreeView1.Attributes.Add("onclick", "postBackByObject()");
               
            }
        }
    }
    /// <summary>
    /// 得到选择的值
    /// </summary>
    /// <param name="tc"></param>
    public void getnode(TreeNodeCollection tc)
    {
        foreach (TreeNode tnode in tc)
        {
            if (tnode.Checked)
            {
                //Response.Write(tnode.Value + "<br/>");
                string txt = txtName.Text.ToString();
                string x = tnode.Value.ToString();
                role.InsertSysRole(txt, x);
            }
            getnode(tnode.ChildNodes);
        }
    } 

    //获取得到空间节点的值和名称
    public TreeNode CreateTreeNode(string NodeName,string NodeValue)
    {
        TreeNode treenode = new TreeNode();
        treenode.Text = NodeName;//节点的名称
        treenode.Value = NodeValue;//节点的值
        return treenode; //返回控件的节点
    }

    /// <summary>
    /// 得到父节点下的子节点
    /// </summary>
    /// <param name="NodeId"></param>
    /// <param name="TreeFather"></param>
    /// <returns></returns>
    public void CreateChileNode(string NodeId, TreeNode TreeFather)
    {
        IList<Model.ProjectModel> DicDModel = PMain.GetProjectModel(""+NodeId+"");
        for (int i = 0; i < DicDModel.Count; i++)
        {
            string dicName = DicDModel[i].ModelName.ToString();
            string dicId=DicDModel[i].ModelId.ToString();

            TreeNode childNode = CreateTreeNode(dicName, dicId);

            AddTree(TreeFather, childNode);

            CreateChileNodes(dicId, childNode);//子级的下一级
        }
        TreeView1.ExpandDepth = 0;
    }

    //得到字节点的下一级节点
    public void CreateChileNodes(string NodeId, TreeNode TreeFather)
    {
        IList<Model.ProjectModel> DicDModel = PMain.GetProjectModel("" + NodeId + "");
        for (int i = 0; i < DicDModel.Count; i++)
        {
            string dicName = DicDModel[i].ModelName.ToString();
            string dicId = DicDModel[i].ModelId.ToString();
            TreeNode childNode = CreateTreeNode(dicName, dicId);
            AddTree(TreeFather, childNode);
        }
    }

    /// <summary>
    /// 将字节点加入父节点
    /// </summary>
    /// <param name="fatherNode"></param>
    /// <param name="chlidNode"></param>
    private void AddTree(TreeNode fatherNode,TreeNode chlidNode)
    {
        fatherNode.ChildNodes.Add(chlidNode);
    }

    public void Bind()
    {
        GridView1.DataSource = DDept.GetDept();
        GridView1.DataBind();
    }
    //提交
    protected void butTH_Click(object sender, EventArgs e)
    {
        string DName = txtName.Text.ToString();
        string DMode = txtDescript.Text.ToString();

        if (DDept.insert(DName, DMode) != 0)//做录入 部门信息
        {
            RegisterClientScriptBlock("", "<script>alert('成功!');</script>");
        }
        TreeNodeCollection tc = TreeView1.Nodes;
        getnode(tc);

    }

 

    protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
    {
        SetChildChecked(e.Node); // 调用 根据父节点状态设置子节点的状态 方法
        SetParentChecked(e.Node);//调用 选中子节点,其父节点也选中 方法
    }

    /// <summary>
    /// 选中子节点,其父节点也选中
    /// </summary>
    /// <param name="childNode"></param>
    private void SetParentChecked(TreeNode childNode)
    {
        if (childNode.Parent != null)
        {
            childNode.Parent.Checked = true;

            SetParentChecked(childNode.Parent);
        }
    }

    /// <summary>
    /// 根据父节点状态设置子节点的状态
    /// </summary>
    /// <param name="parentNode"></param>
    private void SetChildChecked(TreeNode parentNode)
    {
        foreach (TreeNode node in parentNode.ChildNodes)
        {
            node.Checked = parentNode.Checked;
            if (node.ChildNodes.Count > 0)
            {
                SetChildChecked(node);
            }
        }
    }
}
2012-04-11 13:27
汐汐
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-3-27
得分:0 
大侠们 我要是不用treeview控件呢 我想用select显示出来,select绑定数据库,而且可以点击一个option,则与之相关的数据库中子节点的option可以一起移动到另一个select中?????急 求帮忙
2012-04-11 23:05
aspic
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:51
帖 子:2258
专家分:8050
注 册:2008-2-18
得分:7 
js来做 关联的东西就需要自己定义了
2012-04-12 16:51



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-365499-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.083065 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved