Rich Rich - 3 months ago 54 Question

How do I set the Selected Value of a DropDownList inside an EditItemTemplate inside a DetailsView using the codebehind?

I have a Details View on my ASP.NET (VB) page which lists the details of a customer (their name, address, etc).

The 'Country' option is a drop down list, pulled from a database table. This all works fine when viewing the Details View and you can see what country they are in.

However, when I click the Edit button on the DetailsView, the drop down list then shows all the available countries in the db table correctly but it has no selected value, so instead of selecting their currently-set country, it just defaults to the top one in the list. This is wrong as it's not their currently-set value.

Using the codebehind (VB), how should I set the selected value of the dropdown list to be the country name that they currently have set? In the codebehind, I have already read their current country name in and stored it in a string variable called



Response.Write "strCurrentCountryName: " & strCurrentCountryName

Will show:

strCurrentCountryName: United Kingdom

So I now need to match their
value with one of the country names in the drop down list and set this as the SelectedValue for them.

My code in front (which lists all the countries in the db):

<asp:TemplateField HeaderText="Country" SortExpression="address5">
<asp:SqlDataSource ID="SqlDataSourcecountryList" runat="server" ConnectionString="<%$ ConnectionStrings:PDConnectionString %>"
ProviderName="<%$ ConnectionStrings:PDConnectionString.ProviderName %>"
SelectCommand="SELECT country_name_short, country_name_long FROM country_list WHERE deleted = 0 ORDER BY country_name_long">
<asp:DropDownList ID="DropDownListCountry" runat="server" CssClass="form-control"
DataSourceID="SqlDataSourcecountryList" DataTextField="country_name_long" DataValueField="country_name_short">
<asp:Label ID="Label1" runat="server" Text='<%# Bind("address5") %>'></asp:Label>

In the codebehind, I tried this (inside the
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

DropDownListCountry.Items.FindByText(strCurrentCountryName).Selected = True

Which gives an error message of:

'DropDownListCountry' is not declared. It may be inaccessible due to its protection level

I tried using these two lines of code with the drop down list placed OUTSIDE of the EditItemTemplate and it works perfectly:

DropDownListCountry.Items.FindByText(strCurrentCountryName).Selected = True

Sure enough, it does then correctly set their Selected Value to be the correct country.

In my codebehind (vb), do I need to somehow declare the

How do I make this work when placed inside the EditItemTemplate of my asp:TemplateField ?


Since the drop down is inside the template, it is not statically declared anywhere in the page's code, and so there is no way to reference it directly by just DropDownListCountry. Consider this - if that was possible, and you had a grid view template with these, drop down in which row would you be referencing?

So in reality this drop down gets assigned a different ID based on the container template it is in. So you need to find it inside the template on data binding time and then set the selected value the way you did:

Dim ddl As DropDownList = CType(detailsViewId.FindControl("DropDownListCountry"), DropDownList)
ddl.Items.FindByText(strCurrentCountryName).Selected = True

Important thing is to make sure that this code is run once drop down control was created. Most suitable place is right after data binding happend. To do so, subscribe to the DataBound event:

<asp:DropDownList ID="DropDownList1" runat="server" OnDataBound="DropDownList1_DataBound">

and run the code inside the event handler:

Protected Sub DropDownList1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
    ' Code above goes here 
End Sub