实现一个可以动态改变的N层导航树

本文介绍了一种使用web.config文件存储导航树信息的方法,并通过C#代码实现了导航树的读取与展示。利用组合模式设计目录信息类,实现动态生成网站导航结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原理:

将导航树信息存储在web.config文件中,程序获取存储在web.config中导航树的信息并生成相应的导航结构。

代码:

using System; using System.Collections; using System.Collections.Generic; /// <summary> /// Category 的摘要说明 /// </summary> public class Category { private string _Name; private string _Value; private List<Category> _ChildCategoryCollection; public Category() { _ChildCategoryCollection = new List<Category>(); } public Category(string name,string value):this() { _Name = name; _Value = value; } public string Name { get { return _Name; } } public string Value { get { return _Value; } } public List<Category> ChildCategoryCollection { get { return _ChildCategoryCollection; } } public void AddChildCategory(Category child) { _ChildCategoryCollection.Add(child); } }

目录信息类(组合模式)

using System; using System.Configuration; using System.Collections.Generic; using System.Collections; using System.Xml; /// <summary> /// NavigateHandler 的摘要说明 /// </summary> public class NavigateHandler:IConfigurationSectionHandler { public NavigateHandler() { // // TODO: 在此处添加构造函数逻辑 // } #region IConfigurationSectionHandler 成员 public object Create(object parent, object configContext, System.Xml.XmlNode section) { XmlNodeList nodes = section.SelectNodes("link"); if (nodes.Count == 0) return null; Category normalUserRootCategory = new Category("root", "root");//创建根目录 foreach (XmlNode n in nodes) { string pname, pvalue; pname = n.Attributes["name"].Value; pvalue = n.Attributes["value"].Value; Category parentCategory = new Category(pname, pvalue);//创建主目录 normalUserRootCategory.AddChildCategory(parentCategory); XmlNodeList cnodes = n.SelectNodes("clink"); foreach (XmlNode cn in cnodes) { string cname, cvalue; cname = cn.Attributes["name"].Value; cvalue = cn.Attributes["value"].Value; Category childCategory = new Category(cname, cvalue);//创建主目录下的子目录 parentCategory.AddChildCategory(childCategory); } } return normalUserRootCategory.ChildCategoryCollection; } #endregion public static List<Category> GetCategoryList() { return (List<Category>)ConfigurationManager.GetSection("Navigate"); } }

读取web.config文件中的信息并生成相应的导航结构

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> <mce:style type="text/css"><!-- .header{ width:200px; height:30px; background-color:gray; border:solid 1px back; font-size:large; text-align:center; margin:2px 0; line-height:30px; } .content{} --></mce:style><style type="text/css" mce_bogus="1"> .header{ width:200px; height:30px; background-color:gray; border:solid 1px back; font-size:large; text-align:center; margin:2px 0; line-height:30px; } .content{}</style> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" /> <div> <cc1:Accordion ID="Accordion1" runat="server" HeaderCssClass="header" ContentCssClass="content"> <HeaderTemplate><%#Eval("Name") %></HeaderTemplate> <ContentTemplate> <asp:BulletedList runat="server" ID="childcategory" DataSource='<%#Eval("ChildCategoryCollection") %>' DataTextField="Name" DataValueField="Value" BulletStyle="Square"> </asp:BulletedList> </ContentTemplate> </cc1:Accordion> </div> </form> </body> </html>

ASPX文件代码

<?xml version="1.0"?> <configuration> <configSections> <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/> <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere"/> <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/> <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/> </sectionGroup> </sectionGroup> </sectionGroup> <section name="Navigate" type="NavigateHandler,App_Code"/> </configSections> <system.web> <pages> <controls> <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </controls> </pages> <!-- Set compilation debug="true" to insert debugging symbols into the compiled page. Because this affects performance, set this value to true only during development. --> <compilation debug="true"> <assemblies> <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> <add assembly="System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> <add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies> </compilation> <httpHandlers> <remove verb="*" path="*.asmx"/> <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/> </httpHandlers> <httpModules> <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </httpModules> </system.web> <system.web.extensions> <scripting> <webServices> <!-- Uncomment this line to customize maxJsonLength and add a custom converter --> <!-- <jsonSerialization maxJsonLength="500"> <converters> <add name="ConvertMe" type="Acme.SubAcme.ConvertMeTypeConverter"/> </converters> </jsonSerialization> --> <!-- Uncomment this line to enable the authentication service. Include requireSSL="true" if appropriate. --> <!-- <authenticationService enabled="true" requireSSL = "true|false"/> --> <!-- Uncomment these lines to enable the profile service. To allow profile properties to be retrieved and modified in ASP.NET AJAX applications, you need to add each property name to the readAccessProperties and writeAccessProperties attributes. --> <!-- <profileService enabled="true" readAccessProperties="propertyname1,propertyname2" writeAccessProperties="propertyname1,propertyname2" /> --> </webServices> <!-- <scriptResourceHandler enableCompression="true" enableCaching="true" /> --> </scripting> </system.web.extensions> <system.webServer> <validation validateIntegratedModeConfiguration="false"/> <modules> <add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </modules> <handlers> <remove name="WebServiceHandlerFactory-Integrated"/> <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </handlers> </system.webServer> <Navigate> <link name="主栏目1" value="主栏目1"> <clink name="子栏目1" value="子栏目1"></clink> <clink name="子栏目2" value="子栏目2"></clink> <clink name="子栏目3" value="子栏目3"></clink> </link> <link name="主栏目2" value="主栏目2"> <clink name="子栏目1" value="子栏目1"></clink> <clink name="子栏目2" value="子栏目2"></clink> <clink name="子栏目3" value="子栏目3"></clink> </link> <link name="主栏目3" value="主栏目3"> <clink name="子栏目1" value="子栏目1"></clink> <clink name="子栏目2" value="子栏目2"></clink> <clink name="子栏目3" value="子栏目3"></clink> </link> <link name="主栏目4" value="主栏目4"> <clink name="子栏目1" value="子栏目1"></clink> <clink name="子栏目2" value="子栏目2"></clink> <clink name="子栏目3" value="子栏目3"></clink> </link> <link name="主栏目5" value="主栏目5"> <clink name="子栏目1" value="子栏目1"></clink> <clink name="子栏目2" value="子栏目2"></clink> <clink name="子栏目3" value="子栏目3"></clink> </link> <link name="主栏目6" value="主栏目6"> <clink name="子栏目1" value="子栏目1"></clink> <clink name="子栏目2" value="子栏目2"></clink> <clink name="子栏目3" value="子栏目3"></clink> </link> </Navigate> </configuration>

web.config文件代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值