Creating a Configuration Element Collection : New Section « Configuration « ASP.NET Tutorial






File: App_Code\ShoppingCartSection.cs

using System;
using System.Configuration;

namespace MyNamespace
{
    public class ShoppingCartSection : ConfigurationSection
    {
        [ConfigurationProperty("maximumItems", DefaultValue = 100, IsRequired = true)]
        public int MaximumItems
        {
            get { return (int)this["maximumItems"]; }
            set { this["maximumItems"] = value; }
        }

        [ConfigurationProperty("defaultProvider")]
        public string DefaultProvider
        {
            get { return (string)this["defaultProvider"]; }
            set { this["defaultProvider"] = value; }
        }

        [ConfigurationProperty("providers", IsDefaultCollection = false)]
        public ProviderSettingsCollection Providers

        {
            get { return (ProviderSettingsCollection)this["providers"]; }
        }

        public ShoppingCartSection(int maximumItems, string defaultProvider)
        {
            this.MaximumItems = maximumItems;
            this.DefaultProvider = defaultProvider;
        }
    }
}

File: Web.config

<configuration>
  <configSections>
    <sectionGroup name="system.web">
      <section
        name="shoppingCart"
        type="MyNamespace.ShoppingCartSection"
        allowLocation="true"
        allowDefinition="Everywhere" />
    </sectionGroup>
</configSections>
<system.web>

  <shoppingCart
    maximumItems="50"
    defaultProvider="SqlShoppingCartProvider">
    <providers>
      <add
        name="SqlShoppingCartProvider"
        type="MyNamespace.SqlShoppingCartProvider" />
      <add
        name="XmlShoppingCartProvider"
        type="MyNamespace.XmlShoppingCartProvider" />
    </providers>
  </shoppingCart>

</system.web>
</configuration>








16.25.New Section
16.25.1.Creating Custom Configuration Sections by inheriting a new class from ConfigurationSection class
16.25.2.Using the custom configuration section to modify the page style and background color.
16.25.3.Creating a Configuration Element Collection