lstListBox.SelectedItem cannot be converted to 'String'

lstListBox.SelectedItem cannot be converted to 'String'

Post by Trace » Fri, 04 May 2007 23:31:37


How can I convert [ListBox.SelectedItem] to a string (what I thought was a
string)?
and/or
How can I assign the value of a selected item in a list box to Label1.Text?
using VB in VS2005.
Thanks, Tracey

I completed a {simple} class project that displays the value of what was
clicked in a List Box (which appears at the top of the page):
Response.Write("You clicked ")
Response.Write(ListBox.SelectedItem)
Class Project completed as designed without any error message.

But the whole page shifts/jumps down which doesn't look very elegant to me
so I created a label at the top of the page to display the value of what was
selected:
My 1st attempt: --- Label1.Text = "You clicked " : ListBox.SelectedItem ---
gets a blue squiggly line under ListBox.SelectedItem
My 2nd attempt: --- Label1.Text = ListBox.SelectedItem --- gets a blue
squiggly line under ListBox.SelectedItem

Both result in the following error: Value of type
'System.Web.UI.WebControls.ListItem' cannot be converted to 'String'.
 
 
 

lstListBox.SelectedItem cannot be converted to 'String'

Post by Trace » Fri, 04 May 2007 23:55:46

Just FYI I was give the solution Label1.Text = ListBox.Text



Label1.Text?
was
ListBox.SelectedItem ---

 
 
 

lstListBox.SelectedItem cannot be converted to 'String'

Post by Larry Bu » Sat, 05 May 2007 00:09:25


Should be

ListBox.SelectedItem.ToString()