Web.config Settings Editor Control

This control provides a "drag and drop" way to dynamically view and edit application and database connection settings in a Web.config file for an ASP.NET website. The control can be dropped onto a blank ASPX page and by default, it will display editable controls for all the settings in the <appSettings> and <connectionSettings> sections of the Web.config file. This control could be especially useful when deploying an ASP.NET web application that is installable on a user's IIS server and in order for the application to run, the user must set certain settings in the Web.config file.

Instead of forcing the user to have to understand the web.config's somewhat complex structure and manually edit XML properties, this control can be used to automatically create an easy to use web form to edit all the settings and save them to the web.config before running the site. Here's an example of what the editor form of the control looks like when rendered by default on a page: example of the control rendered on a page with default settings



The entire code for this control as well as a sample project and a precompiled DLL can be downloaded here:

WebConfigEditor.zip

Getting Started

Download the latest release of the control. Contained in the Visual Studio solution is a sample web project that shows how to use the control and the all of the code for the control. The control has been implemented as a composite control so it can be compiled into its own assembly and easily deployed. The projects in the solution include:

  • WebConfigEditor.Web This is a sample web project to show how to use the control. The Default.aspx page contains the control. The Web.config file at the web root of the project is the file that is read in when the project is run.
  • WebConfigEditor.Control This project contains all of the code for the WebConfigEditorControl. The code is heavily commented. The control's logic is in the WebConfigEditorControl.cs file.

To add this control to your toolbox so you can just drag and drop it onto your web form, just follow the tutorial that can be found here: http://www.devtoolshed.com/node/85 Once the control is in your toolbox, drag and drop it onto your page. This should register a section at the top of your page for the control that should look like this:

<%@ Register TagPrefix="devtoolshed" Namespace="WebConfigEditor.Control" Assembly="WebConfigEditor.Control" %>

The control on the page should look like this:

<devtoolshed:WebConfigEditorControl ID="WebConfigEditorControl1" runat="server" />

By default, the control edits the web.config file at the root of the website. So if that is the behaviour you desire, run the page and you will see all your settings and connection strings automatically editable. The properties and attributes below allow you to customize the editing experience in a very granular and exact way but at its most basic level, you should be able to get this running in less than 5 minutes with no code written.

Web.config Attributes

This control uses an attribute decoration model for Web.config settings very similar to the way attributes work in .NET code. A collection of one or more custom attributes can be applied on the comment line exactly about the setting to control how the editing of the setting will behave including whether or not it is to be displayed, whether it is required, validation parameters, and text descriptions and labels. The list of attributes must be separated each by a comma. No attributes are required. If no attributes are specified, then the setting is treated as a standard textbox input field and no validation is performed on the setting. It is treated as a string value.

<appSettings> Attributes

The following is a list of the attributes that can be applied for the appSettings of the Web.config file:

Attribute Name Syntax Description
DataType DataType("DateTime") The data type of the field to be validated against. This attribute also sets what control will be rendered in order to edit this setting.
Possible values include:
- None: renders as a TextBox control.
- String: renders as a TextBox control.
- Int: renders as a TextBox control but only allows positive whole numbers.
- DateTime: renders as a Calendar control.
- Bool: renders as a CheckBox control.
- Password: renders as a TextBox control but hides input with password characters.
- MultiLine: renders as a TextMode = MultiLine TextBox control.
- DropDown: renders as a DropDownList control.
- RadioButton: renders as a RadioButtonList control.
Description Description("My description of the field") The description of the field to tell the user what this field is for and how to fill it out.
IsHidden IsHidden("true") Whether or not the setting should be hidden from the user when editing. If true, no other attributes will be parsed and null is returned. Possible values include true or false.
IsRequired IsRequired("true") If true, the field will be required and must have a value before it can be saved. Possible values include true or false.
ItemList ItemList("My Item Value 1,My Item Value 2") A list of comma separated values for the items in the list. Listed in order of how they should appear in the drop down. Only applies to settings with DataType("DropDown") or DataType("RadioButton").
Label Label("My Label Name") The label of the field to show to the user above the control to make it easier to read than the key name of the setting.
LowerLimit LowerLimit("5") The inclusive lower limit range an integer value can contain. Only applies to settings with DataType("Int").
MaxLength MaxLength("10") The maximum length of characters the field can contain. If set to 0, no validation is performed. Only applies to settings with DataType("String").
RegexValidator RegexValidator("\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*") A regular expression to validate the field against. Only applies to settings with DataType("String").
UpperLimit UpperLimit("200") The inclusive upper limit range an integer value can contain. Only applies to settings with DataType("Int").

 

