Dropdownlist with invalid Selectedvalue because it does not exist in the item list

Asked

Viewed 3,016 times

1

I cannot understand what is wrong. Dropdown is in a Formview of a user control. The dropdown links to a database table that should return 2 id and name values

Formview

 <td><asp:DropDownList width="350px"
 ID="EntregaRegiaoDropDownList" runat="server" SelectedValue='<%# Bind ("Regiao") %>' DataSourceID ="SqlDataSource1" DataTextField ="RegiaoNome" DataValueField ="RegiaoID" Enabled ="False"></asp:DropDownList></td>

Code Behind userControl.ascx

public bool Editable
{
    get 
    {
        if (ViewState["editable"] != null)
        {
            return (bool)ViewState["editable"];
        }
        else
        {
            return true;
        }
    }
    set 
    {
        ViewState["editable"] = value;
    }
}
protected override void OnPreRender(EventArgs e)
{
    //Find o botao edit visible
    Button EditButton=
        FormView1 .FindControl ("EditButton") as Button;
    if (EditButton !=null )
    {
        EditButton .Visible =Editable ;
    } 
}

Dropdownlist' has a Selectedvalue which is invalid because it does not exist in the list of items. Parameter name: value

Exception Details: System.Argued tofrangeexception: 'Deliver dropdownlist' has a Selectedvalue which is invalid because it does not exist in the list of items. Parameter name: value

Source Error:

Line 30: {

Line 31: //Find the Visible Edit button

Line 32: Button Editbutton=

Line 33: Formview1 . Findcontrol ("Editbutton") as Button;

Line 34: if (Editbutton != null )

Source File: c: Users PC Documents Visual Studio

2012 Baumsgvo Websites Usercontrols Clientesdetalhes.ascx.Cs Line: 32

Stack Trace:

[Tofrangeexception argued: 'Deliver Dropdownlist' has a Selectedvalue which is invalid because it does not exist in the list of items. Parameter name: value]

1 answer

1


 <td>
   <asp:DropDownList width="350px" ID="EntregaRegiaoDropDownList" runat="server"
     SelectedValue='<%# Bind ("Regiao") %>' //VALOR ATRELADO = REGIAO
     DataSourceID="SqlDataSource1" 
     DataTextField="RegiaoNome" 
     DataValueField="RegiaoID"              //VALOR DO VALUE = REGIAOID
     Enabled="False"></asp:DropDownList>
 </td>

You must use the property Selectedvalue preferably at the time of editing a form, note that you arrow the Datavaluefield as Regiaoid and in the Selectedvalue puts only Regio, This makes it impossible for the application to run because it is running different fields, put in the Selectedvalue as Regiaoid also and run again that should solve, now to have a better control, I suggest you use the property Selectedvalue directly in the codebehind.
Remembering that it is only possible to select the value 'marrying' the fields Regiaoid = Regiaoid.

ERROR:
Selectedvalue='<%# Bind ("Region") %>'

  • Thank you !!!!! I managed to solve the error

  • 1

    I gave one bind before trying to catch the selectedvalue and it worked. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.