asp.net2.0控件treeview绑定数据以及全选操作

//html页面

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


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<script>
function postBackObject()
{  
    var o = window.event.srcElement;
    if (o.tagName == "INPUT" && o.type == "checkbox") //点击treeview的checkbox是触发
    {
              __doPostBack("","");
     }
}
</script>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="TreeView1" runat="server" PopulateNodesFromClient="False" onclick="postBackObject()" ShowCheckBoxes="All" OnTreeNodeCheckChanged="TreeView1_TreeNodeCheckChanged">
        </asp:TreeView>
   
    </div>
    </form>
</body>
</html>

//后台代码

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

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataTable dt1 = SqlDAL.department.listProvince();
            foreach (DataRow row1 in dt1.Rows)
            {
                TreeNode tn = new TreeNode();
                tn.Text = row1["RP_Name"].ToString();
                tn.Value = row1["RP_Id"].ToString();
                this.TreeView1.Nodes.Add(tn);
                DataTable dt2 = SqlDAL.department.listCity(Convert.ToInt32(row1["RP_Id"]));
                foreach (DataRow row2 in dt2.Rows)
                {
                    TreeNode tn2 = new TreeNode();
                    tn2.Text = row2["RC_Name"].ToString();
                    tn2.Value = row2["RC_Id"].ToString();
                    tn.ChildNodes.Add(tn2);
                    DataTable dt3 = SqlDAL.department.listContry(Convert.ToInt32(row2["RC_Id"]));
                    foreach (DataRow row3 in dt3.Rows)
                    {
                        TreeNode tn3 = new TreeNode();
                        tn3.Text = row3["RCY_Name"].ToString();
                        tn3.Value = row3["RCY_Id"].ToString();
                        tn2.ChildNodes.Add(tn3);
                    }
                }
            }
        }
    }
    protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
    {
        SetChildChecked(e.Node);
    }

    private void SetChildChecked(TreeNode parentNode)
    {
        foreach (TreeNode tn in parentNode.ChildNodes)
        {
            tn.Checked = parentNode.Checked;
            if(tn.ChildNodes.Count>0)
            {
                SetChildChecked(tn);
            }
        }
    }
}

 

//获取最末节点的值

protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < this.TreeView1.Nodes.Count; i++)
        {
            if (TreeView1.Nodes[i].Checked == true)
            {
                for (int j = 0; j < this.TreeView1.Nodes[i].ChildNodes.Count; j++)
                {
                    if (this.TreeView1.Nodes[i].ChildNodes[j].Checked == true)
                    {
                        for (int k = 0; k < this.TreeView1.Nodes[i].ChildNodes[j].ChildNodes.Count; k++)
                        {
                            if (this.TreeView1.Nodes[i].ChildNodes[j].ChildNodes[k].Checked == true)
                            {
                                Response.Write(this.TreeView1.Nodes[i].ChildNodes[j].ChildNodes[k].Value.ToString());
                            }
                        }
                    }
                }
            }
        }              
    }

你可能感兴趣的:(treeview)