Question

I have a problem with selectedvalue property in a dynamic created dropdownlist control not updating:

I have a custom control of type ctlProductosFormatos containing a combo and other controls. I create this custom controls dynamically in other place, and then I iterate through all the combos in the dynamically created controls, to update the items list on them copying from another dummy dropdownlist with the values I want.

The controls in this code are iterated right, and the combos are also filled right. My problem is that I want to maintain the selectedvalue on each combo at the same value that it has before the items update, but this fails.

If i step in the code and run only the iteration for the first combo, and jump outside the loop, this combo is right filled and with the right selected value, but if I run the full loop, all the combos are set with the same value than the last combo, so I suppose it's something related with something like I'm instantiating the same control for all the iterations, but it seem not to be that in my code.

    Dim ControlFormato As ctlProductosFormatos
    ' Iterates through custom controls collection, IEnumerable(Of ctlProductosFormatos)
    For Each ControlFormato In Controles
        ' Get the dropdownlist inside the current custom control
        Dim ControlComboFind As Control = ControlFormato.FindControl("cmbFotoFormato")
        Dim ControlCombo As DropDownList = CType(ControlComboFind, DropDownList)
        ' Get the currently selected value in the dropdownlist
        Dim ValorSeleccionado As String = ControlCombo.SelectedValue

        ' Clear the items in the current combo,and fills with the ones in a dummy combo
        ControlCombo.Items.Clear()
        ControlCombo.Items.AddRange(ComboPatron.Items.OfType(Of ListItem)().ToArray())

        ' Sets the current combo selected item with the previously saved one
        ControlCombo.SelectedValue = ValorSeleccionado
    Next
Was it helpful?

Solution 2

As i wrote in Peter's comment, the problem was in the combo items duplication, that was behaving as something like "sharing items". I replace the duplication line for this ugliest and longer code, and the problem was solved. Hope it helps.

    ' Clear the items in the current combo,and fills with the ones in a dummy combo
    ControlCombo.Items.Clear()
    ' THIS LINE HAS BEEN COMMENTED AND REPLACED FOR THE FOLLOWING CODE
    ' ControlCombo.Items.AddRange(ComboPatron.Items.OfType(Of ListItem)().ToArray())

        Dim LSource As ListItem
        Dim LDestination As ListItem
        For i = 0 To ComboPatron.Items.Count - 1
            LSource = ComboPatron.Items(i)
            LDestination = New ListItem(LSource.Text, LSource.Value)
            ControlCombo.Items.Add(LDestination)
        Next

OTHER TIPS

The problem is the ViewState. If you are modifying dynamically the items inside a DropDownList () it's ViewState won't be updated so when you posting back the values the framework will look after the sent value inside the control's ViewState but the value will be mising so the SelectedValue property will not be set. If you want to use the posted values then get them from Request.Form[ddlName.ClientID] (i'm not sure about that the ClientID will be the correct index but the main idea is this).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top