C# Get Dropdownlist Value Into A String?

Joined: 11/28/2008
User offline. Last seen 20 weeks 9 hours ago.

I have a (probably) novice question for C# .NET.

I have a drop down list with email addresses. These do not pull from a DB. That part works.

But I want to do the equivalent of this:

CODE
mail.From = DropDownList_Rep.SelectedItem.Value;

but I can't do that because I get:

QUOTE
Cannot implicitly convert type 'System.Web.UI.WebControls.ListItem' to 'string'

Any thoughts? Thanks!

UPDATE
This works: mail.From = DropDownList_Rep.SelectedItem.Value + "";
But that seems to be "tricking" the code. Surely it's not the correct way of doing it?

Joined: 11/28/2008
User offline. Last seen 1 year 5 weeks ago.
Have you tried adding

Have you tried adding .ToString() on the end of Value?

Who can bring a charge to God's elect? It is God who justifies!

Joined: 11/28/2008
User offline. Last seen 20 weeks 9 hours ago.
Ha! Yep, that did it too!

Ha! Yep, that did it too! /specool.gif" style="vertical-align:middle" emoid=":specool:" border="0" alt="specool.gif" /> I knew it would be something easy that I was just overlooking.

Wonder what makes the + ""; work that I was using to "trick" the code?

Joined: 11/28/2008
User offline. Last seen 1 year 5 weeks ago.
I'm not sure. I'm very new to

I'm not sure. I'm very new to C# myself but am getting exposed to it more and more each day. Also, I've been told that concatenating using + is slower than using string.Format("Hello {0}!", username). It's not as much of an issue in single instances, but in something like a look it can make a difference, or so I've been told. /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />

Who can bring a charge to God's elect? It is God who justifies!

The + "" is forcing a CAST to

The + "" is forcing a CAST to take place. So the .Value property is getting cast from ListItem to string here.