简体   繁体   English

ASP.NET-使用JS设置DropDownList的值和文本属性

[英]ASP.NET - Set DropDownList's value and text attributes using JS

I have a Dropdownlist control in one of my ASCX page. 我的ASCX页面之一中有一个Dropdownlist控件。

<asp:DropDownList ID="demoddl" runat="server" onchange="apply(this.options[this.selectedIndex].value,event)" onclick="borderColorChange(this.id, 'Click')" onblur="borderColorChange(this.id)" CssClass="dropDownBox" DataTextField="EmpName" DataValueField="EmpID">

My objective is to fill this Dropdownlist with 'EmpID' as value attribute and 'EmpName' as text attribute. 我的目标是使用“ EmpID”作为值属性和“ EmpName”作为文本属性填充此下拉列表。

JS code to fetch these 'EmpName' and 'EmpID' values are as follows : 提取这些“ EmpName”和“ EmpID”值的JS代码如下:

$(document).ready(function () 
{
loadSavedFreeTextSearchCombo();
}


function loadSavedFreeTextSearchCombo() {

    var params = {
        loginID: $('#loginID').val()
    };

    var paramsJSON = $.toJSON(params);

    $.ajax({
        type: "POST",
        url: _WebRoot() + "/Ajax/EmpDetails.asmx/GetEmp",
        data: paramsJSON,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            $('#demoddl').empty();
            $('#demoddl').append($("<option></option>").val(0).html("--Employee Names --"));
            $.each(data.d, function (index, value) {
                $('#demoddl').append($("<option></option>").val(value.EmpID).html(value.EmpName));
            });

        },
        error: function () {
            showError("Failed to load Saved Search Data!");

        }
    });

}

Although the entire code runs without any error (the EmpDetails.asmx method returns the valid data successfully), the dropdwonlist doesn't get filled with the required data returned. 尽管整个代码运行都没有任何错误(EmpDetails.asmx方法成功返回有效数据),但dropdwonlist并没有填充返回的必需数据。

What am I doing wrong? 我究竟做错了什么? I guess somehting's wrong at my 'success' event code 我想我的“成功”事件代码出了点问题

Since you're intended to use DropDownList server control ID as selector, it is necessary to set ClientIDMode="Static" , especially if you're using <asp:ContentPlaceHolder> or <asp:Content> to prevent ASPX engine creating <select> element with id attribute containing dropdown's placeholder name: 由于您打算将DropDownList服务器控件ID用作选择器,因此有必要设置ClientIDMode="Static" ,尤其是在使用<asp:ContentPlaceHolder><asp:Content>来防止ASPX引擎创建<select> id属性包含下拉菜单的占位符名称的元素:

<asp:DropDownList ID="demoddl" runat="server" ClientIDMode="Static"
                  onchange="apply(this.options[this.selectedIndex].value,event)"
                  onclick="borderColorChange(this.id, 'Click')"
                  onblur="borderColorChange(this.id)" 
                  CssClass="dropDownBox" DataTextField="EmpName" DataValueField="EmpID">

If you cannot use ClientIDMode="Static" attribute for certain reasons (eg avoiding multiple <select> elements with same ID ), use ClientID property of the control as selector, ie <%= demoddl.ClientID %> : 如果由于某些原因不能使用ClientIDMode="Static"属性(例如, 避免使用具有相同ID的多个<select>元素 ),请使用控件的ClientID属性作为选择器,即<%= demoddl.ClientID %>

$.ajax({
    type: "POST",
    url: _WebRoot() + "/Ajax/EmpDetails.asmx/GetEmp",
    data: paramsJSON,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        $('#<%= demoddl.ClientID %>').empty();
        $('#<%= demoddl.ClientID %>').append($("<option></option>").val(0).html("--Employee Names --"));

        // recommended to check against undefined here
        $.each(data.d, function (index, value) {
            $('#<%= demoddl.ClientID %>').append($("<option></option>").val(value.EmpID).html(value.EmpName));
        });

    },
    error: function () {
        showError("Failed to load Saved Search Data!");
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM