QueryStringValue<T> Class

The QueryStringValue<T> Class is similar to the StateValue<T> Class. ObjectBuilder will automatically inject QueryString Values into public fields of type QueryStringValue<T>.

By default it is assumed that the name of the public field is the same name as the QueryString Parameter. The key is case insensitive:

http://www.myurl.com/default.aspx?pageindex=5

public class Default : Page
{
     public QueryStringValue<string> PageIndex;

     // ...
}


If the name of the public field and QueryString Parameter are different, the difference can be specified by using the QueryStringKeyAttribute:

http://www.myurl.com/default.aspx?p=5

public class Default : Page
{
     [QueryStringKey("p")]
     public QueryStringValue<string> PageIndex;

     // ...
}


If the QueryString Parameter is missing from the Url, the public field will be set to its default value: default(T).

Adding the QueryStringValue<T> Functionality

The QueryStringValue<T> Functionality is implemented as a QueryStringBindingStrategy similar to the SessionStateBindingStrategy used for StateValue<T>. To enable the functionality, create a Custom WebClientApplication Class and add the QueryStringBindingStrategy by overriding the AddBuilderStrategies Method:

public class CustomWebClientApplication : WebClientApplication
{
     protected override void AddBuilderStrategies(IBuilder<BuilderStage> builder)
     {
          builder.Strategies.AddNew<QueryStringBindingStrategy>(BuilderStage.Initialization);
          base.AddBuilderStrategies(builder);
     }
}

Type Conversion

The values of QueryString Parameters are of type String, but are often converted to another type in the application. The feature uses Convert.ChangeType to attempt any necessary conversions. If unsuccessful, it will throw an InvalidCastException.

The following conversion to Int32 and DateTime will happen automatically:

http://www.myurl.com/default.aspx?p=5&dt=1/1/2008

public class Default : Page
{
     [QueryStringKey("p")]
     public QueryStringValue<int> PageIndex;

     [QueryStringKey("dt")]
     public QueryStringValue<DateTime> MyDate;

     // ...
}

Nullable Type Support

Nullable types are also supported.

http://www.myurl.com/default.aspx?p=5&dt=1/1/2008

public class Default : Page
{
     [QueryStringKey("p")]
     public QueryStringValue<int?> PageIndex;

     [QueryStringKey("dt")]
     public QueryStringValue<DateTime?> MyDate;

     // ...
}

Last edited Oct 29, 2007 at 5:36 AM by gblock, version 1

Comments

shatl Jun 20, 2009 at 6:00 PM 
For latest version of WCSF CustomWebClientApplication should look like this:

protected override void AddBuilderStrategies(IBuilder<WCSFBuilderStage> builder)
{
builder.Strategies.AddNew<QueryStringBindingStrategy>(WCSFBuilderStage.Initialization);
base.AddBuilderStrategies(builder);
}