Click here to Skip to main content
15,885,954 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.

C#
dropdownlist.selecteditem.text
 
Share this answer
 
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
 
Share this answer
 
if you are interested in the displayed test in dropdownlist, you can try for dropdownList.SelectedText
 
Share this answer
 
v2
C#
string Svalu = dropdownlist1.SelectedItem.Tostring();
 
Share this answer
 
v2
dropdownlist1.selectedvalue if you want value
dropdownlist1.selectedtext if you want selected text
 
Share this answer
 
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