You must Sign In to post a response.
  • Category: .NET

    how to assign the value selected in the list box to a string variable?

    The forum thread has not been reviewed by Editors yet. Readers are advised to use their best judgement before accessing this thread.
    This thread will be reviewed shortly.
    If you think this thread contain inappropriate content, please report to webmaster.
    i have a list box that has the options of different professions(like doctor,lawyer,teacher,...)
    i want to know how to assign the selected option in the run time to a string variable

    if the options are :

    then plz specify the exact c# code format

    thanks in advance
  • #189
    hi shruthi

    You have not specified whether you are using a databound grid. you can access the selected items text by

    YourStringVariable = Listbox1.SelectedItem.text

    If you are using a databound list then you may be binding a text field(What is displayed , Property named DataTextField ) and a value field (Which is hidden but used to identify the item in the database, Property named DataValueField).

    you can access the value of the itemselected by

    YourStringVariable = Listbox1.SelectedItem.value


  • #191
    Hi Shruthi,
    U can assign by using the following code
    String Str=lstBox.selecteditem.text;


  • #113486
    String str =lstBox.selecteditem.text;

  • #193771
    string sr=listbox.selecteditem.Text
    to obtain the text you can use this.
    if you need the value then
    string sr= listbox.selected.item.value

  • #220655
    string itemtext=ListBox1.SelectedItem.Text

    string itemvalue=ListBox1.SelectedItem.Value

  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.