Surendra Sharma

Surendra Sharma

Search This Blog

Wednesday, July 15, 2015

Didn’t getting selected value of JQuery populated dropdownlist on server side

I was working on ASP.NET page which contains Country and State dropdown list. I filled country dropdown using JQuery AJAX. Now on selected value of Country I want to fill related states. I told my manager that it’s a simple task and will complete it in 3 hours including testing.

And next one hour I was just trying to get value of selected country in C# ;)

I tried with ddlCountry.selectedvalue but OMG I was not getting selected value.

How to get the dropdown selected value?

As we are filling dropdown list at client side so server side code don’t have any idea about the filled items. So in this situation use Request object as below

string country = Request[ddlCountry.UniqueID];

Now I was able to get the selected country in C# code.

Keep in mind that after getting value if there is another postback, you will not get the same country from Request[ddlCountry.UniqueID]. So during first time itself keep it in Viewstate so that you can use it after several post back as well.

One last thing, I completed this task in 3 hours as I told to my manager :)

Please leave your comments or share this tip if it’s useful for you.

No comments:

Post a Comment