将新选择添加到父级时,子列表框不会清空

时间:2017-02-11 19:31:03

标签: asp.net

ASP.NET WebForms - 在我的父ListBox中进行新选择时,它只是附加子ListBox而不是先清除它,然后添加附加列表。

的.aspx

function GetContacts() {

        var LstRight;
        var locationIDs = '';



        var LstRight = document.getElementById("<%=lstSelectedFranchises.ClientID %>"); 
        for (i = 0; i < LstRight.length; i++) {
            locationIDs = locationIDs + LstRight.options[i].value + ',';
        }

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: '<%= ResolveUrl("~/CallReportDetail.aspx/getContacts") %>',
            data: "{'jData':" + JSON.stringify(locationIDs) + "}",
            dataType: "json",
            success: function (result) {

                $('#lstAllContacts').empty();
                $('#lstSelectedContacts').empty();

                //alert(JSON.stringify(result.d));

                for (i = 0; i < result.d.length; i++)
                    $('#<%=lstAllContacts.ClientID %>').append($('<option>').text(result.d[i].Contact).val(result.d[i].ID));

            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert("There was a problem getting the contact list for your selected franchise(s): " + thrownError);

            }
        });
    }

查看

        <strong>Franchise(s)</strong><br />
    <table style="border:none">
        <tr>
            <td>
                <asp:TextBox ID="txtSearchFranchises" runat="server" onkeyup="return SearchFranchiseList();"></asp:TextBox><br />
            </td>
        </tr>
        <tr>
            <td>
                <asp:ListBox ID="lstAllFranchises" runat="server" SelectionMode="Multiple" Rows="10" Width="300px"></asp:ListBox>
            </td>
            <td>
                <asp:LinkButton ID="btnAddFranchises" runat="server" Text="Add >>" Style="text-align:center; width:100%" OnClientClick="return MoveFranchises('R', 0);" CssClass="btn btn-primary btn-sm"/><br />
                <asp:LinkButton ID="btnRemoveFranchises" runat="server" Text="<< Remove" Style="text-align:center; width:100%" OnClientClick="return MoveFranchises('L', 0);" CssClass="btn btn-primary btn-sm"/><br />
                <asp:LinkButton ID="btnAddFranchisesAll" runat="server" Text="Add All >>" Style="text-align:center; width:100%" OnClientClick="return MoveFranchises('R', 1);" CssClass="btn btn-primary btn-sm"/><br />
                <asp:LinkButton ID="btnRemoveFranchisesAll" runat="server" Text="<< Remove All" Style="text-align:center; width:100%" OnClientClick="return MoveFranchises('L', 1);" CssClass="btn btn-primary btn-sm"/>
            </td>
            <td>
                <asp:ListBox ID="lstSelectedFranchises" runat="server" SelectionMode="Multiple" Rows="10" Width="300px"></asp:ListBox>
            </td>
        </tr>
    </table><br /><br />


    <strong>Contact(s)</strong><br />
    <table style="border:none">
        <tr>
            <td>
                <asp:TextBox ID="txtSearchContacts" runat="server" onkeyup="return SearchContactList();"></asp:TextBox><br />
            </td>
        </tr>
        <tr>
            <td>
                <asp:ListBox ID="lstAllContacts" runat="server" SelectionMode="Multiple" Rows="15" Width="200px"></asp:ListBox>
            </td>
            <td>
                <asp:LinkButton ID="btnAddContacts" runat="server" Text="Add >>" Style="text-align:center; width:100%" OnClientClick="return MoveContacts('R', 0);" CssClass="btn btn-primary btn-sm"/><br />
                <asp:LinkButton ID="btnRemoveContacts" runat="server" Text="<< Remove" Style="text-align:center; width:100%" OnClientClick="return MoveContacts('L', 0);" CssClass="btn btn-primary btn-sm"/>
            </td>
            <td>
                <asp:ListBox ID="lstSelectedContacts" runat="server" SelectionMode="Multiple" Rows="15" Width="200px"></asp:ListBox>
            </td>
        </tr>
    </table><br /><br />

任何人都可以看到我出错的地方

1 个答案:

答案 0 :(得分:0)

解决:将.empty()更改为.length = 0

document.getElementById(“&lt;%= lstAllContacts.ClientID%&gt;”)。length = 0; document.getElementById(“&lt;%= lstSelectedContacts.ClientID%&gt;”)。length = 0;