locked
How to selected option in listbox by value RRS feed

  • Question

  • User1487175000 posted

    Hi,

    I populate a list box with jquery after that i want to select a option dynamically by value. But i havent successed. 

    Here is list

    <asp:ListBox ID="listOfOpenBills" runat="server" Width="95%" Height="50px" CssClass="openBill"></asp:ListBox>

    At page load populate list

            function getOpenBilllist() {
                $(function () {
                    $.ajax({
                        type: "POST",
                        url: "businessLounge.aspx/getOpenBillList",
                        data: {},
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (r) {
                            var openbillList = $('.openBill');
                            openbillList.empty();
                            $.each(r.d, function () {
                                openbillList.append($("<option></option>").val(this['Value']).html(this['Text']));
                            });
                        },
                        error: function () {
                            alert("Error");
                        }
                    });
                });
            }

    Script: this method add the article in the bill and getOpenBilllist(); method re-populate the list to get latest list of open bills. 

            function addArticleToBill(articleID) {
                $(function () {
                    var uid = $("#<% = uID.ClientID %>").val();
                    var contactPersonID = getContactPersonID();
                    if (typeof contactPersonID == "undefined") {
                        alert("Please Select Contact Person");
                    } else {
                        $.ajax({
                            type: "POST",
                            url: "businessLounge.aspx/addArticleInBill",
                            data: "{'articleID':'" + articleID + "','contactPersonID':'" + contactPersonID + "','uid':'" + uid + "'}",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function (r) {
                                getBill(contactPersonID, '');
                                getOpenBilllist();
    $('.openBill').val(contactPersonID); }, error: function () { alert("Error"); } }); } }); }

    Problem:

    In success after re-populate the list i want to select the option in the list $('.openBill').val(contactPersonID); but option is not selected. Option is in the list. I dont know it not selected by my code. There is no error in debug window as well.

    Thanks in advance

    /Shahid

    Wednesday, June 19, 2019 6:43 PM

Answers

  • User1487175000 posted

    Hi,

    Get answer at stackoverflow.

    Because $.ajax() is asynchronous I cannot select the option after calling getOpenBilllist();.

    Instead, you can pass the value to the function and so select the option there:

    so updated function like this.

            function getOpenBilllist(contactPersonID) {
                $(function () {
                    $.ajax({
                        type: "POST",
                        url: "businessLounge.aspx/getOpenBillList",
                        data: {},
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (r) {
                            var openbillList = $('.openBill');
                            openbillList.empty();
                            $.each(r.d, function () {
                                openbillList.append($("<option></option>").val(this['Value']).html(this['Text']));
                            });
                            if (contactPersonID !== "") {
                                openbillList.val(contactPersonID);
                            }
                        },
                        error: function () {
                            alert("Error");
                        }
                    });
                });
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 19, 2019 8:02 PM