Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
I am trying to use select2 with ASPX calling a webmethod.

this is my control on the page
<input type="hidden" id="attendee" style="width:300px;"/> 


this is my javascript code
C#
$(document).ready(function () {

      $('#attendee').select2(
      {
          placeholder: 'Enter name',
          minimumInputLength: 1,
          allowClear: true,
          ajax:{
              type: "POST",
              url: "http://localhost:63830/project/TestClientPage2.aspx/GetClientList",
               dataType: 'json',
              data: function (term, page) {
                  return {
                      pageSize: 20,
                      pageNum: page,
                      searchTerm: term
                  };
              },
              results: function (data, page) {
                  //Used to determine whether or not there are more results available,
                  //and if requests for more data should be sent in the infinite scrolling
                  var more = (page * pageSize) &lt; data.Total;
                  return { results: data.results, more: more };
              }
          }


      });

  });


and this is my webmethod.
C#
public static string GetClientList(string searchTerm)
{
    return @"{""employees"": [{ ""firstName"":""John"" , ""lastName"":""Doe"" }]}";
}


the issue i am having is that the javascript is not calling the webmethod.
thank you
Posted
Updated 17-Jan-14 11:21am
v2
Comments
ZurdoDev 17-Jan-14 16:24pm    
Is select2 a jquery plugin? I am not familiar with code like this. I use $.ajax all the time but not like this.

What is select2()?

According to the code, it is a function which resides inside any Plugin.
Have you called any Plugin?

Next thing is, you need to see the Developer Tool's console tab, of there are any errors listed or not.
It will give you the hint about the issues you are facing currently and you can easily rectify them in code.

You can also debug the code by just writing debugger; inside the code and try to check if all the values are correct or not.
 
Share this answer
 
Define your web method like this:
C#
[System.Web.Services.WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string GetClientList(string searchTerm)
{
    return @"{""employees"": [{ ""firstName"":""John"" , ""lastName"":""Doe"" }]}";
}

And provide URL in ajax call like
HTML
url: '<%= ResolveUrl("~/trial.aspx/getResults") %>'

so make ajax call in .Select2() as below:

C#
ajax: {
    url: '<%= ResolveUrl("~/mypage.aspx/getResults") %>',
    dataType: 'json',
    type: "POST",
    params: {
        contentType: 'application/json; charset=utf-8'
    },
    quietMillis: 100,
    data: function (term, page) {
        return JSON.stringify({ q: term, page_limit: 10 });
    },
    results: function (data) {
        console.log(data);
        return { results: data };
    }
}

For more information, you can read http://stackoverflow.com/questions/20846024/select2-with-ajax-asp-net-4-0-webmethod-request[^]
 
Share this answer
 
v2
HI I have used the webservice method and handler. Select2js working fine with it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900