[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-02-12
7
Medium Priority
?
4,099 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:noobPlayer
  • 5
  • 2
7 Comments
 
LVL 16

Expert Comment

by:sunithnair
ID: 23624375
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
 
LVL 16

Expert Comment

by:sunithnair
ID: 23624393
Try setting the SelectedValue in rowbound event..
0
 
LVL 16

Expert Comment

by:sunithnair
ID: 23624436
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:sunithnair
ID: 23624456
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
 

Author Comment

by:noobPlayer
ID: 23624910
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
 
LVL 16

Accepted Solution

by:
sunithnair earned 1500 total points
ID: 23625201
Try setting the selectedindex/selected value in the dropdown list databound event
0
 

Author Closing Comment

by:noobPlayer
ID: 31546175
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question