44

I'm working on an old project written and then patched by several people over the years. At some places they have used SelectedValue property and other places they used SelectedItem.Value.

Question: Is SelectedValue just a syntactic sugar for SelectedItem.Value or SelectedValue works differently under the hood? Which one performs better?

Edit: SelectedItem.Text was replaced with SelectedItem.Value

5 Answers 5

63

SelectedValue returns the same value as SelectedItem.Value.

SelectedItem.Value and SelectedItem.Text might have different values and the performance is not a factor here, only the meanings of these properties matters.

<asp:DropDownList runat="server" ID="ddlUserTypes">
    <asp:ListItem Text="Admins" Value="1" Selected="true" />
    <asp:ListItem Text="Users" Value="2"/>
</asp:DropDownList>

Here, ddlUserTypes.SelectedItem.Value == ddlUserTypes.SelectedValue and both would return the value "1".

ddlUserTypes.SelectedItem.Text would return "Admins", which is different from ddlUserTypes.SelectedValue

edit

under the hood, SelectedValue looks like this

public virtual string SelectedValue
{
    get
    {
        int selectedIndex = this.SelectedIndex;
        if (selectedIndex >= 0)
        {
            return this.Items[selectedIndex].Value;
        }
        return string.Empty;
    }
}

and SelectedItem looks like this:

public virtual ListItem SelectedItem
{
    get
    {
        int selectedIndex = this.SelectedIndex;
        if (selectedIndex >= 0)
        {
            return this.Items[selectedIndex];
        }
        return null;
    }
}

One major difference between these two properties is that the SelectedValue has a setter also, since SelectedItem doesn't. The getter of SelectedValue is faster when writing code, and the problem of execution performance has no real reason to be discussed. Also a big advantage of SelectedValue is when using Binding expressions.

edit data binding scenario (you can't use SelectedItem.Value)

<asp:Repeater runat="server">
 <ItemTemplate>
     <asp:DropDownList ID="ddlCategories" runat="server" 
                       SelectedValue='<%# Eval("CategoryId")%>'>
     </asp:DropDownList>
 </ItemTemplate>
</asp:Repeater>
5
  • Thanks for pointing me out.. I mistakenly typed .Text.. actually .Value is being used everywhere. So, the inner implementation of both properties (SelectedItem.Value & SelectedValue) are same? Isn't SelectedItem (two dots) syntax more complex compared to one-hop-away SelectedValue? Commented Mar 5, 2012 at 15:14
  • Thank you Adrian. Can you detail about the Binding expressions advantage? Would be very obliged! Commented Mar 6, 2012 at 0:37
  • @vulcanraven, welcome, I updated the answer with a databinding example Commented Mar 6, 2012 at 6:04
  • I'm not sure that your "under the hood" section is correct. I just discovered that the SelectedItem will return you an item selected based on its value, not the index. This means if you have multiple items with the same Value, its possible that SelectedItem returns the wrong one.
    – Rachel
    Commented Jan 15, 2013 at 13:47
  • I used Reflector to see it Commented Jan 16, 2013 at 9:02
9

One important distinction between the two (which is visible in the Reflected code) is that SelectedValue will return an empty string if a nothing is selected, whereas SelectedItem.Value will throw a NullReference exception.

4

They are both different. SelectedValue property gives you the actual value of the item in selection whereas SelectedItem.Text gives you the display text. For example: you drop down may have an itme like

<asp:ListItem Text="German" Value="de"></asp:ListItem>

So, in this case SelectedValue would be de and SelectedItem.Text would give 'German'

EDIT:

In that case, they aare both same ... Cause SelectedValue will give you the value stored for current selected item in your dropdown and SelectedItem.Value will be Value of the currently selected item.

So they both would give you the same result.

0
0

In droupDown list there are two item add property.

1) Text 2) value

If you want to get text property then u use selecteditem.text

and If you want to select value property then use selectedvalue property

In your case i thing both value and text property are the same so no matter if u use selectedvalue or selecteditem.text

If both are different then they give us different results

1
  • This comment is confusing. Yes, selectedValue property and selectedItem.Text CAN be the same, but VERY frequently they are NOT as per example above where SelectedValue is "de" and SelectedItem.Text is "German" or SelectedValue is "CA" and SelectedItem.Text is "California". Most commonly, SelectedValue is the string representation of a DB primary index value and SelectedItem.Text is value from a specific column for the same row in a DB table. Commented Mar 18, 2016 at 17:03
0

Be careful using SelectedItem.Text... If there is no item selected, then SelectedItem will be null and SelectedItem.Text will generate a null-value exception.

.NET should have provided a SelectedText property like the SelectedValue property that returns String.Empty when there is no selected item.

Not the answer you're looking for? Browse other questions tagged or ask your own question.