zoukankan      html  css  js  c++  java
  • 动态绑定treeview的方法

     前台代码 注意 一定要加上Target="contentFrame"否则跳转的时候 跳不到内容框架
    Page Language="C#" AutoEventWireup="true" CodeFile="Left.aspx.cs" Inherits="Left" %>
    
    <html>
    <head id="Head1" runat="server">
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
        <link href="../Style/Style.css" rel="stylesheet" type="text/css" />
    </head>
    <body leftmargin="0" topmargin="0" bgcolor="#f7faff">
        <form id="form1" runat="server">
        <table width="200" border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td>
                    <table cellpadding="0" cellspacing="0" border="0" style="color: White; font-family: 宋体, Arial;
                        font-size: 12px;">
                        <tr>
                            <td align="left" background="../images/czy.png" width="200" height="30">
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;登录人:&nbsp;<asp:Label
                                    ID="lbLoginUser" runat="server"></asp:Label>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
        <table width="200" border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td align="left" valign="top">
                    <div style="height: 480px; overflow:scroll; scrollbar-face-color: #DBEBFE; scrollbar-shadow-color: #B8D6FA;
                        scrollbar-highlight-color: #FFFFFF; scrollbar-3dlight-color: #DBEBFE; scrollbar-darkshadow-color: #458CE4;
                        scrollbar-track-color: #FFFFFF; scrollbar-arrow-color: #458CE4">
                        <asp:TreeView ID="tvMenu1" runat="server" EnableViewState="False" BackColor="#f7faff"
                            ForeColor="Black" ShowLines="True" Target="contentFrame"  ExpandDepth="1">
     </form>
    </body>
    </html>
    后台代码
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Configuration;
    using System.Data;
    using System.IO;
    using NetBuild.Common;
    using NetBuild.Models;
    using NetBuild.Bussiness;
    using System.Linq;
    public partial class Left : System.Web.UI.Page
    {
    
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
    
            if (!IsPostBack)
            {
                TreeNode tmpNode;
                lbLoginUser.Text = Session["LoginName"].ToString();
    
    
                AddTreeNode(-1, (System.Web.UI.WebControls.TreeNode)null);
            }
            
            
            
            }
        
         public DataTable bind()
        {
            string str1 = "select * from Common_Menu  ";
            DataSet myds = new DataSet();
            myds = SQLHelper.GetDataSet(str1);
            return myds.Tables[0];
    
    
    
        }
         protected void AddTreeNode(int ParentMenuID, System.Web.UI.WebControls.TreeNode pNode)
         {
    
             DataTable dt = bind();
    
             DataView dv = new DataView(dt);
    
             //过滤parentId,得到当前节点的所有子节点
    
             dv.RowFilter = "ParentMenuID=" + ParentMenuID;
             //foreach (DataRow b in dt.Rows)
             //{
             //    ParentMenuID = Convert.ToInt32(b["ParentMenuID"].ToString());
             
             //}
    
             foreach (DataRowView drv in dv)
             {
    
                 if (ParentMenuID == -1)
                 {
                     System.Web.UI.WebControls.TreeNode tn1 = new System.Web.UI.WebControls.TreeNode();
    
                     tn1.Text = drv["MenuName"].ToString(); //节点上要显示的名称
                     tn1.Value = drv["MenuID"].ToString();
                     tn1.ImageUrl = drv["Icon"].ToString();
    
                     tn1.NavigateUrl = drv["MenuUrl"].ToString();  //点击节点名称,跳转到指定url页面
    
                     tvMenu1.Nodes.Add(tn1); //将根节点加入到TreeView中去
    
                     tn1.Expanded = true;
                     //tn1.SelectAction = TreeNodeSelectAction.Expand;
                     tn1.ShowCheckBox = false;
    
    
    
    
                     //递归调用
    
                     AddTreeNode(Int32.Parse(drv["MenuID"].ToString()), tn1);
    
    
    
                 }
    
                 else
                 {
    
                     System.Web.UI.WebControls.TreeNode tn2 = new System.Web.UI.WebControls.TreeNode();
    
                     tn2.Text = drv["MenuName"].ToString();
                     tn2.Value = drv["MenuID"].ToString();
    
                     tn2.ImageUrl = drv["Icon"].ToString();
                     tn2.NavigateUrl = drv["MenuUrl"].ToString();
    
                     pNode.ChildNodes.Add(tn2);
                     tn2.ShowCheckBox = false;
    
                     tn2.Expanded = true;
                     //递归调用
    
                     AddTreeNode(Int32.Parse(drv["MenuID"].ToString()), tn2);
    
    
                 }
    
             }
    
         }
    
        }
    

      

  • 相关阅读:
    【知了堂学习笔记】java 正则表达式
    【知了堂学习笔记】java 接口与抽象类
    【知了堂学习笔记】java web 简单的登录
    【知了堂学习笔记】java IO流归纳总结
    【知了堂学习笔记】java 自定义异常
    【知了堂学习笔记】java 方法重载与重写的归纳
    编译链接符号重定位流程简述
    项目用到了lua的哪些部分
    lua协程实现简析
    杂乱无章
  • 原文地址:https://www.cnblogs.com/haihang/p/2701654.html
Copyright © 2011-2022 走看看