How To Add a User Define Section in Web.config-ASP.NET

You can create your own configuration section and use one of the staic methods in the System. Configuration namespace to retrieve the information. Attached is a simple example. Also, lookup configuration element and IConfigurationSectionHandler interface for additional information.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UserSection.aspx.cs"
Inherits="UserSection" %>

<!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>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
      <div>
          <asp:GridView  ID="GridView1" runat="server">
         <Columns>
        <asp:BoundField DataField="Key" HeaderText="Key" />
        <asp:BoundField DataField="Value" HeaderText="Value" />
     
         </Columns>
          </asp:GridView>
      </div>
  </form>
</body>
</html>

Code Behind

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;

public partial class UserSection : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
       GridView1.DataSource = ConfigExample();

       GridView1.DataBind();

   }

   private static ArrayList ConfigExample()
   {

       Hashtable htList = new Hashtable();
       System.Collections.Specialized.StringDictionary dictionary =
       (System.Collections.Specialized.StringDictionary)
       System.Configuration.ConfigurationSettings.GetConfig("MySection");
       IEnumerator iterator = dictionary.GetEnumerator();
       DictionaryEntry entry;
       while (iterator.MoveNext())
       {
           entry = (DictionaryEntry)iterator.Current;
           htList.Add(entry.Key);

       }
       return arList;
   }
}

IConfigurationSectionHandler Implementation

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.Xml;

/// <summary>
/// Summary description for MySectionHander
/// </summary>
namespace MyNamespace
{
   public class MySectionHandler :
System.Configuration.IConfigurationSectionHandler
   {
       #region IConfigurationSectionHandler Members

       public object Create(object parent, object configContext, XmlNode
section)
       {
           System.Collections.Specialized.StringDictionary mySection =
               new System.Collections.Specialized.StringDictionary();
           if (section == null || section.ChildNodes.Count == 0)
           {
               return mySection;
           }
           foreach (XmlNode node in section.ChildNodes)
           {
               mySection.Add(node.Attributes[0].Value,
node.Attributes[1].Value);
           }
           return mySection;
       }

       #endregion
   }
}

APP.CONFIG OR WEB.CONFIG

<?xml version="1.0" encoding="utf-8" ?>
 <configuration>

 <configSections>
   <section name="MySection" type="MyNamespace.MySectionHandler,
MyAssemblyName"/>
 </configSections>

 <MySection>
   <add key="key1" value="value1" />
   <add key="key2" value="value2" />
   <add key="key3" value="value3" />
 </MySection>

</configuration>

Please do not post any spam link in the comment box😊

إرسال تعليق (0)
أحدث أقدم