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.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MasterDetail.aspx.cs" 
Inherits="myCsPresentationLayer.MasterDetail" %>

Transitional//EN" "">

<html xmlns="" >
<head runat="server">
<style type="text/css">

    <title>Untitled Page</title>
    <form id="form1" runat="server">
    <div class="column">
<asp:GridView ID="GridView1" DataSourceID="ObjectDataSource1" 	 
DataKeyNames="AccountCode"  AutoGenerateSelectButton="true"  
    	runat="server" >
<asp:BoundField DataField="AccountCode" HeaderText="AccountCode"  />
    <asp:BoundField DataField="AccountName" HeaderText="AccountName"  />
 <div class="column">
<asp:DetailsView ID="DetailsView1" 
	DefaultMode= "Edit"  AutoGenerateEditButton="true" 						
	AutoGenerateInsertButton="true" AutoGenerateDeleteButton="true"				
	DataSourceID="ObjectDataSource2" 	runat="server">
   <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" 
             <asp:ControlParameter  DefaultValue="A002" 
			 Name="AccountCode" ControlID="GridView1" 

output of GridView and DetailsView Master/Detail page

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.

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;

	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