当我在客户端创建选项时,如何在服务器端获取下拉选择值

dec*_*der 2 c# asp.net jquery

我的javascript代码

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

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

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

       });
   }
Run Code Online (Sandbox Code Playgroud)

在服务器端我希望通过dropDist.selectedItem获取价值.但我无法获得价值如何做到这一点.

int DistrictId = Int32.Parse((dropDist.SelectedValue).ToString());
Run Code Online (Sandbox Code Playgroud)

我如何获得服务器端的下拉选择值?任何帮助我非常感谢.

Yur*_*kiy 6

如果在javascript中添加选项,则无法从下拉列表中获取选定值.此外,您也丢失了SelectedIndexChanged事件处理程序.如果您需要在客户端填充下拉列表并且仍然能够使用SelectedValue属性和SelectedIndexChanged事件,则需要开发自己的ajax服务器控件.或者您可以使用AjaxControlToolkit库中的ComboBox.

尽管如此,如果您仍然希望在客户端上使用常规的DropDown,那么您可以在服务器上获取所选值: string dropDistSelectedValue = Request.Form[dropDist.UniqueID];