Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

I am binding dropdownlist items from database using jquery ajax().But the problem is i'm unable to access the dropdownlist selected value in buttonClick event at code behind.

Can any one help me, how to get selected value when listitems binded with $.ajax()

please find code:
C#
.aspx apge

<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/BindDatatoDropdown",
data: "{}",
dataType: "json",
success: function(data) {
$.each(data.d, function(key, value) {
$("#ddlCountry").append($("<option></option>").val(value.CountryId).html(value.CountryName));
});
},
error: function(result) {
alert("Error");
}
});
});
</script>

What I have tried:

I tried to get value from 

protected void btnSubmit_Click(object sender, EventArgs e)
        {
string val = ddlCountry.SelectedItem.Value;// here getting empty value
}
Posted
Updated 5-Oct-16 8:19am
v2

You shall use an Hidden Field to store the selected value in javascript and you can read the same in code behind, it should work.
 
Share this answer
 
Comments
Member 11570261 7-Oct-16 4:48am    
Thanking you.
Karthik_Mahalingam 7-Oct-16 7:41am    
welcome
asp.net doesn't know about any changes you make to server controls via javascript so it can't see your new items. If you are manipulating the dropdown via ajax then don't use an asp:DropDownList, instead just use a normal html <select> like

HTML
<select name="ddlCountry" id="ddlCountry">
<option value="UK">United Kingdom</option>
</select>


then in your click event you read the value using

C#
string country = Request.Form["ddlCountry"];
 
Share this answer
 
Comments
Member 11570261 7-Oct-16 4:47am    
Thanking you, It's working fine now.
use

C#
  $("#ddlcountry")
            .change(function () {

               

                var countryid= $(this).val();

}
 
Share this answer
 

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