locked
How to add listItems in ListBox through ajax in Asp.net? RRS feed

  • Question

  • I want to move list box items from one listbox to another using button but not in postback. So I want to use ajax, through that I can add items in listbox at server side.

    My ajax code is working fine. But When I write the code to add listbox items, It through internal server error. Why?.

    This is my code.

    $('#right').click(function () {
                    //alert("Start process");
                    var item = "testing";
                    $.ajax({
                        url: 'ReportSalesAll.aspx/setRightListBoxitems',
                        type: 'post',
                        contentType: 'application/json',
                        data: JSON.stringify({listItems:item}),
                        dataType: 'json',
                        success: function (data) {
                            alert("result = " + data.d);  // I got the output If I don't add list items at server side
                        },
                        failure: function (response) {
                            alert(response.d);
                        },
                        error: function (error) {
                            console.log("Error:", error);
                        }
                    });
                });

    Asp.net code

    <div class="col-lg-1">
              <input type="button" id="left" value="<<" />
              <input type="button" id="right" value=">>" />
    </div>
    <div class="col-lg-3">
              <asp:ListBox ID="FirstRight" runat="server" SelectionMode="Multiple" Width="100%" Height="220"></asp:ListBox>
    </div>

    server side code:

            [System.Web.Services.WebMethod]
            public static string setRightListBoxitems(string listItems)
            {
                ReportSalesAll rs = new ReportSalesAll();
                
                // If I don't add below line. Ajax is working.
                
                rs.FirstRight.Items.Add(new ListItem(listItems));
                //rs.FirstRight.Items.Add(listItems);
                
                string item = listItems;
                return listItems;
            }

    Help me, I asked  this question on stack over flow, code project. But I didn't get anything from them. Thanks

    • Moved by Kristin Xie Monday, December 26, 2016 7:39 AM
    Saturday, December 24, 2016 10:34 AM

All replies

  • Hi Mohamed.Faisal,

    I am sorry to tell you that this forum is only covering the C# programming
    language, IDE, libraries, samples, and tools. Your ASP.Net related questions
    should be placed inside a forum at
    http://forums.asp.net

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 26, 2016 7:35 AM