Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Complex Parameter Support for ObjectDataSource

0.00/5 (No votes)
3 Feb 2009 1  
An example showing how to create a custom parameter for ASP.NET data sources that allows the passing of arbitrarily complex objects.

Introduction - Object Data Sources with Object Parameters

The built in ASP .NET data-source model comes with support for a variety of parameter types, allowing us to feed our applications, used via data sources such as SqlDataSource, XmlDataSource and ObjectDataSource. The built in parameters however are fairly limited, in that you cannot build complex object structures, and must have all types resolve to a handful of simple types such as strings and integers. The closest ASP.NET allows us is that it automatically constructs some types when performing an Update or Delete, but there is no such support for Select's.

This example code shows how to build arbitrary objects as inputs to ObjectDataSources, by means of a custom parameter type, ObjectParameter. The ObjectParameter creates an instance of a specified type and then evaluates a set of parameter objects in order to populate the properties of the object. Visually this appears somewhat like:

ObjectParameter-Code

When translated to ASP.NET, this appears as:

        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
            SelectMethod="SearchEntities" 
            TypeName="CobaltSoftware.ExampleBusinessLayer.SomedataObject,
                 CobaltSoftware.ExampleBusinessLayer">
            <SelectParameters>
                <Cobalt:ObjectParameter Name="input"

                   TypeName="CobaltSoftware.ExampleBusinessLayer.SearchParametersEntity,
                   CobaltSoftware.ExampleBusinessLayer" > 
                    <Properties>
                        <asp:ControlParameter ControlID="TxtForename" name="Forename" />
                        <asp:ControlParameter ControlID="TxtSurname" name="Surname" />
                        <asp:ControlParameter ControlID="TxtAge" name="MinAge" />   
                    </Properties>
                </Cobalt:ObjectParameter>
            </SelectParameters>     

In this example, we're constructing a simple search parameters type entity, which fetches its state from various elements on the page. An instance of SearchParametersEntity is created (using the default constructor) and then each of the property parameters are evaluated and applied to the properties of the object.

How Does It Work?

The way the code works is as follows:

  • ASP.NET evaluates the arguments group for the data source by calling Evaluate on the ParameterCollection. This returns a dictionary of name to value mappings for each evaluated parameter.
  • When the ParameterCollection is evaluating the ObjectParameter, we look at the value of the TypeName attribute and instantiate the appropriate type dynamically.
  • The ObjectParameter has its own ParameterCollection (the 'Properties' property) that is evaluated.
  • We map each of the returned values from the Properties ParameterCollection onto the properties of the activated type through reflection.

Points to Note

  • Always implement a ToString() on objects you create for parameters, as ASP.NET uses the ToString() value for the caching key when you enable caching on data sources.
  • You can nest these arbitrarily to build up insanely large and complex object trees if you so desire.
  • You can use any number of these to allow calling of code that requires multiple complex parameter inputs as arguments.
  • Please ensure that you add the 'tagprefix' element to your web.config to register the parameter if you try referencing the library from your own code.

Revision History

  • 3rd Februrary, 2009 - Initial submission to CodeProject

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here