GridView and DetailsView Master/Detail page using ObjectDataSource control
The GridView and DetailsView controls are frequently used with ObjectDataSource control to display data in a Master/Detail page. Note that if you are developing an ERP software or any application software based on a three-tier architecture design, SqlDataSource control cannot be used. So, we have to use the ObjectDataSource control to create a three-tier architecture software application.
In this example, we have used GridView control to display the list of accounts, DetailsView control to display the details of a selected account and the ObjectDataSource controls to connect to the business objects.
In the above code, we have used two ObjectDataSource controls, ObjectDataSource1 and ObjectDataSource2, ObjectDataSource1 to bind the GridView control and ObjectDataSource2 to bind the DetailsView control.
Firstly, we need to configure the ObjectDataSource1 control to retrieve code and name of all the accounts. To do so, we set the SelectMethod property to the name of the class method which exists in the Business Layer. The ObjectdataSource control calls the class method to retrieve data (account's code and name).
We set the AllAccounts() method to the SelectMethod of the ObjectDataSource1. We also specify the name of the type of object that the ObjectDataSource control represents using TypeName property. Set the TypeName property as shown below.
TypeName="myCsBusinessLayer.BusinessObjects.Account"
Observe the code in the BusinessLayer class which is given below. The myCsBusinessLayer and BusinessObjects are the namespaces and Account is the class.
The class Account.cs should contain the methods that are directly used by the ObjectDataSource controls. The Account class contains two methods required by the ObjectDataSource controls. The method AllAccounts() retrieves all accounts from the Accountstable and returns as a DataSet. This method is called by ObjectdataSoiurce1. The method SingleAccountDetail() retrieves account details for a paricular account. This method is called by ObjectdataSoiurce2.
The following is the code for the Account class.
After configuring ObjectDataSource1, we bind the GridView control to the ObjectDataSource1 control by using the DataSourceID property.
Next, we need to configure the ObjectDataSource2 control to retrieve the selected account details. We set the SingleAccountDetail()method to the SelectMethod of the ObjectDataSource2. The method SingleAccountDetail() retrieves account details of a particular account.
The GridView control displays AccountCode and AccountName columns. The DetailsView control displays details of the single account which is selected by the user.
By setting the AutoGenerateSelectbutton property to 'true', the Select button is displayed for each row of the GridView control. This button is rendered as a link button as shown in fig.
How to Display the Selected row details in a DetailsView in a Master/Detail page
Explanation is similar to the previous article, listed here..
GridView and DetailsView Master/Detail page using SqlDataSource control
In this example, we have used GridView control to display the list of accounts, DetailsView control to display the details of a selected account and the ObjectDataSource controls to connect to the business objects.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MasterDetail.aspx.cs" Inherits="myCsPresentationLayer.MasterDetail" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <style type="text/css"> .column { float:left; padding:10px; width:265px; } </style> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div class="column"> <asp:GridView ID="GridView1" DataSourceID="ObjectDataSource1" AutoGenerateColumns="false" DataKeyNames="AccountCode" AutoGenerateSelectButton="true" runat="server" > <Columns> <asp:BoundField DataField="AccountCode" HeaderText="AccountCode" /> <asp:BoundField DataField="AccountName" HeaderText="AccountName" /> </Columns> </asp:GridView> </div> <div class="column"> <asp:DetailsView ID="DetailsView1" DefaultMode= "Edit" AutoGenerateEditButton="true" AutoGenerateInsertButton="true" AutoGenerateDeleteButton="true" DataSourceID="ObjectDataSource2" runat="server"> </asp:DetailsView> </div> <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="AllAccounts" TypeName="myCsBusinessLayer.BusinessObjects.Account"> </asp:ObjectDataSource> <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="SingleAccountDetail" TypeName="myCsBusinessLayer.BusinessObjects.Account"> <SelectParameters> <asp:ControlParameter DefaultValue="A002" Name="AccountCode" ControlID="GridView1" PropertyName="SelectedValue"> </asp:ControlParameter> </SelectParameters> </asp:ObjectDataSource> </div> </form> </body> </html>

In the above code, we have used two ObjectDataSource controls, ObjectDataSource1 and ObjectDataSource2, ObjectDataSource1 to bind the GridView control and ObjectDataSource2 to bind the DetailsView control.
Firstly, we need to configure the ObjectDataSource1 control to retrieve code and name of all the accounts. To do so, we set the SelectMethod property to the name of the class method which exists in the Business Layer. The ObjectdataSource control calls the class method to retrieve data (account's code and name).
We set the AllAccounts() method to the SelectMethod of the ObjectDataSource1. We also specify the name of the type of object that the ObjectDataSource control represents using TypeName property. Set the TypeName property as shown below.
TypeName="myCsBusinessLayer.BusinessObjects.Account"
Observe the code in the BusinessLayer class which is given below. The myCsBusinessLayer and BusinessObjects are the namespaces and Account is the class.
The class Account.cs should contain the methods that are directly used by the ObjectDataSource controls. The Account class contains two methods required by the ObjectDataSource controls. The method AllAccounts() retrieves all accounts from the Accountstable and returns as a DataSet. This method is called by ObjectdataSoiurce1. The method SingleAccountDetail() retrieves account details for a paricular account. This method is called by ObjectdataSoiurce2.
The following is the code for the Account class.
namespace myCsBusinessLayer { namespace BusinessObjects { public class Account { private string PrivateConnectionString = "Server=LocalHost ; Database=ERPFinAccounting;User ID=kris;Password=rooman123;"; #region "AllAccounts - Return all Accounts public DataSet AllAccounts() { DataSet localDSOutput; string ParamsStoredProcedure = "AllAccounts"; DataServer localOutPutServer = new DataServer(PrivateConnectionString); localDSOutput = localOutPutServer.returnDataSet (ParamsStoredProcedure, "AccountsTable"); return localDSOutput; } #endregion public DataSet SingleAccountDetail(string AccountCode) { DataSet localDSOutput; string ParamsStoredProcedure; DataServer localOutPutServer = new DataServer(PrivateConnectionString); localOutPutServer.BuildParameter("@AccCode", AccountCode, DataServer.SQLDataType.SQLString, 6, ParameterDirection.Input); ParamsStoredProcedure = "DisplayAccountByCode"; localDSOutput = localOutPutServer.returnDataSet (ParamsStoredProcedure, "AccountsTable"); return localDSOutput; } } }// end of the class }// end of the namespace
After configuring ObjectDataSource1, we bind the GridView control to the ObjectDataSource1 control by using the DataSourceID property.
Next, we need to configure the ObjectDataSource2 control to retrieve the selected account details. We set the SingleAccountDetail()method to the SelectMethod of the ObjectDataSource2. The method SingleAccountDetail() retrieves account details of a particular account.
The GridView control displays AccountCode and AccountName columns. The DetailsView control displays details of the single account which is selected by the user.
By setting the AutoGenerateSelectbutton property to 'true', the Select button is displayed for each row of the GridView control. This button is rendered as a link button as shown in fig.
How to Display the Selected row details in a DetailsView in a Master/Detail page
Explanation is similar to the previous article, listed here..
GridView and DetailsView Master/Detail page using SqlDataSource control
DetailsView Articles
- Difference between DetailsView and FormView control
- DataBinding a DetailsView control
- DropDownList and GridView Master/Detail page using ObjectDataSource control
- DetailsView databound event
- DetailsView integer type conversion error?
- Using detailsview control datakeynames property
- How to get the datakey value in DetailsView control
- DetailsView autogeneraterows property
- DetailsView fields
- Formatting DetailsView control with style properties
- Using Boundfields in DetailsView control
- Display message using EmptyDataTemplate in the DetailsView Control
- How to access DetailsView's fields programmatically
- Bind a DetailsView control with a DropDownList control
- Using Command buttons in DetailsView control
- Using Commandfield element in a DetailsView control
- Using buttonfield in a DetailsView control
Most Viewed
Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
ERP
ERP Software Development
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls