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

为君一笑 提交于 2020-01-04 15:31:10

问题


Here's the scenario:

The selections in DropDownList are bound from a DB Table listing the acceptable values. At one point in time the values were:

One Two Three Four

At some point later, the list of acceptable values change to:

One Two Four Five

However, the field in the DB that stores the dropdown's value still contains a value of "Three" on some rows. When loading one of those row and setting SelectedValue as such:

dd.SelectedValue = data.Field; // where data.Field == "Three"

...an error is thrown stating: 'dd' has a SelectedValue which is invalid because it does not exist in the list of items.

Data clean up isn't an option here. It would cause problems for customers as the stored values aren't invalid choices for data that's already created, but are invalid choices for newly created data.

How have others handled this situation?


回答1:


We do have this kind of situation here.

When that happens, I manually add the missing item to the dropdownlist, but in a red font.

If the user tries to re-save the item, the red items are considered inactives and invalid. A valid choice (non-red) must then be picked from the drop-down list.




回答2:


Assuming data.Field is actually a string here, I would do:

ListItem itemToSelect = dd.Items.FindByText(data.Field);
if(itemToSelect != null)
{
     dd.SelectedItem = itemToSelect;
}



回答3:


You could add an extra column in the database table for the dropdown values called 'Active' which can be either true or false. Then instead of deleting an old value, you just mark it as inactive. You should have a foregin key contraint from the customer to the list of acceptable values to ensure that you cannot delete a value from the table if there are still some customers using it.

In the client, you can show customers who are using inactive types in a different color, and have a validation method that doesn't allow you to change a customer from an active type to an inactive type, but allows customers with inactive types to remain on that setting.




回答4:


so the old value, in this case "Three", is still present in the list table, but just deactivated or is it removed from the table altogether?

if the former, then setup two separate views, one which only includes active items and is used for new data entry, and one which includes all items and is used for viewing historical transactions.




回答5:


So you'll have to add the "historically accurate but now defunct" value into your drop-down list, but you can use a RequiredFieldValidator to prevent the user from saving this value back to the database. The RequiredFieldValidator's ControlToValidate is set to the DropDownList you wish to validate, and then you can set the InitialValue property to the invalid value. Now in your save method you can check the bool Page.IsValid before you save the page to the database. The message on your RequiredFieldValidator could be something like "This value is no longer acceptable due to ..."

Hope this helps!




回答6:


We also have a routine to check the DropDownList for the item first, then add it to the list if it isn't there. Same idea as Danny mentioned above, but I like his additional idea of adding it in red. Even though its the same idea, I thought it would be valuable to post our full routine. It iterates through the list using foreach to look for the value string. That could be made more efficient by using FindByText as Justin indicated above.

protected bool SafeSetDropDownValue(DropDownList ddl, string value, string text, bool addItemIfNotFound)
{
    // first make sure that drop down list has been data bound so that all the options are in there
    ddl.DataBind();

    // look for value in the list of dropdown values
    // (can't use try/catch because exception doesn't happen until later)
    bool found = false;
    bool selected = true;
    foreach (ListItem li in ddl.Items)
    {
        if (li.Value == value)
        {
            found = true;
        }
    }

    if (found)
    {
        ddl.SelectedValue = value;
    } 
    else 
    {
        // the value wasn't in the list,
        // so if addItem is true, then add the value to the list and then set the value 
        if (addItemIfNotFound)
        {
            ListItem li = new ListItem(text, value);
            ddl.Items.Add(li);
            ddl.SelectedValue = value;
        }
        else
        {
            // didn't find it and didn't add it
            selected = false;
        }
    }
    return selected;
}


来源:https://stackoverflow.com/questions/1869150/dropdownlist-has-a-selectedvalue-which-is-invalid-because-it-does-not-exist-in-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!