web.config里:
- <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);
- }
- }
- );
本文转自 xcf007 51CTO博客,原文链接:http://blog.51cto.com/xcf007/566165,如需转载请自行联系原作者