zoukankan      html  css  js  c++  java
  • TreeView数据绑定方法


    <%@ 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" onclick="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>




    aspx页面



    using System;
    using System.Data;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;



    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
    }


    CS页面

  • 相关阅读:
    SpringCloud项目中使用Nacos作为配置中心
    SpringCloud项目中使用Nacos作为注册中心
    idea创建maven多模块Spring Boot项目
    Java代码中对IP进行白名单验证
    idea配置jdk
    win10配置jdk1.8环境变量
    shell 提示符样式设置
    整数转换成中文读法的字符串
    比较三段式软件版本号大小
    Windows7安装 nginx+php 后访问.php文件出现 “No input file specified.” 的解决办法
  • 原文地址:https://www.cnblogs.com/shihao/p/1537633.html
Copyright © 2011-2022 走看看