简体   繁体   中英

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

I have a Dropdownlist control in one of my ASCX page.

<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.

JS code to fetch these 'EmpName' and 'EmpID' values are as follows :

$(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.

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:

<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 %> :

$.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!");
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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