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.
Similarly, the InsertItemTemplate is used to display user interface while DetailsView control is in insert mode.
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.
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.
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.

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

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
- Difference between DetailsView and FormView control
- DataBinding a DetailsView control
- GridView and DetailsView Master/Detail page using SqlDataSource control
- GridView and DetailsView master detail page using ObjectDataSource 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
- 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