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.

3 Answers 3

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
        }
      });

Sign up to request clarification or add additional context in comments.

3 Comments

on the third line of the WebMethod it gave an error: Cannot access a disposed object. Object name: 'DataContext accessed after Dispose.'.
i fixed the problem, turned out that the DataContext dispose of it self after the using statment finishes.
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: encosia.com/…
1

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.