| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1122 人关注过本帖
标题:TreeView问题点击文字选中Check问题!!!!
只看楼主 加入收藏
lanweiming
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-9-28
收藏
 问题点数:0 回复次数:0 
TreeView问题点击文字选中Check问题!!!!
我自己写了2个方法,但实现起来都有问题.
要求:
1.点击子级,父节点不被选中。
2.点击父节点,子节点全部选中
3.点击文字时效果要与点击CheckBox一样.(说明,点击一次文字选中复选框,下面的所有自己的复选框都被选中。再点则取消选中!)

这是我自己写的
Demo1:(问题,只能通过选中复选框来实现效果,而且不支持火狐)
程序代码:
Page:
<asp:TreeView ID="TreeV2" runat="server" ShowLines="True" OnTreeNodeCheckChanged="TreeView1_TreeNodeCheckChanged">
                                    </asp:TreeView>
Page JS:
  // 点击复选框时触发事件
        function postBackByObject() {
            var o = window.event.srcElement;
            if ((o.tagName == "INPUT" && o.type == "checkbox") || o.tagName == "A") {
                //这里的第一个参数是UpdatePanel ID,因为我使用了MS的ASPAJAX来实现局部刷新
                //如果没有使用MS的ASPAJAX,这里的两个参数都可以为空
                __doPostBack("", "");
            }
        }
Code:

 //号码簿
    private void BindWEmail()
    {
        TreeV2.Nodes.Clear();
        string sql = "select * from SMS_NumberGroup where 1=1";//查询组
        if (ht["Detail"] == null)
            sql += " and (Levels=1 or (Levels=0 and UIDS like '%," + UserInfo.UserId + ",%'))";

        DataTable dt = Iwoak.ExecSQL.ExecuteDataTable(sql);
        BindTree(dt, 0, null, TreeV2);
        TreeV2.ShowCheckBoxes = TreeNodeTypes.All;//
        TreeV2.Attributes.Add("onclick", "postBackByObject()");
        //TreeV2.Attributes.Add("onclick", "OnTreeNodeChecked(event);");
    }
    /// <summary>
    /// 绑定Tree
    /// </summary>
    /// <param name="dt">数据集</param>
    /// <param name="parentId">链接ID</param>
    /// <param name="pNode">操作节点</param>
    /// <param name="tv"></param>
    private void BindTree(DataTable dt, int parentId, TreeNode pNode, TreeView tv)
    {
        DataView dv = new DataView(dt);
        dv.RowFilter = "[mid]=" + parentId;
        foreach (DataRowView drv in dv)
        {
            TreeNode tNode = new TreeNode();
            tNode.ShowCheckBox = true;

            string ID = drv["ID"].ToString();
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.AppendFormat("&nbsp;&nbsp;<span title='{0}'>" + drv["SName"].ToString() + "</span>&nbsp;&nbsp;", drv["Remark"]);
            tNode.Text = sb.ToString();
            tNode.Value = ID;
            DataTable dtf = Iwoak.ExecSQL.ExecuteDataTable("select * from [SMS_Number] where nid=" + ID + GetQueryW());
            if (pNode == null)
            {
                #region 联系人

                for (int i = 0; i < dtf.Rows.Count; i++)
                {
                    DataRow dr = dtf.Rows[i];
                    TreeNode p2 = new TreeNode();
                    p2.ShowCheckBox = true;
                    p2.Text = string.Format("<a href=\"javascript:void();\" title='{2}'>{1} {0}</a>", dr["Phone"], dr["Contact"], dr["Remark"]);
                    p2.Value = dr["Phone"].ToString();
                    tNode.ChildNodes.Add(p2);
                }
                #endregion
                tv.Nodes.Add(tNode);
                tNode.Expanded = true;
                BindTree(dt, Convert.ToInt32(drv["ID"]), tNode, tv);

            }
            else
            {
                pNode.ChildNodes.Add(tNode);
                tNode.Expanded = true;
                BindTree(dt, Convert.ToInt32(drv["ID"]), tNode, tv);
                #region 联系人
                for (int i = 0; i < dtf.Rows.Count; i++)
                {
                    DataRow dr = dtf.Rows[i];
                    TreeNode p2 = new TreeNode();
                    //p2.ShowCheckBox = true;
                    p2.Text = string.Format("<a href=\"javascript:void());\" title='{2}'>{1} {0}</a>", dr["Phone"], dr["Contact"], dr["Remark"]);
                    p2.Value = dr["Phone"].ToString();
                    tNode.ChildNodes.Add(p2);
                }
                #endregion
            }

        }
    }

Demo2:(问题,点击文字时,自身的复选框选中,但子级没有选中,支持火狐)
程序代码:
Page:
<asp:TreeView ID="tvDept" runat="server" AllowCascadeCheckbox="True" CssClass="treelink"
                    OnTreeNodeCheckChanged="TreeView1_TreeNodeCheckChanged" CollapseImageUrl="~/CommonFiles/imgs/SystemImg/file.gif"
                    ExpandImageUrl="~/CommonFiles/imgs/SystemImg/folder.gif">
                    <SelectedNodeStyle CssClass="treelink" />
                    <NodeStyle CssClass="treelink" />
                </asp:TreeView>
