Friday, February 12, 2010

Consuming webservice using JQuery Asp.Net application

Download:Here

 The Web Service methods that I will use revolve around client. Having set up a web site in Visual Studio 2008, I have added a new item of type "Web Service" to the project, calling it Common.asmx. The code-behind - Common.cs - is automatically generated within the App_Code folder . The full code for that class file is as follows:




 ClientDataContext db = new ClientDataContext();

    public Common()
    {
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public Client InsertClient(string BizName)
    {
        Client objClient = new Client();
        objClient.BizName = BizName;
        InsertData(objClient);
        return objClient;
    }

    public void InsertData(Client objClient)
    {
        db.Clients.InsertOnSubmit(objClient);
        db.SubmitChanges();
    }
I have used LINQ for database operation Insert client and return Id. 

Create dbml file named with Client.



Include JQuery library:








Return result as Json format following attribute will be responsible
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

and important do not forget to add attribute for webservice class
[System.Web.Script.Services.ScriptService]

Html code is


       
       
   
   
    
   

Now javascript for achieve ajax with Jquery libarary:



   
Description
adding refrence of JQuery library


Then a click event is registered with the button which will invoke the InsertClient () function

Document.Ready() It makes sure code is executed only when a page is fully loaded. We often place code blocks inside this Document.Ready() event.

$(document).ready(function() {
            $('#Save').click(InsertClient);
        }
    );



After that is the InsertClient() function that is fired when the button is clicked. It makes use of the $.ajax(options) function within jQuery, and accepts an object with a number of optional properties. type specifies the HTTP method, which in this case is POST. url specifies the URL of the Web Service, together with the web method that is being called. This is followed by the parameters, which are applied to the data property. In this case, BizName parameters are being passed, as we are calling the method that retrieves the entire collection of Client. The contentType and dataType MUST be specified. Following this are two further functions: success defines what should be done if the call is successful and get Id of client with msg['d']['Id'], and error handles exceptions that are returned.

You can see that an object with a property - d - is returned and we displaying Id of client.
    $('#status').html('Id: '+msg['d']['Id']);


Summary

With the marching forward of AJAX to the forefront of building rich interactive websites, jQuery as the lightweight yet powerful JavaScript library also gains ever more prominence.  Reflecting on the trend of harnessing the best of the web, this article paints in broad strokes some of the characteristics of jQuery and how we can integrate jQuery into ASP .NET.












3 comments:

Anonymous said...

Very True.. Article is Ok. not helped much

Chris Bond said...

Your rapidshare link to the source seems down can you reupload thx

Anonymous said...

aclarar el acne durante la noche http://archive.org/details/gbascompchipe tratamiento de acne de piel clara [url=http://archive.org/details/luslauveni] control de la natalidad acne[/url] lo que funciona mejor para el acne acne de Loestrin 24 fe ?Como reparar las cicatrices del acne
mamoleptino321
hoja y corredor tx coleccion acne 1 2 3 http://www.everyoneweb.es/acne1982a295 acne enquistado detras [url=http://surveys.questionpro.com/a/TakeSurvey?id=3418796] Isolaz laser de acne[/url] acne gratis terminator 10 revision Cupones gratis de acne imprimible tratamiento del acne adolescente

http://www.blogger.com/comment.g?blogID=27474840&postID=5515926617758989612&page=1&token=1361473642461 http://datescam.co.uk/bulletinboard/viewtopic.php?f=2&t=348784 http://holidaytrends.de/haben-sie-lust-fremde-kulturen-kennenzulernen/#comment-78635 http://curtziegler.com/sitedemo/mono/?portfolio=attachment-slider#comment-11558 http://her1sey.forumup.com/viewtopic.php?p=326&mforum=her1sey#326

acne jabon negro http://archive.org/details/florhydlicont acne no mas estafa [url=http://archive.org/details/dispfatdenar] eliminacion de cicatriz de acne natural[/url] acne manchas rojas mejor limpiador de piel para el acne sistema de la piel sensible del acne