SelectedValue which is invalid because it does not exist in the list of items

前端 未结 9 1722
孤独总比滥情好
孤独总比滥情好 2021-02-03 21:16

I encounter this problem repeatedly, and haven\'t a clue what is causing it. I get an exception in the DataBind: SelectedValue which is invalid because it does not exist i

相关标签:
9条回答
  • 2021-02-03 21:46

    I was getting the same error repeatedly and try ending up by not setting the default selected value to Index -1.

    I commented my code ddlDRIBidAmt.SelectedValue = -1

    This value was set at the time where my Page Controls were reset to default values.

    0 讨论(0)
  • 2021-02-03 21:49

    Apparently the solution I posted wasn't entirely effective... Eventually in my application I changed to this:

    listOrgs.Items.Clear();
    listOrgs.SelectedIndex = -1;
    listOrgs.SelectedValue = null;
    listOrgs.ClearSelection();     // Clears the selection to avoid the exception (only one of these should be enough but in my application I needed all..)
    listOrgs.DataSource = new Organization().DTListAll(SiteID);
    listOrgs.DataTextField = "OrganizationName"; 
    listOrgs.DataValueField = "OrganizationID"; 
    listOrgs.DataBind();
    
    0 讨论(0)
  • 2021-02-03 21:49

    I know its too late to answer, but what I tried is an dirty solution but it worked. After databinding, I am insert a item at index 0

    ddl.Items.Insert(0, new ListItem("---Select---","-1"));
    

    And on setting,

    I am placing try catch, In catch i am setting Value to -1

    0 讨论(0)
提交回复
热议问题