Bind a DetailsView control with a DropDownList control

This example demonstates how to bind a DetailsView control with a DropDownList control when DetailsView is in Edit Mode or in Insert mode. By binding the DropDownList control to a SqlDataSource control, we can populate the DropDownList control with the data present in the database

The below code demonstrates how to place a DropDownList control in a EditItemTemplate and InsertItemTemplate. The EditItemTemplate is used to display user interface while DetailsView control is in edit mode.

output of DetailsView control with a DropDownList control in a EditItemTemplate and InsertItemTemplate

Similarly, the InsertItemTemplate is used to display user interface while DetailsView control is in insert mode.

output of DetailsView control used with InsertItemTemplate to display user interface

In the below code, only one field is created as a TemplateField in the DetailsView control and all other fields are created as bound fields. We declare template field for the parent group column.

We also declare InsertItemTemplate and EditItemTemplate inside templatefield element and add DropDownList controls.

We do so because, when DetailsView is in Edit mode, it is necessary to provide a list of groups using the dropdownlist control from where the user will edit a group to which the account belongs. Similarly, when DetailsView is in Insert mode, we provide a list of groups from where the user will select a group to which the account belongs. The DropDownList controls are bound to the SqlDataSource control and displays list of groups.

The DetailsView control displays the data using ItemTemplate section of TemplateField element. Observe the ItemTemplate section of the detailsView control.

<asp:DetailsView ID="DetailsView1"   	AutoGenerateRows="false"
datakeynames="AccountCode"      		DataSourceID="MyDataSource"
AutoGenerateInsertButton="true"   	AutoGenerateEditButton="true" 
AllowPaging="true"          			runat="server" >
<Fields>
	<asp:boundfield datafield="AccountCode"  headertext="Code of Account"/>
	<asp:boundfield datafield="AccountName"  headertext="Name of Account"/>
	<asp:boundfield datafield="Accountdescription" headertext="description"/>
	<asp:TemplateField HeaderText="Parent Group">
		<ItemTemplate>
			<%#Eval("GroupName")%>
		</ItemTemplate>
		<InsertItemTemplate>
			<asp:DropDownList id="GroupList" datasourceid="GroupSqlDataSource"
				datatextfield="GroupName" DataValueField="GroupCode"  
				SelectedValue='<%# Bind("AccountPGroup") %>' runat="server"/>
		</InsertItemTemplate>
		<EditItemTemplate>
			<asp:DropDownList id="GroupList" datasourceid="GroupSqlDataSource"
			datatextfield="GroupName" 	DataValueField="GroupCode"  
			SelectedValue='<%# Bind("AccountPGroup") %>'   runat="server"/>
		</EditItemTemplate>
	</asp:TemplateField>
	<asp:boundfield datafield="AccountAddress1" headertext="First Address"/>
	<asp:boundfield datafield="AccountAddress2" headertext="Second Address"/>
	<asp:boundfield datafield="AccountPhone" headertext="Phone of Account"/>
	<asp:boundfield datafield="AccountCity" headertext="City of Account"/>
	<asp:boundfield datafield="AccountOpamt" headertext="Opening balance" />
	<asp:boundfield datafield="AccountClamt" headertext="Closing balance"/>
	<asp:boundfield datafield="AccountCat" headertext="Category of account"/>
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="MyDataSource"  ConnectionString="<%$Connectionstrings:ERPConnectionString%>"
SelectCommand="SELECT AccountsTable.AccountCode,AccountsTable.AccountName,
AccountsTable.Accountdescription,GroupTable.GroupName,
AccountsTable.AccountPgroup,AccountsTable.AccountAddress1,AccountsTable.AccountAddress2,
AccountsTable.AccountPhone,AccountsTable.AccountCity, 
AccountsTable.AccountOpamt,AccountsTable.AccountClamt,
AccountsTable.AccountCat FROM AccountsTable,GroupTable 
WHERE AccountsTable.AccountPgroup=GroupTable.GroupCode"  
UpdateCommand="Update AccountsTable SET AccountName=@AccountName,Accountdescription
=@Accountdescription,AccountAddress1=
@AccountAddress1,AccountAddress2=@AccountAddress2,AccountPhone=@AccountPhone,
AccountCity=@AccountCity,AccountOpamt=@AccountOpamt, AccountClamt=@AccountClamt, 
AccountCat=@AccountCat,AccountPGroup=@AccountPgroup Where AccountCode=@AccountCode" 
Insertcommand="Insert AccountsTable  (AccountCode, AccountName, Accountdescription,
AccountPgroup,AccountAddress1,AccountAddress2,
AccountPhone,AccountCity,AccountOpamt, AccountClamt, AccountCat) 
values(@AccountCode,@AccountName,@Accountdescription,@AccountPgroup,
@AccountAddress1,@AccountAddress2,@AccountPhone,@AccountCity,
@AccountOpamt,@AccountClamt,@AccountCat)" runat="server"/>

<asp:SqlDataSource ID="GroupSqlDataSource"  ConnectionString="<%$Connectionstrings:ERPConnectionString%>"
SelectCommand="SELECT GroupCode,GroupName FROM GroupTable"  runat="server"/>


Using Bind() and Eval() methods in templates

We are familiar with the Eval() method when using data binding expressions. The difference between the Eval() method and the Bind() method is:

We use Eval() method to display data and this data is not going to take part in Insert and Update operations.

We use Bind() to display data and this data can take part in Insert and Update operations. When we use Bind() method in a TemplateField, the Bind() method supports two-way data binding, which means data can be updated, inserted, and deleted automatically from the data source control.

DetailsView Articles
Most Viewed