Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SelectedValue vs SelectedItem.Value of DropDownList

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

like image 987
vulcan raven Avatar asked Mar 05 '12 09:03

vulcan raven


People also ask

How can set the selected value of dropdown in asp net?

You can set the SelectedValue to the value you want to select. If you already have selected item then you should clear the selection otherwise you would get "Cannot have multiple items selected in a DropDownList" error. dropdownlist. ClearSelection(); dropdownlist.

What is DropDownList control?

The DropDownList control is a web server element that creates the drop-down menu and lets users select a single item from various options. You can add any number of items to the DropDownList. Almost all web pages contain a drop-down list, but it is mostly present in registration forms or sign-in pages.


1 Answers

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> 
like image 181
Adrian Iftode Avatar answered Sep 28 '22 12:09

Adrian Iftode