在web.config存储自定义的对象友情链接
2011-05-16 10:16
561 查看
web.config里:
FriendLinks.cs:
demo:读取
后台修改保存:
我这里用了个笨法子,清掉,重新添加的:
<configSections> .. <section name="friendLinks" type="JiaYiSoftSite.App_Code.FriendLinks,JiaYiSoftSite,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" /> </configSections>
<friendLinks> <links> <add id="lnk1" href="#" text="关于本站" /> <add id="lnk2" href="#" text="版权声明" /> <add id="lnk3" href="#" text="活动公告" /> <add id="lnk4" href="#" text="站点地图" /> <add id="lnk5" href="#" text="合作伙伴" /> </links> </friendLinks>
FriendLinks.cs:
namespace JiaYiSoftSite.App_Code { //自定义友情链接元素 public class FriendLinkElement : ConfigurationElement { public FriendLinkElement() { } [ConfigurationProperty("id", IsRequired = true)] public string ID { get { return (string)this["id"]; } set { this["id"] = value; } } //超链接地址 [ConfigurationProperty("href", IsRequired = true)] public string Href { get { return (string)this["href"]; } set { this["href"] = value; } } //超链接文本 [ConfigurationProperty("text", IsRequired = true)] public string Text { get { return (string)this["text"]; } set { this["text"] = value; } } } //自定义元素集合 public class FriendLinkElementCollection : ConfigurationElementCollection { public FriendLinkElementCollection() { FriendLinkElement myElement = (FriendLinkElement)CreateNewElement(); Add(myElement); } public void Add(FriendLinkElement friendLinkElement) { BaseAdd(friendLinkElement); } protected override void BaseAdd(ConfigurationElement element) { base.BaseAdd(element, false); } public override ConfigurationElementCollectionType CollectionType { get { return ConfigurationElementCollectionType.AddRemoveClearMap; } } protected override ConfigurationElement CreateNewElement() { return new FriendLinkElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((FriendLinkElement)element).ID; } public FriendLinkElement this[int Index] { get { return (FriendLinkElement)BaseGet(Index); } set { if (BaseGet(Index) != null) { BaseRemoveAt(Index); } BaseAdd(Index, value); } } new public FriendLinkElement this[string Name] { get { return (FriendLinkElement)BaseGet(Name); } } public int indexof(FriendLinkElement element) { return BaseIndexOf(element); } public void Remove(FriendLinkElement link) { if (BaseIndexOf(link) >= 0) BaseRemove(link.ID); } public void RemoveAt(int index) { BaseRemoveAt(index); } public void Remove(string name) { BaseRemove(name); } public void Clear() { BaseClear(); } } //自定义友情链接节 class FriendLinks : ConfigurationSection { FriendLinkElement link; public FriendLinks() { link = new FriendLinkElement(); } //链接节属性-集合 [ConfigurationProperty("links", IsDefaultCollection = false)] [ConfigurationCollection(typeof(FriendLinkElementCollection), AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")] public FriendLinkElementCollection Links { get { return (FriendLinkElementCollection)base["links"]; } } } }
demo:读取
//友情链接 FriendLinks flinks = (FriendLinks)ConfigurationManager.GetSection("friendLinks"); ArrayList arr = new ArrayList(); foreach (FriendLinkElement link in flinks.Links) { if (link.ID != null && link.ID != "") { arr.Add(link); } } rptFLinks.DataSource = arr; rptFLinks.DataBind();
后台修改保存:
我这里用了个笨法子,清掉,重新添加的:
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); if (!string.IsNullOrEmpty(Request.Form["cntLinks"])) { FriendLinks flinks = (FriendLinks)config.GetSection("friendLinks"); int cnt = int.Parse(Request.Form["cntLinks"]); if (cnt > 0) flinks.Links.Clear(); for (int i = 1; i <= cnt; i++) { FriendLinkElement el = new FriendLinkElement(); el.ID = "lnk"+i; el.Href = Request.Form["lnk_url" + i]; el.Text = Request.Form["lnk_text" + i]; flinks.Links.Add(el); } } );
相关文章推荐
- 如何在web.config中存储自定义对象
- 如何在web.config中存储自定义对象
- 在配置文件(.settings、.config)中存储自定义对象
- 在配置文件(.settings、.config)中存储自定义对象
- NSUserDefaults 简介,使用 NSUserDefaults 存储自定义对象
- NSUserDefaults 简介,使用 NSUserDefaults 存储自定义对象
- 如何在web.config 或者 app.config 文件中配置自定义节点
- 存储自定义对象并遍历
- HyperLink链接动态传参数(Web.Config中设置绑定样式:格式字符串)
- HashSet 集合存储自定义对象并遍历
- 使用IConfigurationSectionHandler在web.config中增加自定义配置
- Web.config自定义节点configSections
- JDBC:数据库自定义类型与Java类的映射—将对象存储在关系数据库中(一)
- 集合框架--HashSet存储自定义对象
- TreeSet存储自定义对象并保证排序和唯一
- Web.config自定义节点configSections
- 集合框架---TreeSet存储自定义对象
- Web小结---Servlet,JSP,过滤器和监听器,四个作用域和九个内置对象,EL表达式语言,自定义标签,JSTL,国际化与汉字的编码
- web.config C#中使用自定义配置
- 05-方法values演示) 06-Map集合-常见子类对象) 07-Map集合-HashMap存储自定义对象) 08-Map集合-TreeMap存储自定义对象) 1 2