Background:
I was designing an ASP.NET application and had a page that used a SqlDataSource control. Within the data source controls, ASP.NET has the following built-in inputs for the select parameters:
<SelectParameters>
<asp:ControlParameter />
<asp:CookieParameter />
<asp:FormParameter />
<asp:Parameter />
<asp:ProfileParameter />
<asp:QueryStringParameter />
<asp:SessionParameter />
</SelectParameters>
I was storing the value I wanted to use as a select parameter argument as a key/value pair within the web application's <appSettings> configuration settting. While there was no out-of-box ASP.NET parameter for handling data stored in the application settings configuration collection, the SessionParameter control was very close in that it also returned the value of a key/value pair, but was retreiving the data from the HttpSessionState. Firing up Reflector, I examined and then dissembled the SessionParameter class as a starting point for creating a class that inherits from Parameter. The result is a class I named ApplicationSettingParameter.
Overview:
The ApplicationSettingParameter class provides the SetttingField property in addition to those inherited from the Parameter class.
The SettingField property identifies the application setting variable from which the ApplicationSettingParameter retrieves a value.
Note: Controls that bind data to the parameter might throw an exception if a ApplicationSettingParameter object is specified but no corresponding
application setting variable is set. Similarly, they might display no data if the application setting variable is set with a null reference (Nothing in Visual Basic). Set the DefaultValue property to avoid these situations where appropriate.
Example:
<%@ Register Namespace="Oppositional.Web.UI.WebControls" TagPrefix="oppositional" %>
<asp:SqlDataSource
ID="SqlDataSourceProcedureResults"
runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionStringName%>"
DataSourceMode="DataSet"
EnableCaching="false"
SelectCommand="dbo.StoredProcedureName"
SelectCommandType="StoredProcedure"
>
<SelectParameters>
<oppositional:ApplicationSettingParameter Name="StoredProcInputName" Type="String" Direction="Input" SettingField="AppSettingKeyName" />
</SelectParameters>
</asp:SqlDataSource>
In the above example you would add the ApplicationSettingParameter.cs class file to your web application's App_Code directory, register a reference to the web control, and then utilize the parameter class in a SqlDataSource as shown above.
Implementation Details:
The ApplicationSettingParameter class does the actual lookup of the application setting value witihn the overridden Evaluate(HttpContext context, Control control) method. Here is the actual code of this routine, where SECTION_NAME is a string constant that has been set to "appSettings":
protected override object Evaluate(HttpContext context, Control control)
{
//------------------------------------------------------------
// Attempt to evaluate and return result
//------------------------------------------------------------
try
{
//------------------------------------------------------------
// Validate context
//------------------------------------------------------------
if ((context != null) && (context.GetSection(SECTION_NAME)!= null))
{
//------------------------------------------------------------
// Get application settings configuration collection
//------------------------------------------------------------
NameValueCollection applicationSettings = (NameValueCollection)context.GetSection(SECTION_NAME);
//------------------------------------------------------------
// Return value of specified setting
//------------------------------------------------------------
return applicationSettings[this.SettingField];
}
//------------------------------------------------------------
// Return empty result
//------------------------------------------------------------
return null;
}
catch
{
//------------------------------------------------------------
// Rethrow exception
//------------------------------------------------------------
throw;
}
}
As you can see the routine takes the provided context and uses it to extract the collection of configured application settings, and then returns the value for the key specified in the parameter's SettingField property. The only other thing to note is that the value of SettingField is stored in ViewState["SettingField"].
While this class has been designed to work against the <appSettings> configuration section, you could modify it to use any configuration section that returns a NameValueCollection, and could add a property called SectionName to allow for designating the configuration section to read from at design time. I plan on doing this and then having ApplicationSettingParameter inherit from this more generic implementation. As you can see it is fairly easy to extend the base Parameter class to handle a variety of scenarios.
Download the zip compressed archive of the class implementation: ApplicationSettingParameter.zip (1.92 kb)