在web.config存储自定义的对象友情链接

本文涉及的产品
实时计算 Flink 版,5000CU*H 3个月
简介:

 web.config里:

 

 
  1. <configSections> 
  2. .. 
  3.    <section name="friendLinks" type="JiaYiSoftSite.App_Code.FriendLinks,JiaYiSoftSite,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" /> 
  4.  </configSections> 

 

 
  1. <friendLinks> 
  2.   <links> 
  3.     <add id="lnk1" href="#" text="关于本站" /> 
  4.     <add id="lnk2" href="#" text="版权声明" /> 
  5.     <add id="lnk3" href="#" text="活动公告" /> 
  6.     <add id="lnk4" href="#" text="站点地图" /> 
  7.     <add id="lnk5" href="#" text="合作伙伴" /> 
  8.   </links> 
  9. </friendLinks> 

FriendLinks.cs:

 

 
  1. namespace JiaYiSoftSite.App_Code 
  2.     //自定义友情链接元素 
  3.     public class FriendLinkElement : ConfigurationElement 
  4.     { 
  5.         public FriendLinkElement() 
  6.         { 
  7.         } 
  8.  
  9.         [ConfigurationProperty("id", IsRequired = true)] 
  10.         public string ID 
  11.         { 
  12.             get { return (string)this["id"]; } 
  13.             set { this["id"] = value; } 
  14.         } 
  15.  
  16.         //超链接地址 
  17.         [ConfigurationProperty("href", IsRequired = true)] 
  18.         public string Href 
  19.         { 
  20.             get { return (string)this["href"]; } 
  21.             set { this["href"] = value; } 
  22.         } 
  23.  
  24.         //超链接文本 
  25.         [ConfigurationProperty("text", IsRequired = true)] 
  26.         public string Text 
  27.         { 
  28.             get { return (string)this["text"]; } 
  29.             set { this["text"] = value; } 
  30.         } 
  31.     } 
  32.  
  33.     //自定义元素集合 
  34.     public class FriendLinkElementCollection : ConfigurationElementCollection 
  35.     { 
  36.         public FriendLinkElementCollection() 
  37.         { 
  38.             FriendLinkElement myElement = (FriendLinkElement)CreateNewElement(); 
  39.             Add(myElement); 
  40.         } 
  41.  
  42.         public void Add(FriendLinkElement friendLinkElement) 
  43.         { 
  44.             BaseAdd(friendLinkElement); 
  45.         } 
  46.  
  47.         protected override void BaseAdd(ConfigurationElement element) 
  48.         { 
  49.             base.BaseAdd(element, false); 
  50.         } 
  51.  
  52.         public override ConfigurationElementCollectionType CollectionType 
  53.         { 
  54.             get 
  55.             { 
  56.                 return ConfigurationElementCollectionType.AddRemoveClearMap; 
  57.             } 
  58.         } 
  59.  
  60.         protected override ConfigurationElement CreateNewElement() 
  61.         { 
  62.             return new FriendLinkElement(); 
  63.         } 
  64.  
  65.         protected override object GetElementKey(ConfigurationElement element) 
  66.         { 
  67.             return ((FriendLinkElement)element).ID; 
  68.         } 
  69.  
  70.         public FriendLinkElement this[int Index] 
  71.         { 
  72.             get 
  73.             { 
  74.                 return (FriendLinkElement)BaseGet(Index); 
  75.             } 
  76.             set 
  77.             { 
  78.                 if (BaseGet(Index) != null
  79.                 { 
  80.                     BaseRemoveAt(Index); 
  81.                 } 
  82.                 BaseAdd(Index, value); 
  83.             } 
  84.         } 
  85.  
  86.         new public FriendLinkElement this[string Name] 
  87.         { 
  88.             get 
  89.             { 
  90.                 return (FriendLinkElement)BaseGet(Name); 
  91.             } 
  92.         } 
  93.  
  94.         public int indexof(FriendLinkElement element) 
  95.         { 
  96.             return BaseIndexOf(element); 
  97.         } 
  98.  
  99.         public void Remove(FriendLinkElement link) 
  100.         { 
  101.             if (BaseIndexOf(link) >= 0) 
  102.                 BaseRemove(link.ID); 
  103.         } 
  104.  
  105.         public void RemoveAt(int index) 
  106.         { 
  107.             BaseRemoveAt(index); 
  108.         } 
  109.  
  110.         public void Remove(string name) 
  111.         { 
  112.             BaseRemove(name); 
  113.         } 
  114.  
  115.         public void Clear() 
  116.         { 
  117.             BaseClear(); 
  118.         } 
  119.     } 
  120.  
  121.     //自定义友情链接节 
  122.     class FriendLinks : ConfigurationSection 
  123.     { 
  124.         FriendLinkElement link; 
  125.         public FriendLinks() 
  126.         { 
  127.             link = new FriendLinkElement(); 
  128.         } 
  129.  
  130.         //链接节属性-集合 
  131.         [ConfigurationProperty("links", IsDefaultCollection = false)] 
  132.         [ConfigurationCollection(typeof(FriendLinkElementCollection), AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")] 
  133.         public FriendLinkElementCollection Links 
  134.         { 
  135.             get 
  136.             { 
  137.                 return (FriendLinkElementCollection)base["links"]; 
  138.             } 
  139.         } 
  140.     } 
  141.  

demo:读取

 

 
  1. //友情链接 
  2. FriendLinks flinks = (FriendLinks)ConfigurationManager.GetSection("friendLinks"); 
  3. ArrayList arr = new ArrayList(); 
  4. foreach (FriendLinkElement link in flinks.Links) 
  5.     if (link.ID != null && link.ID != ""
  6.     { 
  7.         arr.Add(link); 
  8.     } 
  9. rptFLinks.DataSource = arr; 
  10. rptFLinks.DataBind(); 

后台修改保存:

我这里用了个笨法子,清掉,重新添加的:

 

 
  1. Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); 
  2. if (!string.IsNullOrEmpty(Request.Form["cntLinks"])) 
  3.     FriendLinks flinks = (FriendLinks)config.GetSection("friendLinks"); 
  4.     int cnt = int.Parse(Request.Form["cntLinks"]); 
  5.     if (cnt > 0) flinks.Links.Clear(); 
  6.     for (int i = 1; i <= cnt; i++) 
  7.     { 
  8.         FriendLinkElement el = new FriendLinkElement(); 
  9.         el.ID = "lnk"+i; 
  10.         el.Href = Request.Form["lnk_url" + i]; 
  11.         el.Text = Request.Form["lnk_text" + i]; 
  12.         flinks.Links.Add(el); 
  13.     } 
  14. ); 

 

 本文转自 xcf007 51CTO博客,原文链接:http://blog.51cto.com/xcf007/566165,如需转载请自行联系原作者

相关实践学习
基于Hologres轻松玩转一站式实时仓库
本场景介绍如何利用阿里云MaxCompute、实时计算Flink和交互式分析服务Hologres开发离线、实时数据融合分析的数据大屏应用。
Linux入门到精通
本套课程是从入门开始的Linux学习课程,适合初学者阅读。由浅入深案例丰富,通俗易懂。主要涉及基础的系统操作以及工作中常用的各种服务软件的应用、部署和优化。即使是零基础的学员,只要能够坚持把所有章节都学完,也一定会受益匪浅。
相关文章
|
3月前
|
存储 移动开发 JSON
H5学习之路之Web存储解决方案
H5学习之路之Web存储解决方案
18 0
|
4月前
|
存储 安全
Web存储—localStorage存储
Web存储—localStorage存储
|
4月前
|
存储
Web存储—获取Cookie
Web存储—获取Cookie
|
4月前
|
存储 编解码 缓存
Web存储—本地存储Cookie
Web存储—本地存储Cookie
|
5月前
|
Web App开发 存储 安全
大师学SwiftUI第17章Part1 - Web内容访问及自定义Safari视图控制器
App可以让用户访问网页,但实现的方式有不止一种。我们可以让用户通过链接在浏览器中打开文档、在应用界面中内嵌一个预定义的浏览器或是在后台下载并处理数据。
46 0
|
8月前
|
Web App开发 JavaScript 前端开发
Web组件规范和自定义元素
Web组件规范和自定义元素
|
9月前
|
搜索推荐 JavaScript 数据可视化
数据可视化大屏高德地图javascript webAPI开发的智慧治安物联网管理系统实战解析(web GIS、3D视图、个性化地图、标注、涟漪动画、自定义弹窗、3D控件)
数据可视化大屏高德地图javascript webAPI开发的智慧治安物联网管理系统实战解析(web GIS、3D视图、个性化地图、标注、涟漪动画、自定义弹窗、3D控件)
383 0
|
9月前
|
JSON 前端开发 API
layui框架实战案例(8):web图片裁切插件croppers.js组件实现上传图片的自定义截取(含php后端)
layui框架实战案例(8):web图片裁切插件croppers.js组件实现上传图片的自定义截取(含php后端)
380 0
|
10月前
|
存储 Web App开发 JavaScript
web存储(Storage)
web存储(Storage)
|
10月前
|
安全 Java 编译器
【Java Web编程 八】深入理解Servlet常用对象
【Java Web编程 八】深入理解Servlet常用对象
95 1