0

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

1 Answer 1

1

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 (e.g. avoiding multiple <select> elements with same ID), use ClientID property of the control as selector, i.e. <%= 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!");
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Tetsuya Yamamoto. Adding 'ClientIDMode' attribute helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.