Error with dropdownlist: There can not be multiple items selected in DropDownList

1

This error just came up in my GridView because I have 6 dropdownlist in the GridView in EditItemTemplate, and when I click Edit, this error occurs:

  

System.Web.HttpException: There can be multiple items   selected in DropDownList.

This is my Code-Behind:

 Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow AndAlso GridView1.EditIndex = e.Row.RowIndex Then
        Dim ddlParIDF As DropDownList = DirectCast(e.Row.FindControl("ddlParIDF"), DropDownList)
        Dim ddlFilaIDF As DropDownList = DirectCast(e.Row.FindControl("ddlFilaIDF"), DropDownList)
        Dim ddlTabIDF As DropDownList = DirectCast(e.Row.FindControl("ddlTabIDF"), DropDownList)
        ddlParIDF.Items.FindByValue(TryCast(e.Row.FindControl("lblParIDF"), Label).Text).Selected = True
        ddlFilaIDF.Items.FindByValue(TryCast(e.Row.FindControl("lblFilaIDF"), Label).Text).Selected = True
        ddlTabIDF.Items.FindByValue(TryCast(e.Row.FindControl("lblTabIDF"), Label).Text).Selected = True

        Dim ddlFilaMDF As DropDownList = DirectCast(e.Row.FindControl("ddlFilaMDF"), DropDownList)
        Dim ddlTabMDF As DropDownList = DirectCast(e.Row.FindControl("ddlTabMDF"), DropDownList)
        Dim ddlParMDF As DropDownList = DirectCast(e.Row.FindControl("ddlParMDF"), DropDownList)
        ddlFilaMDF.Items.FindByValue(TryCast(e.Row.FindControl("lblFilaMDF"), Label).Text).Selected = True
        ddlTabIDF.Items.FindByValue(TryCast(e.Row.FindControl("lblTabMDF"), Label).Text).Selected = True
        ddlParMDF.Items.FindByValue(TryCast(e.Row.FindControl("lblParMDF"), Label).Text).Selected = True
    End If
End Sub

I use a Label inside EditItemTemplate next to the DropDownList to save the last selected value of the dropdownlist, but I realize that if I remove the ".Selected = True" from two or more Label, this error no longer happens, but the disadvantage is that it will not save the last value chosen from the DropDownList.

To fill the DropDownList I use the ListItems within the gridview.

Or if someone knows that there is another way to maintain the selected value, if you could help me too.

Thanks to everyone!

    
asked by Isaias Toledo 04.06.2016 в 19:17
source

1 answer

1

In the second block you are using ddlTabIDF or in this one:

ddlFilaMDF.Items.FindByValue(TryCast(e.Row.FindControl("lblFilaMDF"), Label).Text).Selected = True
ddlTabIDF.Items.FindByValue(TryCast(e.Row.FindControl("lblTabMDF"), Label).Text).Selected = True
ddlParMDF.Items.FindByValue(TryCast(e.Row.FindControl("lblParMDF"), Label).Text).Selected = True

when it should be ddlTabMDF

    
answered by 06.06.2016 в 07:50