Home > ASP.NET > CodeFluent Entities and ASP.NET Web Forms Binding: Sorting Data

CodeFluent Entities and ASP.NET Web Forms Binding: Sorting Data


Pursuing our ASP.NET post series, in this post we’ll see how to set-up sorting in your GridView so users can click on the column header to sort their contacts. Sorting in CodeFluent Entities is done in the persistence layer to ensure optimal performances, however it’s not generated by default. To do so, select the properties you wan to be able to sort with and set there “Is Sortable” property to “True”:

image

Or in XML:

<Contact>
   <Id />
   <FirstName sortable="true" />
   <LastName sortable="true" />
</Contact>

Generate over again so CodeFluent Entities updates all your layers and you’re ready to go.

Set-up the ObjectDataSource to use the PageLoadAll method as sorting information is passed through its PageOptions parameter:

<asp:ObjectDataSource runat="server" ID="SortingDataSource" SelectMethod="PageLoadAll"
   TypeName="ContactAdmin.ContactCollection" DataObjectTypeName="ContactAdmin.Contact"
   StartRowIndexParameterName="pageIndex" MaximumRowsParameterName="pageSize" EnablePaging="true">
   <SelectParameters>
       <asp:Parameter Name="pageIndex" Type="Int32" DefaultValue="0" />
       <asp:Parameter Name="pageSize" Type="Int32" DefaultValue="100" />
       <cfw:PageOptionsParameter Name="pageOptions" DefaultPageSize="100" />
   </SelectParameters>
</asp:ObjectDataSource>

Set the GridView to allow sorting, and subscribe to the OnSorting event to apply it:

<asp:GridView runat="server" DataSourceID="SortingDataSource" AutoGenerateColumns="False"
    AllowPaging="true" AllowSorting="true" OnSorting="OnGridViewSorting">
    <Columns>
        <asp:BoundField HeaderText="Id" DataField="Id" />
        <asp:BoundField HeaderText="First Name" DataField="FirstName" SortExpression="[Contact].[FirstName]" />
        <asp:BoundField HeaderText="Last Name" DataField="LastName" SortExpression="[Contact].[LastName]" />
    </Columns>
</asp:GridView>
    
<script runat="server">
    private void OnGridViewSorting(object sender, GridViewSortEventArgs e)
    {
        CodeFluent.Runtime.Web.UI.WebControls.PageOptionsParameter p = (CodeFluent.Runtime.Web.UI.WebControls.PageOptionsParameter)SortingDataSource.SelectParameters["pageOptions"];
        p.HandlePostBackEvent(e);
    }
</script>

Note: you don’t need to use a code-behind file for the OnSorting event handler. Instead you can embed the code in the page using the Single-File page model which has the great benefit that to update the page you won’t have to compile and deploy a new dll to your web site.

And here it is, you can now sort your contacts by clicking on the “First Name” (first screenshot) and “Last Name” (second screenshot) column titles:

image image

Furthermore, since the sorting feature is implemented by the paged version of the method, you can also sort across paged results:

<asp:GridView runat="server" DataSourceID="SortingDataSource" AutoGenerateColumns="False" 
    AllowSorting="true" OnSorting="OnGridViewSorting"
    AllowPaging="true" PageSize="4">
    <Columns>
        <asp:BoundField HeaderText="Id" DataField="Id" />
        <asp:BoundField HeaderText="First Name" DataField="FirstName" SortExpression="[Contact].[FirstName]" />
        <asp:BoundField HeaderText="Last Name" DataField="LastName" SortExpression="[Contact].[LastName]" />
    </Columns>
</asp:GridView>

Here’s the result (sorted by first names):

image image

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