2

my javascript code

$().ready(function () {
       $.ajax({
           type: "POST",
           url: "../WebService.asmx/GetDistricts",
           data: "{}",
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           success: function (msg) {
               BindDist(msg.d);
           }
       });
   });

as

function BindDist(msg) {
       $.each(msg, function () {

           $("#dropDist").append($("<option></option>").val(this['DistrictId']).html(this['Name']));

       });
   }

in server side i want to get value by dropDist.selectedItem.but i can't get value how to do it.

int DistrictId = Int32.Parse((dropDist.SelectedValue).ToString());

how i get the dropdown selected value in server side?any help me is highly appreciated.

decoder
  • 886
  • 22
  • 46

1 Answers1

6

You can't get selected value from dropdown if you adding options in javascript. Also, you lost SelectedIndexChanged event handler as well. If you need to fill dropdown on client and still have ability to use SelectedValue property and SelectedIndexChanged event you need to develop own ajax server control. Or you can use some like ComboBox from AjaxControlToolkit library.

Despite of all above, if you still want to use regular DropDown filled on client then you can get selected value on server as there: string dropDistSelectedValue = Request.Form[dropDist.UniqueID];

Yuriy Rozhovetskiy
  • 22,270
  • 4
  • 37
  • 68
  • Hi Yuri. I have a very similar problem. Hope you have time to check it out. thanks, https://stackoverflow.com/questions/54210335/get-selected-item-value-on-a-server-side-button-in-datatables?noredirect=1&lq=1 –  Jan 16 '19 at 04:21
  • ...also "You can't get selected value from dropdown if you adding options in javascript." So, is my topic impossible? +1 for your work thanks. –  Jan 16 '19 at 04:31