自带TreeView数据绑定,checkbox选择无刷新联动,[asp.net基础]

TreeView,导航控件,用处大大滴。vs2010自带TreeView,数据绑定,checkbox选择后联动示例,支持各浏览器。


先来个不绑定数据的TreeView:

<asp:TreeView ID="TreeView1" runat="server" ShowLines="True">
            <Nodes>
                <asp:TreeNode Text="新建节点1" Value="新建节点">
                    <asp:TreeNode Text="新建节点1-1" Value="新建节点1-1"></asp:TreeNode>
                    <asp:TreeNode Text="新建节点1-2" Value="新建节点1-2"></asp:TreeNode>
                </asp:TreeNode>
                <asp:TreeNode Text="新建节点2" Value="新建节点2">
                    <asp:TreeNode Text="新建节点2-1" Value="新建节点2-1">
                        <asp:TreeNode Text="新建节点2-1-1(打开布布分享)" Value="新建节点2-1-1" NavigateUrl="http://tech.bubufx.com/"
                            Target="_blank"></asp:TreeNode>
                        <asp:TreeNode Text="新建节点2-1-2(js脚本)" Value="新建节点2-1-2" NavigateUrl="javascript:alert(’hello!’);">
                        </asp:TreeNode>
                    </asp:TreeNode>
                    <asp:TreeNode Text="新建节点2-2" Value="新建节点2-2" ShowCheckBox="true"></asp:TreeNode>
                </asp:TreeNode>
            </Nodes>
        </asp:TreeView>
业务系统,TreeView是最常用的控件。带checkbox,选择上一级节点,子节点全部选中,选中子节点,上级也选中,且联动无刷新。下面的例子可以直接复制到新建的页面内运行。

图示:

自带TreeView数据绑定,checkbox选择无刷新联动,[asp.net基础]

bubufx_treeview.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="bubufx_treeview.aspx.cs"
    Inherits="bubufx_treeview" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" language="javascript">
        function postBackByObject(event) {
            var o = (window.event) ? window.event.srcElement : event.target;
            if (o.nodeName == "INPUT" && o.type == "checkbox") {
                __doPostBack("", "");
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="TreeView2" runat="server" ShowLines="True" ShowCheckBoxes="All"
            OnTreeNodeCheckChanged="TreeView2_TreeNodeCheckChanged">
        </asp:TreeView>
        <asp:Button ID="Button1" runat="server" Text="获得选中" onclick="Button1_Click" />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>
bubufx_treeview.aspx.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

public partial class bubufx_treeview : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //
        TreeView2.EnableClientScript = true;
        TreeView2.Attributes.Add("onclick", "postBackByObject(event)");
        //

        if (!Page.IsPostBack)
        {
            string sql = "";
            string connstr = System.Configuration.ConfigurationManager.AppSettings["DataSQL"];

            DB cDB = new DB();

            sql = "select * from mztree";

            DataTable DT = cDB.RunSQLGetDT(sql, connstr);

            if (DT.Rows.Count > 0)
            {
                nodeBinding(TreeView2, DT, "0", null);
            }
        }
    }


    public void nodeBinding(TreeView _treeView, DataTable _dt, string _filter, TreeNode _pNode)
    {
        //循环构建树
        string sFilter = "pid=" + _filter;
        TreeNode parentNode = _pNode;

        DataView dv = new DataView(_dt);

        dv.RowFilter = sFilter;

        if (dv.Count > 0)
        {
            foreach (DataRowView drv in dv)
            {

                TreeNode tempNode = new TreeNode();

                tempNode.Text = drv["name"].ToString();
                tempNode.Value = drv["ID"].ToString();
                //tempNode.NavigateUrl = "bubufx_tech.aspx?c=" + drv["ID"].ToString();
                //tempNode.Target = "bubufxIframe";

                
                if (parentNode != null)
                    parentNode.ChildNodes.Add(tempNode);
                else
                    _treeView.Nodes.Add(tempNode);


                nodeBinding(_treeView, _dt, drv["ID"].ToString(), tempNode);

            }
        }
    }



    protected void Button1_Click(object sender, EventArgs e)
    {
        string str = getCheckedNodeValue(TreeView2.Nodes,",");


        Label1.Text = str;
    }

    public string getCheckedNodeValue(TreeNodeCollection aNodes,string chuan)
    {
        string str = "";

        for (int i = 0; i < aNodes.Count; i++)
        {

            if (aNodes[i].Checked == true)
            {
                str = str + aNodes[i].Value + chuan;
            }

            if (aNodes[i].ChildNodes.Count > 0)
            {
                str = str + getCheckedNodeValue(aNodes[i].ChildNodes, chuan);
            }
        }

        return str;
    }


    protected void TreeView2_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
    {
        RecursiveCheckCheckBox(e.Node, e.Node.Checked, true);
        TreeNode current = e.Node;
        while (!current.Checked && current.Parent != null && !IsChildNodesChecked(current.Parent))
        {
            current.Parent.Checked = false;
            current = current.Parent;
        }
        if (e.Node.Checked)
        {
            RecursiveCheckCheckBox(e.Node, e.Node.Checked, false);
        }


    }

    private bool IsChildNodesChecked(TreeNode n)
    {
        bool result = false;
        if (n.ChildNodes.Count == 0)
        {
            result = n.Checked;
        }
        foreach (TreeNode t in n.ChildNodes)
        {
            if (result |= t.Checked)
            {
                break;
            }
        }
        return result;
    }

    private void RecursiveCheckCheckBox(TreeNode n, bool isChecked, bool topDown)
    {
        if (topDown)
        {
            foreach (TreeNode l in n.ChildNodes)
            {
                l.Checked = isChecked;
                if (l.ChildNodes.Count != 0)
                {
                    RecursiveCheckCheckBox(l, isChecked, topDown);
                }
            }
        }
        else
        {
            n.Checked = isChecked;
            if (n.Parent != null)
            {
                RecursiveCheckCheckBox(n.Parent, isChecked, false);
            }
        }
    }

}


自带TreeView数据绑定,checkbox选择无刷新联动,[asp.net基础]

上一篇:HA高可用配置


下一篇:3DMAX制作镂空装饰瓶建模全过程