0

Is it possible to retrieve value or date from calling a webmethode from javascript, here is a sample code:

//This method is in a webservice.asmx file.
[WebMethod]
public List<tbl_City> GetAllCitiesByCountry(int countryID)
{

    return Cities.GetAllCitiesByCountry(CountryID: countryID);
}


<script language="javascript" type="text/javascript">

function fillCities() {
    var dropDownList = document.getElementById('<%=DropDownList_Country.ClientID %>');
    var selectedIndex = dropDownList.selectedIndex;
    var value = dropDownList[selectedIndex].value;

   WebService.GetAllCitiesByCountry(parseInt(value.toString()), onSuccess, null, "");

}

   function onSuccess(result){
      alert(result[0].(PropertyName));
      }

The variable x doesn't retrieve anything and I guess that it produce an error. I have tried to define an array but still it didn't work. Any idea ?

Edit:

The above code have been altered and is now an answer to my question along with the answer below which used JQuery.

RAS
  • 8,100
  • 16
  • 64
  • 86
yahya kh
  • 751
  • 3
  • 11
  • 23

3 Answers3

2

Use Json response with Jquery, Its realy cool and easy.

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class CitiService : WebService
{
    [WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<tbl_City> GetAllCitiesByCountry(int countryID)
    {
     List<tbl_City> cities = GetCities(countryID);
     JavaScriptSerializer js = new JavaScriptSerializer();
        var jsonObj = js.Serialize(cities);
        Context.Response.Clear();
        Context.Response.Write(jsonObj);
        Context.Response.End();
     }
 }

on ASp.net page

<script language="javascript" type="text/javascript">
 $.ajax({
        url: '<%= ResolveClientUrl("~/CitiService.asmx/GetAllCitiesByCountry") %>',
        dataType: "json",
        data: "{countryID:'100'}",
        success: function (result) {
            alert(result.d.tbl_City.Length) // loop here i.e. foreach to insert in to grid
        }
      });

dipak
  • 2,011
  • 2
  • 17
  • 24
  • on the third line of the WebMethod it gave an error: Cannot access a disposed object. Object name: 'DataContext accessed after Dispose.'. – yahya kh Dec 08 '11 at 12:09
  • i fixed the problem, turned out that the DataContext dispose of it self after the using statment finishes. – yahya kh Dec 08 '11 at 12:59
  • You don't need to manually JSON serialize that. If you just return `cities`, ASP.NET will automatically handle running it through JavaScriptSerializer for you. More info: http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/ – Dave Ward Feb 08 '12 at 16:34
1

you can do this easily with JQuery and ASP.NET webmethods Encosia

Zo Has
  • 12,599
  • 22
  • 87
  • 149
0

You need to register your web service with ScriptManager and then call it from the client side. Take a look on this tutorial:

Client-Side Web Service Calls with AJAX Extensions

Also you can use web service with jQuery but in this case you need to switch to JSON: Calling ASMX from jQuery

Community
  • 1
  • 1
Samich
  • 29,157
  • 6
  • 68
  • 77