noobPlayer
asked on
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="dd lState_Sel ectedIndex Changed" ></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)
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="dd
</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">
Try setting the SelectedValue in rowbound event..
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
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
ASKER
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.
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
Open in new window