网络日志
发布于:2007-01-26 10:13
关于2.0中TreeView的带有CheckBox全选测试代码

效果图:

javascript全选代码,放在aspx

   <script language="javascript">
function public_GetParentByTagName(element, tagName)
{
    var parent = element.parentNode;
    var upperTagName = tagName.toUpperCase();
    //如果这个元素还不是想要的tag就继续上溯
    while (parent && (parent.tagName.toUpperCase() != upperTagName))
    {
        parent = parent.parentNode ? parent.parentNode : parent.parentElement;
    }
    return parent;
}
//设置节点的父节点Cheched——该节点可访问,则他的父节点也必能访问
function setParentChecked(objNode)
{
        var objParentDiv = public_GetParentByTagName(objNode,"div");
        if(objParentDiv==null || objParentDiv == "undefined")
        {
            return;
        }
        var objID = objParentDiv.getAttribute("ID");
            objID = objID.substring(0,objID.indexOf("Nodes"));
            objID = objID+"CheckBox";
        var objParentCheckBox = document.getElementById(objID);
            if(objParentCheckBox==null || objParentCheckBox == "undefined")
        {
            return;
    }
    if(objParentCheckBox.tagName!="INPUT" && objParentCheckBox.type == "checkbox")
    return;
    objParentCheckBox.checked = true;
    setParentChecked(objParentCheckBox);
}
//设置节点的子节点uncheched——该节点不可访问,则他的子节点也不能访问
function setChildUnChecked(divID)
{
    var objchild = divID.children;
    var count = objchild.length;
        for(var i=0;i<objchild.length;i++)
        {
            var tempObj = objchild[i];
            if(tempObj.tagName=="INPUT" && tempObj.type == "checkbox")
        {
            tempObj.checked = false;
        }
            setChildUnChecked(tempObj);
        }
}
//设置节点的子节点cheched——该节点可以访问,则他的子节点也都能访问
function setChildChecked(divID)
{
    var objchild = divID.children;
    var count = objchild.length;
for(var i=0;i<objchild.length;i++)
{
    var tempObj = objchild[i];
    if(tempObj.tagName=="INPUT" && tempObj.type == "checkbox")
    {
    tempObj.checked = true;
    }
    setChildChecked(tempObj);
    }
}
//触发事件
function CheckEvent()
{
    var objNode = event.srcElement;
    if(objNode.tagName!="INPUT" || objNode.type!="checkbox")
    return;
    if(objNode.checked==true)
    {
        setParentChecked(objNode);
        var objID = objNode.getAttribute("ID");
        var objID = objID.substring(0,objID.indexOf("CheckBox"));
        var objParentDiv = document.getElementById(objID+"Nodes");
        if(objParentDiv==null || objParentDiv == "undefined")
            {
                return;
            }
        setChildChecked(objParentDiv);
        }
        else
        {
            var objID = objNode.getAttribute("ID");
            var objID = objID.substring(0,objID.indexOf("CheckBox"));
            var objParentDiv = document.getElementById(objID+"Nodes");
            if(objParentDiv==null || objParentDiv == "undefined")
        {
            return;
        }
            setChildUnChecked(objParentDiv);
    }
}
    </script>

cs代码如下:

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 System.Data.SqlClient;

