小D的TreeView数据绑定方法

 CS代码页面

public partial class Left : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindTree();
            //InitTree();
        }
    }

    #region 主从表绑定
    private void BindTree()
    {
        DataSet dst = GetTreeViewData();
        TreeView1.ShowCheckBoxes = TreeNodeTypes.All;
        foreach (DataRow masterRow in dst.Tables["province"].Rows)
        {
            TreeNode masterNode = new TreeNode((string)masterRow["province"]);
            TreeView1.Nodes.Add(masterNode);
            foreach (DataRow childRow in masterRow.GetChildRows("Children"))
            {
                TreeNode childNode =new TreeNode((string)childRow["city"]);
                masterNode.Expanded = false;
                masterNode.ChildNodes.Add(childNode);
            }
        }
    }

    private DataSet GetTreeViewData()
    {
        string constring = System.Configuration.ConfigurationSettings.AppSettings["ConnectionStr"];
        SqlConnection con = new SqlConnection(constring);
        SqlDataAdapter daprovince = new SqlDataAdapter("SELECT * FROM province", con);
        SqlDataAdapter dacity = new SqlDataAdapter("SELECT * FROM city", con);
        DataSet ds = new DataSet();
        daprovince.Fill(ds, "province");
        dacity.Fill(ds, "city");
        ds.Relations.Add("Children", ds.Tables["province"].Columns["provinceid"], ds.Tables["city"].Columns["father"]);
        return ds;
    }
    #endregion

    #region 递归绑定同一个表数据
    private void InitTree()
    {
        DataTable dt = GetTreeViewTable();
        DataView dv = new DataView(dt);
        dv.RowFilter = "ParentID=0";
        TreeView1.ShowCheckBoxes = TreeNodeTypes.All;
        foreach (DataRowView drv in dv)
        {
            TreeNode node = new TreeNode();
            node.Text = drv["text"].ToString();
            node.Value = drv["ID"].ToString();
            node.Expanded = false;
            TreeView1.Nodes.Add(node);
            AddReplies(dt,node);
        }
    }

    private DataTable GetTreeViewTable()
    {
        string constring = System.Configuration.ConfigurationSettings.AppSettings["ConnectionStr"];
        SqlConnection con = new SqlConnection(constring);
        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM treeview", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }

    private void AddReplies(DataTable dt, TreeNode node)
    {
        DataView dv = new DataView(dt);
        dv.RowFilter = "ParentID='" + node.Value + "'";
        foreach (DataRowView row in dv)
        {
            TreeNode replyNode = new TreeNode();
            replyNode.Text = row["text"].ToString();
            replyNode.Value = row["ID"].ToString();
            replyNode.Expanded = false;
            node.ChildNodes.Add(replyNode);
            AddReplies(dt,replyNode);
        }
    }
    #endregion
}

 

 

HTML页面

 

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <script language="javascript">
        function chkAll()
        {
            //debugger;
            var chkall= document.all["chkall"];
            var chkother= document.getElementsByTagName("input");
            for (var i=0;i<chkother.length;i++)
            {
                if( chkother[i].type=='checkbox')
                {
                    if(chkother[i].id.indexOf('TreeView1')>-1)
                    {
                        if(chkall.checked==true)
                        {
                            chkother[i].checked=true;
                        }
                        else
                        {
                            chkother[i].checked=false;
                        }
                    }
                }
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
         <table width=100% height=100%>
            <tr height=10>
                <td><input id="chkall" type="checkbox" οnclick="chkAll();" />全选/取消</td>
                <td><asp:Button ID="Button1" runat="server" Text="Button" /></td>
            </tr>
            <tr valign=top>
                <td><asp:TreeView ID="TreeView1" runat="server" ></asp:TreeView></td>
                <td><iframe id=fMain style="BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BORDER-BOTTOM-STYLE: none"
                            src="" frameBorder="0" width="100%" scrolling="yes" height="100%"></iframe></td>
            </tr>
            <tr height=10>
                <td></td>
            </tr>
        </table>

    </form>
</body>
</html>

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值