[Solved] Can't set the SelectedValue programmatically when the value member is a string

1 Answer 14 Views
DropDownList
YF
Top achievements
Rank 1
Iron
YF asked on 17 Mar 2026, 10:51 PM

I use a RadDropDownList, I want to give the user the ability to search the items in the drop down so I use the default DropDownStyle = DropDown with AutoCompleteMode = SuggestAppend.

However, I want to ensure that the user selects a valid item from the list and doesn't leave random text. So I created the extension method EnsureValidSelection() below that remembers the original value, and on validation resets it to that original value if no valid value is now selected.

This works well when the ValueMember is an int. But when it's a string, it ignores the code that sets the SelectedValue.

public static void EnsureValidSelection(this RadDropDownList combo)
{
    object originalValue = null;
    combo.Enter += DropDownEnter;
    combo.SelectedValueChanged += DropDownSelectedValueChanged;
    combo.Validated += DropDownValidated;
    return;

    void DropDownEnter(object sender, EventArgs e)
    {
        originalValue = ((RadDropDownList)sender).SelectedValue;
    }

    void DropDownSelectedValueChanged(object sender, EventArgs e)
    {
        if (originalValue != null && combo.SelectedValue != null)
            originalValue = combo.SelectedValue;
    }

    void DropDownValidated(object sender, EventArgs e)
    {
        if (combo.SelectedValue == null)
            combo.SelectedValue = originalValue;
        originalValue = null;
    }
}

1 Answer, 1 is accepted

Sort by
0
Accepted
Martin Ivanov
Telerik team
answered on 24 Mar 2026, 12:42 PM

Hello Yossi,

To get the desired behavior you can use the Text property instead of SelectedValue. For example:

void DropDownValidated(object sender, EventArgs e)
{
    if (combo.SelectedValue == null && originalValue != null)
    {
        var item = combo.Items.FirstOrDefault(i => Equals(i.Value, originalValue));                    
        combo.Text = item.Text;
    }
    originalValue = null;
}

I hope that helps.

Regards,
Martin Ivanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

YF
Top achievements
Rank 1
Iron
commented on 24 Mar 2026, 01:23 PM

Thanks Martin, this works.

I'm just wondering if there's an explanation for why SelectedValue doesn't always work (and why SelectedIndex never works).

Tags
DropDownList
Asked by
YF
Top achievements
Rank 1
Iron
Answers by
Martin Ivanov
Telerik team
Share this question
or