Displaying Data With the ObjectDataSource [1]:
In previous tutorials we've seen how to programmatically bind data from the DAL and BLL to a data Web control in an ASP.NET page. This syntax assigning the data Web control's DataSource property to the data to display and then calling the control's DataBind() method was the pattern used in ASP.NET 1.x applications, and can continue to be used in your 2.0 applications.
However, ASP.NET 2.0's new data source controls offer a declarative way to work with data. Using these controls you can bind data retrieved from the BLL without having to write a line of code!
ASP.NET 2.0 ships with five built-in data source controls:
- SqlDataSource: with this dara source control it is possible to access, insert, update, and delete database data directly from an ASP.NET page, bypassing the architecture. Doing so places the specific database queries and business logic directly in the web page. For sufficiently large or complex applications, designing, implementing, and using a tiered architecture is vitally important for the success, updatability, and maintainability of the application. Developing a robust architecture, however, may be unnecessary when creating exceedingly simple, one-off applications. The SqlDataSource can be used to access and modify data directly from a relational database, including Microsoft SQL Server, Microsoft Access, Oracle, MySQL, and others. NOTE: Contrary to the ObjectDataSource, the SqlDataSource control, lacks the properties for implementing custom paging. [4]
- AccessDataSource: The sole difference between the AccessDataSource and SqlDataSource controls is how the database connection information is specified. The AccessDataSource control needs just the file path to the Access database file. The SqlDataSource, on the other hand, requires a complete connection string [4];
- ObjectDataSource: with this data source control it is possible to declaratively interface with the architecture from the presentation layer;
- XmlDataSource;
- SiteMapDataSource;
Note:
Conceptually, both the ObjectDataSource and SqlDataSource controls are simply proxies to data. [4]
The ObjectDataSource's Configure Data Source wizard offers a quick way to specify the object it uses and to associate what methods of the object are invoked. You can, however, configure the ObjectDataSource through its properties, either through the Properties window or directly in the declarative markup. Simply set the
TypeName property to the type of the underlying object to be used, and the SelectMethod to the method to invoke when retrieving data.Even if you prefer the Configure Data Source wizard there may be times when you need to manually configure the ObjectDataSource, as the wizard only lists developer-created classes. If you want to bind the ObjectDataSource to a class in the .NET Framework such as the Membership class, to access user account information, or the Directory class to work with file system information you'll need to manually set the ObjectDataSource's properties.<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"SelectMethod="GetProducts" TypeName="ProductsBLL"></asp:ObjectDataSource>
The ObjectDataSource serves as a proxy for working with some other object. To configure the ObjectDataSource we specify this underlying object and how its methods map to the ObjectDataSource's Select, Insert, Update, and Delete methods. Once this underlying object has been specified and its methods mapped to the ObjectDataSource's, we can then bind the ObjectDataSource to a data Web control (like: GridView, DetailsView, RadioButtonList, and DropDownList).
To specify the ObjectDataSource's underlying object and how that object's methods map to the ObjectDataSource's, click on the Configure Data Source link from the ObjectDataSource's smart tag.
Passing input parameters [2]:
The ObjectDataSource can be used to invoke methods that expect input parameters, but in order to do so we must specify where the values for these parameters come from. The parameter values can be hard-coded or can come from a variety of dynamic sources, including: querystring values, Session variables, the property value of a Web control on the page, or others.
Programmatically Setting the ObjectDataSource's Parameter Values [3]:
Whenever the ObjectDataSource's
Select method is invoked the ObjectDataSource first raises its Selecting event. The ObjectDataSource's underlying object's method is then invoked. Once that completes the ObjectDataSource's Selected event fires (Figure 1 illustrates this sequence of events). The parameter values passed into the ObjectDataSource's underlying object's method can be set or customized in an event handler for the Selecting event.The final screen of the DataSource configuration wizard asks us to provide the
month parameter value's source. Since we'll set this value programmatically, leave the Parameter source set to the default None option and click Finish.This will create a
Parameter object in the ObjectDataSource's SelectParameters collection that does not have a value specified.<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
OldValuesParameterFormatString="original_{0}"
onselecting="ObjectDataSource1_Selecting"
SelectMethod="GetEmployeesByHiredDateMonth" TypeName="EmployeesBLL">
<SelectParameters>
<asp:Parameter Name="month" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>To set this value programmatically, we need to create an event handler for the ObjectDataSource's
Selecting event.protected void ObjectDataSource1_Selecting(object sender,
ObjectDataSourceSelectingEventArgs e)
{
e.InputParameters["month"] = DateTime.Now.Month;
}We can also assign a value directly to a ObjectDataSource parameter.
For example if we want to set the "CategoryID" parameter of the next ObjectDataSource with a value passed by querystring we could do it in code behind with:
aspx file:
<asp:ObjectDataSource ID="CategoryProductsDataSource" runat="server"
OldValuesParameterFormatString="original_{0}"
SelectMethod="GetProductsByCategoryID" TypeName="ProductsBLL">
<SelectParameters>
<asp:Parameter Name="categoryID" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource> cs file:
CategoryProductsDataSource.SelectParameters["categoryID"].DefaultValue =
Page.Request.QueryString["id"];Related articles:
[1] - Displaying Data With the ObjectDataSource
[2] - Declarative Parameters
[3] - Programmatically Setting the ObjectDataSource's Parameter Values
[4] - Querying Data with the SqlDataSource Control
No comments:
Post a Comment