I lose selected value in ASP dropdownlist after postback when a text box becomes visible after user selection

Hello, I am new to ASP 3.5 and have the following problem:

My database has to keep track of doctoral students' extra-curricular activities. One of these activities is making "presentations".  I have to track information about these presentations, including the location of these presentations.

On my webform, I have a templated dropdown list (Autopostback = true) in a detailsview that allows the user to select a location.  The dropdown list is databound to a list of locations.  If the user selects "Other" from this list, a previously hidden textbox (also in the detailsview) becomes visible so that the user can enter the name of the location.
However, in edit mode, if user changes from a prior selection (for example FL) to "Other", the textbox becomes visible, but the drop down list selection reverts to FL.  I don't know how to make the drop down remember the user's selection of "Other".

If I leave the textbox visible all the time, then there is no problem, and the dropdownlist remembers the most recent selection. So I think the problem might be here:

              </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlState" runat="server" AutoPostBack="True"
                        DataSourceID="sqlStates" DataTextField="StateName" DataValueField="StateCode"
                        SelectedValue='<%# Bind("StateProv") %>'
                        onselectedindexchanged="ddlState_SelectedIndexChanged" ></asp:DropDownList>
                </EditItemTemplate>

when the page redraws itself to make the textbox visible (or hidden)?

Any help would be appreciated!
Thank you (this is my very first question, so suggestions on clarity etc. are also welcome)

VB Code Behind
   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.lblName.Text = CStr(Me.Session("Recid"))
        If CStr(Me.Session("Recid")) = "" Then
            Me.DetailsView1.DefaultMode = DetailsViewMode.Insert
        End If
        'EDIT MODE
        If Not Me.IsPostBack Then
            Dim myDdl As DropDownList
            myDdl = Me.DetailsView1.FindControl("ddlState")
            If myDdl.SelectedValue = "ZZ" Then
                Me.DetailsView1.Fields(4).Visible = True
            Else
                Me.DetailsView1.Fields(4).Visible = False
            End If
        End If
    End Sub
 
    Protected Sub ddlState_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
 
        Dim myDdl As DropDownList
        myDdl = Me.DetailsView1.FindControl("ddlState")
 
        If myDdl.SelectedValue = "ZZ" Then
            Me.DetailsView1.Fields(4).Visible = True
        Else
            Me.DetailsView1.Fields(4).Visible = False
        End If
End Sub
 
 
SQL Data Source for locations list:
   <asp:SqlDataSource ID="sqlStates" runat="server" 
        ConnectionString="<%$ ConnectionStrings:CPSMainConnectionString1 %>" 
        
        SelectCommand="SELECT StateName, StateCode FROM tblStates ORDER BY SortOrder, StateCode">

Open in new window

noobPlayerAsked:
Who is Participating?
 
sunithnairCommented:
Try setting the selectedindex/selected value in the dropdown list databound event
0
 
sunithnairCommented:
I think it might be because of this. You are setting the selectedValue as <%# Bind("StateProv") %> which i suppose resets to tha value in the database
<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="True"
                        DataSourceID="sqlStates" DataTextField="StateName" DataValueField="StateCode"
                        SelectedValue='<%# Bind("StateProv") %>'
                        onselectedindexchanged="ddlState_SelectedIndexChanged" ></asp:DropDownList>

Open in new window

0
 
sunithnairCommented:
Try setting the SelectedValue in rowbound event..
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
sunithnairCommented:
Sorry in detailsview try in this event
    Protected Sub dv_ItemCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles dv.ItemCreated
 
    End Sub

Open in new window

0
 
sunithnairCommented:
Like this

    Protected Sub dv_ItemCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles dv.ItemCreated
        CType(dv.FindControl("ddlState"), DropDownList).SelectedValue = "VALUE FROM DATABASE"
    End Sub

Open in new window

0
 
noobPlayerAuthor Commented:
Hiya Sunithair, your answer got me going in a new direction.

I placed the index of the user's selection in a session variable during the SelectedIndexChanged event.  Then, I added the code below on the DetailsView DataBound event.  It doesn't seem to retrigger the selected index changed event in the DropDownList.

I will test some more, and hopefully get some other input.

   Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.DataBound
        Dim myDdl As DropDownList
        myDdl = Me.DetailsView1.FindControl("ddlState")
        If Not Me.Session("idxState") Is Nothing Then
            myDdl.SelectedIndex = CInt(Me.Session("idxState"))
        End If
    End Sub

Open in new window

0
 
noobPlayerAuthor Commented:
Expert pointed me in the right direction, so I resolved the problem after hours of frustration. A little more detail would have been helpful, but I understood the point. Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.