Connecting Tech Pros Worldwide Forums | Help | Site Map

Drop down lists

Member
 
Join Date: Jul 2009
Posts: 41
#1: Sep 10 '09
I feel like an idiot, but I can't figure out how to get the text from my drop down list.

I've tried all sorts of things like:

ddStatus.SelectedValue
ddStatus.SelectedItem
ddStatus.SelectedItem.Value
ddStatus.SelectedValue.ToString()

nothing seems to work.

Am I doing something wrong?

Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#2: Sep 10 '09

re: Drop down lists


  • ddStatus.SelectedItem: gives you the ListItem that was selected (the lowest index if more than one selection is made).
  • ddStatus.SelectedItem.Text: will give you the "text" for the selected
    ListItem.
  • ddStatus.SelectedItem.Value: will give you the "value" for the selected ListItem
  • ddStatus.SelectedValue.ToString(): will give you a String representation of the "value" of the ListItem selected....ddlStatus.SelectedValue will return the value of the selected item in the list control (is whatever you set the ListItem's value to when you populated the DropDownList)
Newbie
 
Join Date: Sep 2009
Posts: 3
#3: Sep 10 '09

re: Drop down lists


Try this:

Expand|Select|Wrap|Line Numbers
  1. var select = document.getElementById ("yourSelectElement");
  2. var txt = select.options[select.selectedIndex].text;
  3.  
  4.  
Watch these links:
selectedIndex, text
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#4: Sep 10 '09

re: Drop down lists


EisBear, that's great if you want to access the value client side (using JavaScript), however I have a feeling that the OP was wondering how to access the value Server Side in their .NET code :)
Member
 
Join Date: Jul 2009
Posts: 41
#5: Sep 10 '09

re: Drop down lists


Thanks for the replies, and yes, I am trying to access server side.

Here's the code I'm playing with:

Expand|Select|Wrap|Line Numbers
  1. Status: 
  2.                 <asp:DropDownList ID="ddStatus" runat="server" Height="22px" Width="128px">
  3.                     <asp:ListItem Selected="True"></asp:ListItem>
  4.                     <asp:ListItem Value="Closed" Text="Closed"/>
  5.                     <asp:ListItem Value="Closed - Minor" Text="Closed - Minor"/>
  6.                     <asp:ListItem Value="JWDTS" Text="JWDTS"/>
  7.                     <asp:ListItem Value="Notification" Text="Notification"/>
  8.                     <asp:ListItem Value="Open" Text="Open"/>
  9.                     <asp:ListItem Value="Pending" Text="Pending"/>
  10.                     <asp:ListItem Value="Reviewing" Text="Reviewing"/>
  11.                 </asp:DropDownList>
  12.  
Expand|Select|Wrap|Line Numbers
  1. MsgBox(ddStatus.SelectedValue)
  2.         If ddStatus.SelectedItem.ToString = "" Then
  3.             MsgBox("'Status' is empty")
  4.             Return False
  5.         End If
  6.  
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#6: Sep 10 '09

re: Drop down lists


Well that wont work.

SelectedItem is a property of your DropDownList that returns a ListItem.

If you call the ToString() method on the ListItem it's likely to display something like "System.Web.UI.WebControls.ListItem".

That's not what you want.

You want use the Text Property of the ListItem. This will return you the String that is displayed to the user...

eg: ddStatus.SelectedItem.Text

If you don't want the String being displayed to the user, and you want to check the Value that the user selected (because these can differ) then you'd either use the ListItem's Value property

eg: ddStatus.SelectedItem.Value.ToString()

Or you'd use the DropDownList's SelectedValue property:

eg: ddStatus.SelectedValue.ToString()


Please note that you will not be able to use the MessageBox in a Web Application. This is only available to Desktop Applications .....

Put a Label or a Localize control on the web page and set it's Text property to whatever is selected if you want to see it.

If you have a label on the page

eg:<asp:Label ID="myMessage" runat="server" />

Then you'd be able to display the selected item's text like:

myMessage.Text = ddStatus.SelectedItem.Text
Member
 
Join Date: Jul 2009
Posts: 41
#7: Sep 10 '09

re: Drop down lists


Okay, figured it out.

I guess it didn't like a blank item. When I took that out, it worked fine.

I played around with it a bit and found that

<asp:ListItem Value="Blank" Text=""/>

is acceptable.
Reply