public partial class rc_Dict : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.TreeView1.Attributes.Add("onclick", "CheckEvent()");
    }
    protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        if (e.Node.ChildNodes.Count == 0)
        {
            switch (e.Node.Depth)
            {
                case 0:
                    PopulateXmdept(e.Node);
                    break;
                case 1:
                    PopulateImuser(e.Node);
                    e.Node.Collapse();
                    break;
            }
        }
    }
    //获取部门组下的用户
    void PopulateImuser(TreeNode node)
    {
        SqlCommand sqlQuery = new SqlCommand();
        sqlQuery.CommandText = "Select USERTRUENAME,USERID From IMUSER " +
            " Where USERDEPT = @PTID";
        sqlQuery.Parameters.Add("@PTID", SqlDbType.VarChar).Value = node.Value;
        DataSet ResultSet = RunQuery(sqlQuery);
        if (ResultSet.Tables.Count > 0)
        {
            foreach (DataRow row in ResultSet.Tables[0].Rows)
            {

                TreeNode NewNode = new TreeNode();
                NewNode.Text = row["USERTRUENAME"].ToString();
                NewNode.Value = row["USERID"].ToString();
                NewNode.PopulateOnDemand = false;
                NewNode.SelectAction = TreeNodeSelectAction.None;
                node.ChildNodes.Add(NewNode);
            }
        }
    }
    //获取系统组
    void PopulateXmdept(TreeNode node)
    {
        SqlCommand sqlQuery = new SqlCommand(
            "Select PtName, PtID From XMDEPT Order by PtName");
        DataSet resultSet;
        resultSet = RunQuery(sqlQuery);
        if (resultSet.Tables.Count > 0)
        {
            foreach (DataRow row in resultSet.Tables[0].Rows)
            {
                TreeNode NewNode = new
                    TreeNode(row["PtName"].ToString(),
                    row["PtID"].ToString());
                NewNode.PopulateOnDemand = true;
                NewNode.SelectAction = TreeNodeSelectAction.Expand;
                node.ChildNodes.Add(NewNode);
            }
        }
    }
    private DataSet RunQuery(SqlCommand sqlQuery)
    {
        SqlConnection DBConnection = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["SqlClientString"]);
        SqlDataAdapter dbAdapter = new SqlDataAdapter();
        dbAdapter.SelectCommand = sqlQuery;
        sqlQuery.Connection = DBConnection;
        DataSet resultsDataSet = new DataSet();
        try
        {
            dbAdapter.Fill(resultsDataSet);
        }
        catch
        {
            Response.Write("Unable to connect to SQL Server.");
        }
        return resultsDataSet;
    }
    protected void TreeView1_Disposed(object sender, EventArgs e)
    {
        Response.Write("<script language='javascript'> alert('测试选择成功TreeView1_Disposed!');</script>");
    }
    protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
    {
        if (TreeView1.CheckedNodes.Count > 0)
        {
            //选择后激发Checked
            foreach (TreeNode node in TreeView1.CheckedNodes)
            {
                //Response.Write(node.Value + "<br>");
            }
        }
        else
        {
            //Response.Write("No items selected.");
        }
    }
    protected void ib_submit_Click(object sender, ImageClickEventArgs e)
    {
        string sWorkName = null;

        if (TreeView1.CheckedNodes.Count > 0)
        {
            // Clear the message label.
            //Response.Write("You selected: <br><br>");
            // Iterate through the CheckedNodes collection and display
            // the selected nodes.
            //选择后激发Checked
            foreach (TreeNode node in TreeView1.CheckedNodes)
            {
                if (node.Depth == 2)
                {
                    this.tb_Select.Text += node.Text + ";";
                }
            }
            Server.Transfer("rc_add.aspx", true);
        }
        else
        {
            Response.Write("<script language='javascript'> alert('没有选择人员!');</script>");
            this.tb_Select.Text = null;
        }

    }
}

【阅读 10968】 【评论 4】
评分统计
评论
【4楼】回复: 关于2.0中TreeView的带有CheckBox全选测试代码
行不行啊
发布者 billjoe  -  2008-04-09 5:09

【3楼】Reply: 关于2.0中TreeView的带有CheckBox全选测试代码
能不能把这两个页的全部代码贴出来?
发布者 ltx  -  2008-04-14 9:45

【2楼】回复: 关于2.0中TreeView的带有CheckBox全选测试代码
可否把数据绑定部分贴出来
发布者 .net  -  2008-05-29 4:49

【1楼】回复: 关于2.0中TreeView的带有CheckBox全选测试代码
发布者 asdfasdf  -  2011-05-03 4:36
发布评论
login
名称:
密码:(游客无须)
您的网络日志 URL(可选):
标题:
评论
验证码:
换一个


  
  Sign in    
添加表情