.net之旅-树型结构及相关功能的实现(41)

    技术2022-05-11  119

     

    树型结构一直是我们常用的结构。本文将详细具体的对其进行实现。

    数据库是SQL server 2000。

    webconfig的数据库配置内容:

      <appSettings>    <add key="DatabaseConnStr" value="server=localhost;uid=sa;pwd=123456;database=infoFlat" />  </appSettings>

    其中的数据库设计:

    sysFunction表:

       名称 类型 长度 说明

    1 FunctionId int 4 00 FunctionName varchar 50 00 ParentFunctionId int 4 0

    内容如下:

     1 系统功能 0 2 信息中心 1 3 新闻 2 4 通知 2 5 公告 2 6 项目中心 1 7 申报 6 8 审批 6 9 过程 6 10 结题 6 11 产品中心 1 12 在研产品 11   

    具体代码:

    1 file:///H:/infoFlat/InterFace/Left.aspx 树型结构页面

    <% @ Page Language="C#" AutoEventWireup="true" CodeFile="Left.aspx.cs" Inherits="InterFace_Left"  %> <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > < html  xmlns ="http://www.w3.org/1999/xhtml"   > < head  runat ="server" >      < title > 功能列表 </ title > </ head > < body >      < form  id ="formFuncList"  runat ="server" >           < div >              < asp:Label  ID ="LabelUserId"  runat ="server"  Text ="Label" ></ asp:Label >               < br  />               < asp:TreeView  ID ="TreeViewFuncList"  runat ="server"  ShowLines ="True" >               </ asp:TreeView >           </ div >      </ form > </ body > </ html >

     2 file:///H:/infoFlat/App_Code/Database/DbConn.cs 数据库连接

    /// ************************************************************/// Copyright (C), 2006-2007, GUET./// FileName: Index.aspx.cs/// Author: longronglin/// Version : 1.0/// Date: 2007-01-23/// Description:      /// Function List:   ///     1. ///     2. ///     3.  /// History:      ///      <author> <time> <version> <desc>///      longronglin    2007/01/23     1.0      modify xxx . /// ************************************************************* using  System; using  System.Data; using  System.Configuration; 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; using  System.Data.SqlClient; namespace  infoFlat.App_Code.Database {    /// <summary>    /// Summary description for DbConn    /// </summary>    public class DbConn : IDisposable    {        // 数据库连接        private static SqlConnection conn;        public DbConn()        {            //        }        /// <summary>        /// 获取数据库连接并打开        /// </summary>        public static SqlConnection GetConn()        {            // 获取数据库连接            if (conn == null)            {                // DatabaseConnStr为web.config对应的appSettings设置                conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["DatabaseConnStr"]);            }            // 打开数据库连接            if (conn.State == System.Data.ConnectionState.Closed)            {                conn.Open();            }             return conn;        }        /// <summary>        /// 释放资源        /// </summary>        public void Dispose()        {            // 确认连接是否已经关闭            if (conn != null)            {                conn.Dispose();                conn = null;            }        }    }}

    3 file:///H:/infoFlat/InterFace/Left.aspx.cs Left对应的代码:

    /// ************************************************************/// Copyright (C), 2006-2007, GUET./// FileName: Index.aspx.cs/// Author: longronglin/// Version : 1.0/// Date: 2007-01-24/// Description:      /// Function List:   ///     1. void Page_Load()///     2. ///     3. /// History:      ///      <author> <time> <version> <desc>///      longronglin    2007/01/24     1.0      modify xxx . /// ************************************************************* using  System; using  System.Data; 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; using  System.Data.SqlClient; using  infoFlat.App_Code.Database; public   partial   class  InterFace_Left : System.Web.UI.Page {    protected void Page_Load(object sender, EventArgs e)    {        // 获取Session中的用户ID        String userId = Convert.ToString(Session["userId"]);        LabelUserId.Text = userId;        // 初始化功能树        InitFuncTree();        AllOverTree(TreeViewFuncList.Nodes);    }    private void InitFuncTree()    {                 SqlConnection conn = DbConn.GetConn();        SqlDataAdapter adp = new SqlDataAdapter("select * from sysFunction", conn);        DataSet ds = new DataSet();        try        {            adp.Fill(ds);        }        catch (Exception ex)        {            ex.ToString();        }        finally        {                conn.Close();        }        this.ViewState["ds"= ds;        AddTree(0, (TreeNode)null);    }    //递归添加树的节点    private void AddTree(int ParentFunctionId, TreeNode pNode)    {        DataSet ds = (DataSet)this.ViewState["ds"];        DataView dvTree = new DataView(ds.Tables[0]);        //过滤ParentFunctionId,得到当前的所有子节点        dvTree.RowFilter = "[ParentFunctionId] = " + ParentFunctionId;        foreach (DataRowView Row in dvTree)        {            TreeNode Node = new TreeNode();            //添加根节点            if (pNode == null)            {                   Node.Text = Row["FunctionName"].ToString();                TreeViewFuncList.Nodes.Add(Node);                Node.Expanded = true;                //从根节点开始递归                AddTree(Int32.Parse(Row["FunctionId"].ToString()), Node);                }            else            {                   //̀添加当前节点的子节点                Node.Text = Row["FunctionName"].ToString();                                pNode.ChildNodes.Add(Node);                Node.Expanded = true;                //再次递归                AddTree(Int32.Parse(Row["FunctionId"].ToString()), Node);                  }        }    }        protected  void  AllOverTree(TreeNodeCollection tnc)       {           foreach(TreeNode node in tnc)           {            if (node.ChildNodes.Count != 0)            {                AllOverTree(node.ChildNodes);            }            else            //在叶子节点设置其NavigateUrl               {                setNavigateUrl(node);                //node.NavigateUrl = "~/InterFace/FunctionChoose.aspx?" + node.Text;            }        }       }    private void setNavigateUrl(TreeNode node)    {        if (node.Text.Equals("新闻"))        {            node.NavigateUrl = "~/InterFace/News.aspx";        }        if (node.Text.Equals("通知"))        {            node.NavigateUrl = "~/InterFace/Notice.aspx";        }        else        {            node.NavigateUrl = "~/InterFace/Others.aspx";        }    }}

    测试通过:

               本来打算贴图的可是传不上。遗憾!代码中有些是Session的内容是用来做权限控制的,这里用不到。

    网页显示如下:

     

    系统功能 信息中心 新闻 通知 公告 项目中心 申报 审批 过程 结题 产品中心 在研产品

    最新回复(0)