Home > ASP.NET > CodeFluent Entities, ASP.NET ObjectDataSource and GridView

CodeFluent Entities, ASP.NET ObjectDataSource and GridView


In this article, we will show how to use CodeFluent, to bind data to a GridView, using the ASP.NET ObjectDataSource control. We will show the following functionalities:

  1. How CodeFluent Entities takes care of paging for you,
  2. How easy it is to add sorting using PageOptionsParameter class.

We will be using Visual Studio 2010 and .NET 3.5, although this article applies also to Visual Studio 2008. We will omit the generation of the layers, assuming you already have that ready.

Setting up the ObjectDataSource with the generated BOM (Business Object Model)

In the image above, we have our solution structure, with the 3 projects:

  • Contacts.Design,
  • Contacts BOM,
  • Contacts.Website Layer for our user interface.

In the default.aspx, we will be implementing the complete example, to simplify the coding, and make it easy to understand. In real customer scenarios, all code will be put in the BOM, user controls (ascx) or custom controls in respect with your coding architecture.
Here’s the entity we will be using :

The generated Contacts database shown hereafter:

First, let’s add the references and connection string to our generated database. This connection string of course is necessary for the BOM to connect to the Contacts database:

1 – Add reference to the Business Object Model :

Select the Contacts Project, and click ok.

2 – Next, go to the Web.Config and add the following section right under the <ConfigSections> opening Tag:

<section name="Contacts" type="CodeFluent.Runtime.CodeFluentConfigurationSectionHandler, CodeFluent.Runtime"/>

This will define the possibility to have our own custom section, to manipulate database connection strings.
Next, add the following element:
<Contacts connectionString="server=(local);database=Contacts;uid=sa;pwd=SoftFluent2010"/>

3 – Now we get started with the creation of the GridView, ObjectDataSource and the aspx page

Select the Contacts.PersonCollection, or the collection you are using, and click next:

In the “Select” Tab, choose the PageLoadAll with the two parameter signature, and click Next.

pageIndex and pageSize parameters

The default values for each parameter are shown on the page. Then click Finish.

Paging

Let’s add the paging functionality. We start by setting the following properties to the ObjectDataSource:
EnablePaging="true"
MaximumRowsParameterName="pageSize"
StartRowIndexParameterName="pageIndex"

By default, Paging is already enabled, in the ObjectDataSource. The default paging mechanism for this control, is that it will load all data in memory, then, let you page through it. It can quickly become a mess if you need to load huge amounts in a heavily accessed application.

By setting EnablePaging=”true”, we’re telling the ObjectDataSource that we want to implement our own paging. In this case, we use the PageLoadAll parameters, which takes the two parameters, handled by the ObjectDataSource (MaximumRowsParameterName=”pageSize” and StartRowIndexParameterName=”pageIndex”).

This way, we let CodeFluent Entities take care of paging, by constructing only the necessary rows, and thus, gaining a lot in performance, and not having to code our own custom paging.

Notice: in we dive a little bit more into this paging mechanism, the BOM layer which is usually installed on the application server (IIS for instance), will retrieve all the rows between 1 to pageSize x pageIndex. But, only the retained rows corresponding to what the user expects in the GridView, will be constructed in memory and put in the corresponding collection, in our case, the PersonCollection object.

CodeFluent implements a great custom paging mechanism that can be easily used in your project without any development.

This is it for paging, the grid should work just fine. Here’s a complete code listing :

ASP.NET code for with paging capabilities

Sorting

For each column of you GridView that you need to sort, you must add a SortExpression:

<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
<asp:BoundField DataField="Email" HeaderText="Email"
SortExpression="Email" />
</Columns>

The PageLoadAll method has two overloads, the second overload supports 3 parameters. So we will simply add the third parameters to the “SelectParameters” section.

For Sorting, we will add a custom parameter, provided by CodeFluent Entities, which will handle most of the mechanism for us.

Here’s the Register directive for this control:

<%@ Register TagPrefix="_cfwc" Namespace="CodeFluent.Runtime.Web.UI.WebControls" Assembly="CodeFluent.Runtime.Web" %>

And here’s the syntax for adding this parameter to the SelectParameters tag:

<_cfwc:PageOptionsParameter Name="pageOptions" DefaultPageSize="100" />

This will change the code list for the GridView and ObjectDataSource to this:

Using PageOptionsParameter

For now, the sorting, will still not work, as we have to tell the gridview, to use the sorting mechanism provided by CodeFluent Entities.  In this case, we will add a handler to the “OnSorting” event, in order to have custom sorting activated.

Finally, we need to add code, which will execute the HandlePostBackEvent method of the PageOptionsParameter class as shown hereafter:

Sorting event used by the GridView

Here’s the complete code listing with paging and sorting capabilities:

Complete code listing with paging and sorting capabilities

The corresponding aspx.cs code is the following:

Corresponding aspx.cs code

Notice 1:  the SortExpression must use an expression expected by the underlying stored procedure generated by CodeFluent. If you look at the content of the Person_LoadAll stored procedure, you’ll notice that CodeFluent has inserted a sorting behavior and you’ll immediately understand on which expression the sorting relies.
Notice 2: if you need to allow update operations through the GridView (also works with FormView, DetailsView and ListView), you must populate the DataKeyName attribute with the correct properties which are usually “Id” and “RowVersion” in a standard CodeFluent project.

Conclusion

In this tutorial, we illustrated how to include paging and sorting functionalities, using CodeFluent Entities, the Gridview control and the ObjectDataSource control. In upcoming articles, we will be focusing also on the ListView Control, on paging and performance, and other functionalities of CodeFluent, which can help enhance your WebForms, MVC or WebForms MVP developments.

Oussama Dinia – SoftFluent Consultant

Categories: ASP.NET
  1. BindraBindu
    October 14, 2011 at 10:00 am

    This is nice one, great job. It’s really helpful.
    It solved my problem and this link http://mindstick.com/Articles/61ee812f-4abb-4294-a780-202ef0b8b031/?ObjectDataSource%20in%20ASP.Net also helped me to complete my task.

    Thanks

  1. January 24, 2011 at 10:01 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s