Page JS:

    <script type="text/javascript">
        //        $(function() {
        //            parent.ymPrompt.max();
        //        });
        // 点击复选框时触发事件
        function postBackByObject() {
            var o = window.event.srcElement;
            if ((o.tagName == "INPUT" && o.type == "checkbox") || o.tagName == "A") {
                //这里的第一个参数是UpdatePanel ID,因为我使用了MS的ASPAJAX来实现局部刷新
                //如果没有使用MS的ASPAJAX,这里的两个参数都可以为空
                __doPostBack("", "");
            }
        }
        function OnTreeNodeChecked(e) //单击父节点子结点选中的代码
        {
            if (window.event) e = window.event;
            var evt = e.srcElement ? e.srcElement : e.target;
            if (evt.type != undefined || evt.tagName != undefined) {

                if (evt.type == 'checkbox') {
                    var childrenDivID = evt.id.replace('CheckBox', 'Nodes');
                    var div = document.getElementById(childrenDivID);
                    if (div == null) return;
                    var checkBoxs = div.getElementsByTagName('INPUT');
                    for (var i = 0; i < checkBoxs.length; i++) {
                        if (checkBoxs[i].type == 'checkbox')
                            checkBoxs[i].checked = evt.checked;
                    }

                }
                else if (evt.tagName == "A") {
                    if (evt.previousSibling && evt.previousSibling.tagName == "INPUT" && evt.previousSibling.type == "checkbox") {
                        //                        evt.previousSibling.checked = true;
                        evt.previousSibling.checked = evt.previousSibling.checked ? false : true;
                    }
                    if (window.event) {
                        window.event.cancelBubble = true;
                        window.event.returnValue = false;
                    }
                    else {
                        e.stopPropagation();
                    }

                }
            }
        }
    </script>


 Code:
    #region 页面加载时执行
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Common_BindTree(rule.GetDept(), 0, (TreeNode)null, this.tvDept);
            tvDept.ShowCheckBoxes = TreeNodeTypes.All;//
            tvDept.Attributes.Add("onclick", "OnTreeNodeChecked(event);");
        }
    }
    #endregion
    //初始化部门
    public void Common_BindTree(DataSet ds, int parentId, TreeNode pNode, TreeView tv)
    {
        DataView dv = new DataView(ds.Tables[0]);
        dv.RowFilter = "[CID]=" + parentId;
        foreach (DataRowView drv in dv)
        {
            TreeNode tNode = new TreeNode();

            tNode.Text = drv["Name"].ToString();
            tNode.ShowCheckBox = true;
            tNode.NavigateUrl = "#";
            tNode.Value = drv["ID"].ToString();

            if (ViewState["Selected"] != null)
            {
                DataTable dt = (DataTable)ViewState["Selected"];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["DeptID"].ToString() == tNode.Value)
                    {
                        tNode.Checked = true;
                    }
                }

            }

            //添加该节点下的所有用户
            Iw_UsersRule rule = new Iw_UsersRule();
            DataSet UsersDs = rule.GetUsers(tNode.Value);
            if (UsersDs.Tables[0].Rows.Count > 0)
            {
                for (int k = 0; k < UsersDs.Tables[0].Rows.Count; k++)
                {
                    string UserID = UsersDs.Tables[0].Rows[k]["ID"].ToString();
                    string UserName = UsersDs.Tables[0].Rows[k]["UserName"].ToString();
                    TreeNode NodeUser = new TreeNode();
                    NodeUser.Text = UserName;
                    NodeUser.Value = "User_" + UsersDs.Tables[0].Rows[k]["ID"].ToString();
                    NodeUser.ImageUrl = "~/CommonFiles/imgs/MenuImages/employee.gif";
                    NodeUser.ShowCheckBox = true;
                    if (Request.QueryString["ChooseUserID"] != null && Request.QueryString["ChooseUserID"] != "")//再次选择时显示上次已选中的项
                    {
                        string[] ChooseDeptID = Request.QueryString["ChooseUserID"].Split(',');
                        for (int j = 0; j < ChooseDeptID.Length; j++)
                        {
                            if (UserID == ChooseDeptID[j].ToString())
                                NodeUser.Checked = true;
                        }
                    }

                    tNode.ChildNodes.Add(NodeUser);
                }
            }
            //父节点
            if (pNode == null)
            {
                tv.Nodes.Add(tNode);
                tNode.Expanded = true;


                Common_BindTree(ds, Convert.ToInt32(drv["ID"]), tNode, tv);
            }
            else
            {

                pNode.ChildNodes.Add(tNode);
                Common_BindTree(ds, Convert.ToInt32(drv["ID"]), tNode, tv);
            }
        }
    }



搜索更多相关主题的帖子: 火狐 效果 
2012-09-28 10:10
快速回复:TreeView问题点击文字选中Check问题!!!!
数据加载中...
 
   



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

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