<connectionSettings> Attributes

The following is a list of the attributes that can be applied for the connectionSettings of the Web.config file:

 

Attribute Name Syntax Description
Description Description("My description of the connection.") The description of the connection to tell the user what this connection string is for.
IsHidden IsHidden("true") Whether or not the setting should be hidden from the user when editing. If true, no other attributes will be parsed and null is returned. Possible values include true or false.

 

Control Properties

The following is a list of the custom properties that can be set for the control and their purpose.

Control Text

Property Name Data Type Description
ApplicationSettingsHeadingText String The heading text of the Application Settings form section.
ButtonText String The text of the button to save changes for the form.
ConnectionNameRowHeaderText String The title of the Connection Name header for the connection strings table.
ConnectionProviderRowHeaderText String The title of the Connection Provider header for the connection strings table.
ConnectionSettingsHeadingText String The heading text of the Connection Settings form section.
ConnectionStringRowHeaderText String The title of the Connection String header for the connection strings table.
RequiredFieldText String The text shown at the bottom of the form next to the button to denote that fields are required or not.
SubTitle String The sub title text below the title of the web.config settings editor form.
Title String The title of the web.config settings editor form.

CSS Properties

Property Name Data Type Description
CssClassConnectionName String The Cascading Style Sheet (CSS) for the Connection Name in the connection settings table.
CssClassConnectionRow String The Cascading Style Sheet (CSS) for the standard row in the connection settings table.
CssClassConnectionRowAlt String The Cascading Style Sheet (CSS) for the alternate row in the connection settings table.
CssClassConnectionTableHeader String The Cascading Style Sheet (CSS) for the table header of the connection settings table.
CssClassFeedbackError String The Cascading Style Sheet (CSS) for the error labels on the form when a control is invalid.
CssClassFeedbackSuccess String The Cascading Style Sheet (CSS) for the success message when changes on the form are changed successfully.
CssClassFormHeading String The Cascading Style Sheet (CSS) for the form's headings for the Application and Connection settings sections.
CssClassInputButton String The Cascading Style Sheet (CSS) for the button that saves changes to the form.
CssClassInputDescription String The Cascading Style Sheet (CSS) for the descriptive text next to each input control's title.
CssClassInputDisplayText String The Cascading Style Sheet (CSS) for the form's input controls.
CssClassInputLabel String The Cascading Style Sheet (CSS) for the title text above each input control.
CssClassInputText String The Cascading Style Sheet (CSS) for the form's input text boxes.
CssClassSubTitle String The Cascading Style Sheet (CSS) for the form's sub title.
CssClassTitle String The Cascading Style Sheet (CSS) for the form's title.

Error Messages

Property Name Data Type Description
ErrorMessageCannotBeEmpty String The error message to show when a field cannot be empty.
ErrorMessageFieldInvalid String The error message to show when a field is not in the correct format.
ErrorMessageLowerLimit String The error message to show when an integer field is not within the inclusive lower limit of the value.
ErrorMessageMaxLength String The error message to show when a field's length is greater than the maximum allowed. NOTE: To add the length that is specified to the message, this place holder should be used: {0}
ErrorMessageMustBeValidNumber String The error message to show when a field is not a valid number.
ErrorMessageUpperLimit String The error message to show when an integer field is not within the inclusive upper limit of the value.
MainErrorMessage String The error message text shown next to the save button when 1 or more errors exists on the page.

Error Messages

Property Name Data Type Description
IsRestrictToLocalAccess Bool Whether or not access to the page where this control is hosted on should be allowed remotely. If true, only URL's that begin with: http://localhost will be allowed to access this page. If false, no restrictions will be enforced.
PathToWebConfigFile String The path to a web.config file. If not specified, the web.config at the root level of the site will be loaded.
RedirectOnSuccessUrl String The URL to redirect to if all fields were saved correctly. If nothing specified, no redirect will occur.

Target .NET Framework

This control was compiled under .NET Framework 2.0 so that it would be compatible with both .NET 2.0 and .NET 3.5 web projects. The code is provided as well as the compiled control. The control was developed using C# 2.0.