![]() |
Web Development »
ASP.NET »
General
Intermediate
License: The Code Project Open License (CPOL)
Complex Parameter Support for ObjectDataSourceBy Steven James GrayAn example showing how to create a custom parameter for ASP.NET data sources that allows the passing of arbitrarily complex objects. |
C#, ASP.NET, WebForms, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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:
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.
The way the code works is as follows:
Evaluate on the ParameterCollection. This returns a dictionary of name to value mappings for each evaluated parameter. ParameterCollection is evaluating the ObjectParameter, we look at the value of the TypeName attribute and instantiate the appropriate type dynamically. ObjectParameter has its own ParameterCollection (the 'Properties' property) that is evaluated. ParameterCollection onto the properties of the activated type through reflection. 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. tagprefix' element to your web.config to register the parameter if you try referencing the library from your own code. | You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 3 Feb 2009 Editor: Deeksha Shenoy |
Copyright 2009 by Steven James Gray Everything else Copyright © CodeProject, 1999-2009 Web17 | Advertise on the Code Project |