Click here to Skip to main content
15,888,803 members
Articles / Web Development / ASP.NET
Tip/Trick

SelectedValue for a DropDownList in a DetailsView control

Rate me:
Please Sign up or sign in to vote.
4.33/5 (2 votes)
25 Aug 2013CPOL 22K   2
Setting the selected value for a dropdownlist in a detailsview control when in edit mode

Introduction

Setting the C# ASP.NET DetailsView control in Edit modus, trigger the DataBound event for the DetailsView control, and in this event handler populate a DropDownList and finally set the SelectedValue of that DropDownList to the value of the underlying data. This might seem fairly easy, but there is a lot of confusion about it. Now here's the very basic guide to programmatically setting the default value for a DropDownList in a DetailsView control.

Prerequisites

You need a detailsview control into the HTML-part (name it DetailsView1 in the .aspx file) and use templated fields, and somehow bind that control to data. You also need to set the OnDataBound event in the detailsvei control. Set it to point to a method in your code behind file (aspx.cs). Example: OnDataBound="DetailsView_Databound". And finally you need a DropDownList inside the EditItemTemplate of your choice.

Procedure

C#
protected void DetailsView_Databound(Object sender, EventArgs e)
{
    if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
    {
        DropDownList dropper = (DropDownList)DetailsView1.FindControl(
               "inserthereThe_id_ofYourDropDownControl");
        if (dropper != null)
        {
            dropper.DataSource=FunctionThatReturnsData(); // This could be a list for instance
            dropper.DataBind();
            if (dropper.Items.Contains(dropper.Items.FindByValue(DataBinder.Eval(
                     DetailsView1.DataItem, "UnderlyingDataFieldName").ToString())))
            {
                dropper.SelectedIndex = dropper.Items.IndexOf(
                  dropper.Items.FindByValue(DataBinder.Eval(DetailsView1.DataItem, 
                  "UnderlyingDataFieldName").ToString()));
            }
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Norway Norway
Like programming in C#. Systems integration as a speciality. Love electronics and the lates rave, iot.

Comments and Discussions

 
QuestionA simpler Way of doing it? Pin
satyan1235-Feb-15 19:33
satyan1235-Feb-15 19:33 
Though the question is old, somebody may land here. While You have suggested a programmatic way of solving the issue, namely the Edit mode should keep relevant data fields selected of the DDl as per ReadOnlyMode. This can be done in a design mode in a simpler way using SelectedValue property as follows:
< EditItemTemplate >
...
DataTextField="FieldToShow" DataValueField="FieldValueToSelect" SelectedValue='<%# Bind("FieldValueToSelect")%>'
...


I found this solution in Pro ASP.NET4 in VB2010 by Mathew MacDonald et al (Apress)page 463.
That saved a lot of my work...!
Since Internet Solutions are searched even after many years, this I thought of sharing.
Of course your article suggested a programatic way of doing it.
Generalthanks Pin
mathewtinu12-Jul-14 3:52
mathewtinu12-Jul-14 3:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.