This document provides details regarding how to use objectdatasource control in
asp.net application for grid view paging using select method.
2. What is ObjectDataSource?
ObjectDataSource is a non-display control in ASP.NET 2.0 that acts as a bridge
between your data-access objects and data-bound controls on the web page.
ObjectDataSource helps the developer to reduce the code to bind data to data-bound
controls. ObjectDataSource works in stateless mode like the web itself.
ObjectDataSource can be used for CRUD operations (Create, Retrieve, Update, and
Delete). If you use ObjectDataSource to display data in the asp.net page, it calls the
‘select’ method to create an object, reads the properties from that object for the
data-bound control and disposes it before the page has completed. This article
provides a clear description of using ObjectDataSource to bind data to grid view and
paging logic.
3. How to use ObjectDataSource?
Create an ASP.NET website naming ObjectDataSourceDemo for instance. In this
sample I have used enterprise library for establishing database connection. If you are
not familiar with enterprise library you can use your own way to establish connection
to database. In the aspx page just add a grid view and an objectdatasource control
like the code below.
AutoGenerateColumns="false" AllowPaging="true" Width="835px"
OnDataBound="dgvRequests_DataBound" ShowFooter="True">
AutoGenerateColumns="false" AllowPaging="true" Width="835px"
OnDataBound="dgvRequests_DataBound" ShowFooter="True">
runat="server" SelectCountMethod="SelectCount" EnablePaging="true"
SelectMethod="GetRequest" TypeName="RequestData">
Set the DataSourceId property of grid view as the id of the ObjectDataSource,
In our case it is ‘objRequest’, and AllowPaging and ShowFooter to true.
Here let us know certain properties of ObjectDataSource before using them.
EnablePaging :
Gets or sets a value indicating whether the ObjectDataSource control has data
caching enabled.
SelectMethod :
Gets or sets the name of the method or function that the ObjectDataSource control
invokes to retrieve data.
SelectCountMethod:
Gets or sets the name of the method or function that the ObjectDataSource control
invokes to retrieve a row count.
TypeName:
To create an instance of the object that the ObjectDataSource control binds to, the
control uses reflection to load the type that is identified by the type name at run time.
Therefore, the value of the TypeName property can be a partially qualified type for code
that is located in the Bin or App_Code directories or a fully qualified type name for code
that is registered in the global assembly cache. If you use the global assembly cache, you
must add the appropriate reference to the assemblies section of the Machine.config or
Web.config file.
In this example I have added a class RequestData.cs in App_Code directory and so the
TypeName is specified as above. Fully qualified class name that is “namespace.Classname”
can be given in this property if the class is not placed in App_Code directory.
Let’s have a look at the SelectMethod.
public DataTable GetRequest(int maximumRows, int
startRowIndex,ObjectDataSourceSelectingEventArgs e)
{
DataTable dtRequest = null;
objBus = new objBusiness();
dtRequest = objBus.GetRequestDetail(out totalPage).Tables[0];
if (dtRequest != null && dtRequest.Rows.Count > 0)
{
Page 5 of 8
e.Arguments.TotalRowCount = totalPage;
}
return dtRequest;
}
The selectMethod must have maximumRows, startRowIndex as arguments as shown
above and return type as Datatable. GetRequestDetail is the method in base class used to
get the data from database and totalPage is the out variable of the method is the total
number of records that is to be returned from the procedure, ‘objBus’ is the instance of the
business class in order to use the method.
The ObjectDataSourceSelectingEventArgs class is used in the OnSelecting method. Because
it is derived from the ObjectDataSourceMethodEventArgs class, the
ObjectDataSourceSelectingEventArgs class provides access to input parameters for
validation and manipulation through the InputParameters property. The
ObjectDataSourceSelectingEventArgs class also provides the ExecutingSelectCount
property, which is used to check whether the currently executing data retrieval operation is
retrieving a row count, in addition to the data. This is important because when data source
paging is enabled, the Selecting event is raised twice. For more information, see
ExecutingSelectCount.
To add the argument ‘e’ in the selectMethod we have added it in InputParameters. If you
need to pass any argument in select method it is necessary to add it in OnSelecting event of
the ObjectDataSource.
public void objRequest_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
if (!e.ExecutingSelectCount)
{
e.Arguments.MaximumRows = dgvRequests.PageSize;
e.Arguments.RetrieveTotalRowCount = true;
e.InputParameters.Add("e", e);
}
}
The SelectCountMethod in the App_Code just returns the row count as shown
below.
public int SelectCount(ObjectDataSourceSelectingEventArgs e)
{
return e.Arguments.TotalRowCount;
}
To display the page count in the footer of grid view add a label in Footer template of the
grid view and assign the value in DataBound event of the grid view like the code below.
protected void dgvRequests_DataBound(object sender, EventArgs e)
{
if (dgvRequests.Rows.Count > 0)
{
lblpageString = (Label)dgvRequests.FooterRow.FindControl("lblPageString");
if (lblpageString != null)
lblpageString.Text = string.Format("Page {0} Of {1}",
this.dgvRequests.PageIndex + 1, this.dgvRequests.PageCount);
}
}
}
After this just run the asp.net application and you may see the grid view working fine
with the
paging implementation.
4. Conclusion
Thus paging logic can be implemented in grid view using ObjectDataSource.The same can also
be done to DataView control and we can also use ObjectDataSource for deleting. Updating and inserting
data in database.