Click here to Skip to main content
14,924,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get the selected value of a dropdownlist in code behind?

In my aspx page I have a dropdown list

XML
<asp:DropDownList ID="cityName" runat="server">
</asp:DropDownList>


Using Jquery, I'm adding data

C#
var city = $("#city").val();
    if (city.toString() != "") {

        var citySelect = $('#cityName');
        citySelect.append($('<option></option>').val('0').html('-Select City-'));
        for (i = 0; i < City.response_list.length; i++) {
            citySelect.append($('<option></option>').val(City.response_list[i].id_city + " - " + City.response_list[i].label).html(City.response_list[i].id_city + " - " + City.response_list[i].label));
        }
    }


In code behind when I try to get the selected value,
I'm getting "System.NullReferenceException: Object reference not set to an instance of an object"

How to get the selected value?
Posted
Updated 15-Jul-13 8:42am
v5
Comments
Asp_Learner 12-Jul-13 10:35am
   
Paste your code ,so that we can find out what exactly is happening
sunpop 15-Jul-13 14:44pm
   
I've updated my question by including the code.
ThatsAlok 13-Jul-13 1:37am
   
Agreed with ASP_learner we ought to know what excatly is your code, before responding
sunpop 15-Jul-13 14:44pm
   
I've updated my question by including the code.
footballpardeep 15-Jul-13 15:48pm
   
Are you sure,Items get added into the dropdownlist.
sunpop 16-Jul-13 1:00am
   
yes, Items are added, from that I'm selecting too. But in code behind I'm getting that 'null' problem.

C#
dropdownlist.selecteditem.text
   
Comments
sunpop 12-Jul-13 9:44am
   
dropdownlist.SelectedItem.Text gives me "System.NullReferenceException: Object reference not set to an instance of an object"
maneavnash 12-Jul-13 9:56am
   
show me your code............
sunpop 15-Jul-13 14:44pm
   
I've updated my question by including the code.
C#
dropdownlist1.SelectedValue
   
if you are interested in the displayed test in dropdownlist, you can try for dropdownList.SelectedText
   
v2
C#
string Svalu = dropdownlist1.SelectedItem.Tostring();
   
v2
dropdownlist1.selectedvalue if you want value
dropdownlist1.selectedtext if you want selected text
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900