[c#] Cant get text of a DropDownList in code - can get value but not text

I am using ASP.NET 3.5

I have a drop-down list called lstCountry with an item in it like this:

<asp:ListItem Value="United States">Canada</asp:ListItem> 

This will display Canada but in code the value will be "United States". How can I retrieve the value "Canada" also in my code?

I have tried all of these and all of them return "United States"

lstCountry.Text
lstCountry.SelectedValue
lstCountry.SelectedItem.Text

My Drop Down list:

 <asp:DropDownList ID="lstCountry" runat="server" Width="200px">
              <asp:ListItem>Please Select</asp:ListItem>
  <asp:ListItem>United States</asp:ListItem>
  <asp:ListItem Value="United States">Canada</asp:ListItem>
 </asp:DropDownList>

How I read the value in code:

    Dim country As String
    country = lstCountry.SelectedItem.Text

This question is related to c# .net asp.net vb.net drop-down-menu

The answer is


try

lstCountry.SelectedItem.Text

What about

lstCountry.Items[lstCountry.SelectedIndex].Text;

You can try

lstCountry.SelectedItem.Text

Have a look here, this has a proof-of-concept page and demo you can use to get anything from the drop-down: asp:DropDownList Control Tutorial Page


had the same problem and just solved it, i used string [variable_Name] =dropdownlist1.SelectedItem.Text;


AppendDataBoundItems="true" needs to be set.


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to .net

You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to use Bootstrap 4 in ASP.NET Core No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible Update .NET web service to use TLS 1.2 EF Core add-migration Build Failed What is the difference between .NET Core and .NET Standard Class Library project types? Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies Nuget connection attempt failed "Unable to load the service index for source" Token based authentication in Web API without any user interface

Examples related to asp.net

RegisterStartupScript from code behind not working when Update Panel is used You must add a reference to assembly 'netstandard, Version=2.0.0.0 No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization How to use log4net in Asp.net core 2.0 Visual Studio 2017 error: Unable to start program, An operation is not legal in the current state How to create roles in ASP.NET Core and assign them to users? How to handle Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause() ASP.NET Core Web API Authentication Could not load file or assembly 'CrystalDecisions.ReportAppServer.CommLayer, Version=13.0.2000.0 WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery

Examples related to vb.net

How to get parameter value for date/time column from empty MaskedTextBox HTTP 415 unsupported media type error when calling Web API 2 endpoint variable is not declared it may be inaccessible due to its protection level Differences Between vbLf, vbCrLf & vbCr Constants Simple working Example of json.net in VB.net How to open up a form from another form in VB.NET? Delete a row in DataGridView Control in VB.NET How to get cell value from DataGridView in VB.Net? Set default format of datetimepicker as dd-MM-yyyy How to configure SMTP settings in web.config How to implement drop down list in flutter? How can I create a dropdown menu from a List in Tkinter? How can I close a dropdown on click outside? Making a drop down list using swift? HTML: Select multiple as dropdown How to get selected value of a dropdown menu in ReactJS Avoid dropdown menu close on click inside Bootstrap 3 dropdown select How to make a drop down list in yii2? Android custom dropdown/popup menu