locked
‘dropdownlist1′ has a SelectedValue which is invalid because it does not exist in the list of items RRS feed

  • Question

  • User-111824800 posted

    I'm having some issues populating a city ddl based on the user's selection of the previous state ddl. I was able to assign a temporary value to the city field so the page will load. It seems to be working but the datavalue is not writing to the database.

    For the DDLCity1, I am pulling all cities from a table called Facilities where the users's choice of state equals the state field in the db. However, I am writing the selection back to a different table called Students. The field in the lookup table is called "city", the field in the database I'm writing to is called "CityPref1". My code is below.

    <!DOCTYPE html>
    <script>
    Protected Sub Formview1_PreRender(ByVal sender As Object, ByVal e As
        System.EventArgs) Handles Formview1.PreRender
        If Formview1.CurrentMode = FormViewMode.Edit Then
            Dim rowView As DataRowView = CType(Formview1.DataItem, DataRowView)
            If Not IsNothing(rowView) AndAlso Not IsNothing(CType(Formview1. & _
                FindControl("DDLCity1"), DropDownList).Items. & _
                FindByValue(rowView("CityPref1").ToString())) Then
                    CType(Formview1.FindControl("DDLCity1"),
                    DropDownList).SelectedValue = rowView("CityPref1").ToString()
             End If         
        End If
    End Sub
    
    Protected Sub Formview1_ItemUpdating(ByVal sender As Object, ByVal e As & _
        System.Web.UI.WebControls.FormViewUpdateEventArgs) & _
        Handles Formview1.ItemUpdating
            e.NewValues("CityPref1") = CType(formview1.FindControl("DDLCity1"), & _
                DropDownList).SelectedValue
    End Sub
    </script>
    


     

    Monday, November 19, 2012 11:37 AM

All replies

  • User1495804863 posted

    Hi,

    Here are two articles about implementing Cascading DropDownList :

    Ajax CascadingDropDown: http://www.asp.net/ajax/ajaxcontroltoolkit/samples/CascadingDropDown/CascadingDropDown.aspx

    DropDownList: http://www.mikepope.com/blog/DisplayBlog.aspx?permalink=1629

    Check this also for reference

    http://www.tek-tips.com/viewthread.cfm?qid=1680902

    Monday, November 19, 2012 11:45 PM
  • User-111824800 posted
    Thanks for your help, almost there! The only issue now is that the city value is not saving to the AccessDataSource1 Student table. 

    <!DOCTYPE html> <script runat="server"> Protected Sub FormView_DataBound(ByVal sender As Object, _ ByVal e As System.EventArgs) If FormView.CurrentMode = FormViewMode.Edit Then Dim dv As System.Data.DataRowView = FormView.DataItem Dim ddl2 As DropDownList = FormView.FindControl("DDLCity1") Dim dsc As AccessDataSource = FormView.FindControl("AccessDataSource1") dsc.SelectParameters("States").DefaultValue = dv("States") ddl2.DataBind() If Not IsDBNull(dv("City")) Then ddl2.SelectedValue = dv("City") End If End If End Sub Protected Sub DDLCity1_SelectedIndexChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) Dim dsc As AccessDataSource = FormView.FindControl("AccessDataSource1") Dim ddl1 As DropDownList = FormView.FindControl("DDLState1") Dim ddl2 As DropDownList = FormView.FindControl("DDLCity1") dsc.SelectParameters(0).DefaultValue = ddl2.SelectedValue ddl2.DataBind() End Sub Protected Sub FormView_ItemUpdating(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.FormViewUpdateEventArgs) Dim dsc As AccessDataSource = FormView.FindControl("AccessDataSource1") Dim ddl2 As DropDownList = FormView.FindControl("DDLCity1") e.NewValues("CityPref1") = ddl2.SelectedValue ddl2.DataBind() End Sub </script>

    		<asp:FormView runat="server" id="FormView" DataSourceID="AccessDataSource1" DataKeyNames="ID" DefaultMode="Edit" Width="311px"  OnItemUpdated="FormView_ItemUpdated" >
    			<EditItemTemplate>
    				<fieldset>
    				
    <legend>Geographic</legend>
    				<p>
    				<asp:AccessDataSource runat="server" ID="States" DataFile="../ClinicalSites.mdb" SelectCommand="SELECT [State Abbreviation] AS State_Abbreviation FROM [States] ORDER BY [State Abbreviation]">
    				</asp:AccessDataSource>
    				1. State
    				<asp:DropDownList runat="server" id="DDLState1" DataValueField="State_Abbreviation" DataTextField="State_Abbreviation" DataSourceID="States" SelectedValue='<%# Bind("StatePref1") %>' AutoPostBack="True">
    				</asp:DropDownList>
    				&nbsp;<asp:DropDownList runat="server" id="DDLCity1" DataValueField="City" DataTextField="City" DataSourceID="City" OnSelectedIndexChanged="DDLCity1_SelectedIndexChanged">
    					<asp:ListItem Text="Choose City"></asp:ListItem>
    				</asp:DropDownList>
    				<asp:AccessDataSource runat="server" ID="City" DataFile="../ClinicalSites.mdb" SelectCommand="SELECT DISTINCT [City] FROM [Facilities] WHERE ([State] = ?) ORDER BY [City]">
    					<SelectParameters>
    						<asp:controlparameter ControlID="DDLState1" PropertyName="SelectedValue" Name="State2" Type="String" />
    					</SelectParameters>
    				</asp:AccessDataSource>
    				</p>
    </fieldset>
    </EditItemTemplate>
    		</asp:FormView>
    

    Friday, November 23, 2012 1:42 PM