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
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.
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.
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
They are both different.
SelectedValue
property gives you the actual value of the item in selection whereasSelectedItem.Text
gives you the display text. For example: you drop down may have an itme likeSo, in this case
SelectedValue
would bede
andSelectedItem.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 andSelectedItem.Value
will be Value of the currently selected item.So they both would give you the same result.
SelectedValue
returns the same value asSelectedItem.Value
.SelectedItem.Value
andSelectedItem.Text
might have different values and the performance is not a factor here, only the meanings of these properties matters.Here,
ddlUserTypes.SelectedItem.Value == ddlUserTypes.SelectedValue
and both would return the value "1".ddlUserTypes.SelectedItem.Text
would return "Admins", which is different fromddlUserTypes.SelectedValue
edit
under the hood, SelectedValue looks like this
and SelectedItem looks like this:
One major difference between these two properties is that the
SelectedValue
has a setter also, sinceSelectedItem
doesn't. The getter ofSelectedValue
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)