locked
'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items RRS feed

  • Question

  • User1028962535 posted

    Hello

    I have a gridview with a dropdownlist populated with list items like this (see below).. in edit mode I can update the dropdownlist , no problem. However if I try to edit the same record again I get  this error 

    'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items"...any idea what I am doing wrong?

          <asp:DropDownList ID="DropDownList10" runat="server"
                           SelectedValue ='<%# Bind("ProdLoc")%>' Height="25px" Width="56px">
                            <asp:ListItem></asp:ListItem>
                           <asp:ListItem>CL</asp:ListItem>
                           <asp:ListItem>CR</asp:ListItem>
                           <asp:ListItem>FL</asp:ListItem>
                           <asp:ListItem>FR</asp:ListItem>
                          <asp:ListItem>None</asp:ListItem>
                        </asp:DropDownList>

                 <asp:RequiredFieldValidator ID="RequiredFieldValidator99" runat="server" ErrorMessage="Required" ControlToValidate ="Dropdownlist10"></asp:RequiredFieldValidator>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label100" runat="server" Text='<%# Bind("ProdLoc") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

    thanks

    Thursday, April 2, 2015 8:24 AM

Answers

  • User61956409 posted

    Hi Dan5,

    Thanks for your post.

    I create the following sample to reproduce your problem. The value of the field “RoleID” is 1 or 2, and I add two ListItems (Admin, User). In this situation, selectedvalue will not be found in ListItems, and I get the error as yours.

    <asp:TemplateField HeaderText="RoleID">
        <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%#Eval("RoleID") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:DropDownList ID="ddlrole" runat="server" SelectedValue='<%#Eval("RoleID") %>'>
                <asp:ListItem>Admin</asp:ListItem>
                <asp:ListItem>User</asp:ListItem>
            </asp:DropDownList>
        </EditItemTemplate>
    </asp:TemplateField> 
    

    If I add the following two ListItems (1, 2), the code will work fine. So please ensure the selectedvalue could be found in ListItems.

    <asp:TemplateField HeaderText="RoleID">
        <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%#Eval("RoleID") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:DropDownList ID="ddlrole" runat="server" SelectedValue='<%#Eval("RoleID") %>'>
                <asp:ListItem>1</asp:ListItem>
                <asp:ListItem>2</asp:ListItem>
            </asp:DropDownList>
        </EditItemTemplate>
    </asp:TemplateField> 
    

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 2, 2015 11:25 PM

All replies

  • User-1313211662 posted

    Yow can not give the selected value which is not present in the dropdown list item.

    Check for the selectedvalue you are giving present in the dropdown or not.

    Thursday, April 2, 2015 8:43 AM
  • User1028962535 posted

    If I can update the record successfully on my first try to something in the dropdownlist, why can't I amend it on my second try?..I don't understand what's going on

    Thursday, April 2, 2015 9:03 AM
  • User61956409 posted

    Hi Dan5,

    Thanks for your post.

    I create the following sample to reproduce your problem. The value of the field “RoleID” is 1 or 2, and I add two ListItems (Admin, User). In this situation, selectedvalue will not be found in ListItems, and I get the error as yours.

    <asp:TemplateField HeaderText="RoleID">
        <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%#Eval("RoleID") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:DropDownList ID="ddlrole" runat="server" SelectedValue='<%#Eval("RoleID") %>'>
                <asp:ListItem>Admin</asp:ListItem>
                <asp:ListItem>User</asp:ListItem>
            </asp:DropDownList>
        </EditItemTemplate>
    </asp:TemplateField> 
    

    If I add the following two ListItems (1, 2), the code will work fine. So please ensure the selectedvalue could be found in ListItems.

    <asp:TemplateField HeaderText="RoleID">
        <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%#Eval("RoleID") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:DropDownList ID="ddlrole" runat="server" SelectedValue='<%#Eval("RoleID") %>'>
                <asp:ListItem>1</asp:ListItem>
                <asp:ListItem>2</asp:ListItem>
            </asp:DropDownList>
        </EditItemTemplate>
    </asp:TemplateField> 
    

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 2, 2015 11:25 PM
  • User1028962535 posted

    Thank you  very much, for taking the time to explain this.

    Friday, April 3, 2015 5:23 AM