ObjectContainerDataSource On Selecting Event Enhancement Work Around
ObjectContainerDataSource is a great source used to load data into controls such as GridView. It implements data binding in a way that easily integrates with the Model-View-Presenter pattern and easily supports paging and sorting.
To start using this control you must download the required assembly “Microsoft.Practices.Web.UI.WebControls.dll” Add it as a reference to the project, and add this code to the web.config so it can be shared within the project:
</strong> <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/><strong>
Add the following code in the page section using Microsoft.Practices.Web.UI.WebControls. When using this data source to bind or display your data to the UI, you need to control the binding process that occurs when the “OnSelecting” event fires. Also, since this event will be raised it needs to be controlled in the code. We need to control this event behavior to minimize the unnecessary database calls (since we are binding the grid from the database). Each time there is a post back as a result of page load sorting, paging, or any command fired in the page, this event will be raised and a database connection will be opened. In turn, data will be retrieved and displayed more than one time.
To control this event, I will use a Boolean flag saved in the ViewState. This flag will be used by getting its value or setting it inside our GridView “OnRowCommand” event and in ObjectContainerDataSource “OnSelecting” event. First, we need to add the GridView and the ObjectContainerDataSource to the page as follows:
</strong> <asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectContainerDataSource1" AutoGenerateColumns="False" DataKeyNames="" AllowPaging="true" AllowSorting="true" OnRowCommand="GridView1_RowCommand"> </asp:GridView> <Microsoft:ObjectContainerDataSource ID="ObjectContainerDataSource1" runat="server" DataObjectTypeName="TestProject.BusinessEntities.Employees.Employee" TotalRowCount="0" UsingServerPaging="true" UsingServerSorting="true" OnSelecting="ObjectContainerDataSource1_Selecting"/> <strong>
Now we need to provide the IsSortOrPageCommand property declaration. This property is used to store the value that indicates that the event is sorting or paging. We only need the “OnSelecting” event to be performed when we have sorting or paging. The other behaviors, such as the search or page load, will be handled individually so we can separate the page behaviors from each other:
</strong>
private bool IsSortOrPageCommand
{
get
{
if (ViewState["isSortOrPageCommand"] != null)
return Convert.ToBoolean(ViewState["isSortOrPageCommand"], CultureInfo.InvariantCulture);
else
return false;
}
set
{
ViewState["isSortOrPageCommand"] = value;
}
}
<strong>
After adding the previous controls, we need to configure their events:
</strong>
protected void GridView1_RowCommand(object sender, CommandEventArgs e)
{
if (string.Compare(e.CommandName, "Sort", StringComparison.OrdinalIgnoreCase) == 0 ||
string.Compare(e.CommandName, "Page", StringComparison.OrdinalIgnoreCase) == 0)
{
IsSortOrPageCommand = true;
}
}
protected void ObjectContainerDataSource1_Selecting(object sender, Microsoft.Practices.Web.UI.WebControls.ObjectContainerDataSourceSelectingEventArgs e)
{
if (IsPostBack && IsSortOrPageCommand)
{
if (!String.IsNullOrEmpty(e.Arguments.SortExpression))
{
// Get the sort field and direction from e.Arguments.SortExpression
}
else
{
// Get the sort field and direction from your default values
}
// Do the search (bind process)
_presenter.OnSearch();
// reset the flag
IsSortOrPageCommand = false;
}
}
<strong>
Now we can utilize this improved control ObjectContainerDataSource and achieve the maximum benefits with the maximum performance from the aspx page.
