TreeView控件数据绑定_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > TreeView控件数据绑定

TreeView控件数据绑定

 2013/7/31 17:09:11  蒋叶湖  博客园  我要评论(0)
  • 摘要:aspx页面代码<headrunat="server"><title>UntitledPage</title><scriptlanguage="javascript">functionchkAll(){//debugger;varchkall=document.all["chkall"];varchkother=document.getElementsByTagName("input");for(vari=0;i<chkother
  • 标签:view 数据 控件
   

aspx 页面代码

<head 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"
                        onselectednodechanged="TreeView1_SelectedNodeChanged" ></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.cs代码

  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();
            masterNode.Text = (string)masterRow["B_Name"];
            masterNode.Value = masterRow["B_Id"].ToString();
            TreeView1.Nodes.Add(masterNode);
            foreach (DataRow childRow in masterRow.GetChildRows("Children"))
            {
                TreeNode childNode =new TreeNode((string)childRow["s_name"]);
                masterNode.Expanded = false;
                masterNode.ChildNodes.Add(childNode);
            }
        }
    }

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

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

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

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

    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
      
        WanHePMS.Common.MessageBox.Show(this, this.TreeView1.SelectedNode.Value+"  --" +this.TreeView1.SelectedNode.Text);
    }

发表评论
用户名: 匿名