Database Paging and Sorting with CSLA Part 3 - The presentation layer

This is the final part on how to implement database paging with a custom CSLA business class and SQL Server 2005.  In this post I will demonstrate how to call the business object from part two from the applications presentation layer.  This approach will place minimal code in the presentation layer and allows for clean seperation and the ability to write automated tests against the intended behavior.

 I used a DataGrid and CSLADataSource to present the data from the list class:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="CslaDataSourceInfoList"

DataKeyNames="Id" OnRowDeleted="GridView1_RowDeleted" Width="100%" PageSize="10"

AllowPaging="True" AllowSorting="true" EmptyDataText="No Records Available."

PagerSettings-Mode="NumericFirstLast ">

<Columns>

<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id"

Visible="False" />

<asp:HyperLinkField HeaderText="Subject" SortExpression="Subject" DataNavigateUrlFormatString="InfoEdit.aspx?id={0}"

DataNavigateUrlFields="Id" DataTextField="Subject" ItemStyle-Width="300" />

<asp:BoundField DataField="CategoryName" HeaderText="Category" ReadOnly="True"

SortExpression="CategoryName" ItemStyle-Width="75"/>

<asp:BoundField DataField="Description" HeaderText="Description" ReadOnly="True"

SortExpression="Description" ItemStyle-Width="200"/>

<asp:TemplateField ControlStyle-Width="50px" HeaderStyle-Width="60px" ItemStyle-HorizontalAlign="Center">

<ItemTemplate>

<asp:Button ID="btnDelete" CommandName="Delete" runat="server" Text="Delete"

OnClientClick="showConfirm(this); return false;" />

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

<csla:CslaDataSource ID="CslaDataSourceInfoList" runat="server" TypeAssemblyName=""

TypeName="InfoTracker.InfoListItem, InfoTracker" TypeSupportsPaging="True" TypeSupportsSorting="False"

OnSelectObject="CslaDataSourceInfoList_SelectObject" OnDeleteObject="CslaDataSourceInfoList_DeleteObject">

</csla:CslaDataSource>

In the code behind, I used the SelectObject event handler to retrieve the business object and specify the paging information.  Finally in the GetInfoList method I call into the business class to get the data:

protected void CslaDataSourceInfoList_SelectObject(object sender, Csla.Web.SelectObjectArgs e)

{

InfoList infoList = GetInfoList(GridView1.PageIndex, GridView1.PageSize, GetSort(GridView1.SortExpression, GridView1.SortDirection.ToString()),tbSearch.Text);

lblTotalRecords.Text = infoList.TotalRecordCount.ToString();

e.BusinessObject = infoList;

}

private InfoTracker.InfoList GetInfoList(int index, int pageSize, string sort, string searchString)

{

return InfoTracker.InfoList.GetInfoList(index, pageSize, sort, searchString);

}