DropDownList with Invalid SelectedValue because it does not exist in the list of items

1

I can not understand what's wrong. DropDown is in a Formview of a user control. The dropDown binds 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.ArgumentOutOfRangeException:   'DeliveryRegiaoDropDownList' 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 \ WebSites \ BaumSGVO \ UserControls \ ClientsDetails.ascx.cs Line:   32

     

Stack Trace:

     

[ArgumentOutOfRangeException: 'DeliveryRegiaoDropDownList' has a   SelectedValue which is invalid because it does not exist in the list   of items. Parameter name: value]

    
asked by anonymous 25.04.2014 / 11:20

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 should instead use the SelectedValue property at the time of editing a form, please note that you DataValueField as RegiaoID and < strong> SelectedValue only places Region , this makes it impossible for the application to run because it is set to different fields , put < strong> RegiaoID and also run again, which should solve, now for better control, I suggest you use the SelectedValue property directly in codebehind. Home Remember that you can only select the value 'matching' the RegionID = RegiaoID fields.

  

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

    
25.04.2014 / 16:20