Binding Drop Down List control when Details View is in Edit Mode

Hi

This is quite common requirement where Master – Detail reporting functionality is implemented using two ASP.NET forms.

For example, GridView control is used to display all user details and one of the Grid view columns contains View/Open, so that each user record can be opened on next page when relevant row is selected. This can be achieved in various ways, most common approach is by passing UserID in URL. On second page using Query String parameter UserID is accessed and the detailed user record is displayed using DetailsView control.

Now DetailsView control displays a single record on second page. When it is required to provide functionality to Edit the DetailsView, simply you can change DetailsView mode to Edit as below. Note that in this example the button is out of DetailsView control.

/// <summary> 
/// Change DeailsView mode to Edit
///and data bind
/// Change DetailsView mode to Edit 
/// </summary> 
/// <param name="sender"></param> 
/// <param name="e"></param> 
protected void btnEditUser_Click(object sender, EventArgs e) 
{ 
dvUserRecord.ChangeMode(DetailsViewMode.Edit); 
//Bind DetailsView with your method
dvUserRecord.DataSource = GetUserRecord(); 
dvUserRecord.DataBind(); 
}

Note that initially DetailsView displays the data that is with in ItemTemplate section of asp:TemplateField section, in the below example code snippet country (i.e., United kingdom) is shown where it related to current user record.

When DetailsView changes to Edit mode, it is important to provide list of countries using List control such as DetailsView, so that the end user can change the country to desired one. Note that the controls and binded data with in EditItemTemplate section is shown when the DetailsView is in Edit mode. As shown in below code snippet, Drop Down List control is shown with binded list of countries.

<asp:TemplateField HeaderText="Country"> 
<ItemTemplate> 
<asp:Label ID="lblCountry" runat="server" Text='<%# bind("country")%>' /> 
</ItemTemplate> 
<EditItemTemplate> 
<asp:DropDownList ID="ddlCountry" runat="server" 
AppendDataBoundItems="True" > 
<asp:ListItem Value="" Text="" /> 
</asp:DropDownList> 
</EditItemTemplate> 
</asp:TemplateField>

How to bind Drop Down List in Details View when Details View is in Edit mode

This article concentrates and explains how to bind Drop Down List control with custom method from code behind page.

In order to bind drop down list with custom method, the right event is Details View Data Bound.

All it requires is checking DetailsView current mode is Edit and finding drop down List control and binding the custom method to it as shown below.

protected void dvUserRecord_DataBound(object sender, EventArgs e) 
{ 
if (dvUserRecord.CurrentMode == DetailsViewMode.Edit) 
{ 
//Find Drop down list from aspx page
DropDownList ddlCountry = dvUserRecord.FindControl("ddlCountry")
as DropDownList;
//check ddlCountry is not null || country drop down list is found
if(ddlCountry != null)
{
//Bind countries data to ddlCountry 
ddlCountry.DataTextField = "countryText"; 
ddlCountry.DataValueField = "IndexValue"; 
ddlCountry.DataSource = GetCountries; 
//custom method that gets all countries 
ddlCountry.DataBind();
}
}

Thats all! you are done!!

References

http://msdn.microsoft.com/en-us/library/aa581793.aspx

http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/detailsview.aspx

3 Comments

Comments have been disabled for this content.