Passing JavaScript Objects to the CLR

by Vishal 16. December 2009 08:19

Passing JavaScript Objects to the CLR

Asp.Net 3.5 makes it easy to pass JavaScript Objects to the CLR for AJAX calls. Here it is...

For this example, I created an ASP.Net Web Service Application which gave me a simple web service to start off with called Service1.asmx. The sample service has a single sample web method called HelloWorld() which just returns the text “Hello World”. For this example, since we want to pass something to the service method, I modify the service method a little bit so it looks like below:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
namespace JSAjaxTest 
{ 
    /// <summary> 
    /// Summary description for Service1 
    /// </summary> 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService] 
    public class Service1 : System.Web.Services.WebService 
    { 
        [WebMethod] 
        public string HelloWorld(string text) 
        { 
            return text; 
        } 
    } 
}

 Basically I just modified the method to accept a string and return that same string back.

Now add an application page to the project, default.aspx, which will contain the javascript that makes the AJAX call. The default.aspx page contains a script manager, a html button and a piece of javascript that gets called when the button is clicked to make the AJAX call. This is what the page looks like:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="JSAjaxTest._default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title></title>

    <script type="text/javascript">

        function CallService() {

            JSAjaxTest.Service1.HelloWorld("Hello World", function(response) { alert(response); });

        }

    </script>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        &nbsp;<asp:ScriptManager ID="ScriptManager1" runat="server">

            <Services>

                <asp:ServiceReference Path="/Service1.asmx" />

            </Services>

        </asp:ScriptManager>

        <input type="submit" value="hitme" onclick="javascript:CallService();" /></div>

    </form>

</body>

</html>

 

Looking at the javascript on the page, the call is really made here:

JSAjaxTest.Service1.HelloWorld("Hello World", function(response) { alert(response); });

The call to the webservice through javascript uses the script manager to make the call, pass in the javascript argument and specifies the inline call back function which gets called when the AJAX call is complete. The inline function simply has an alert statement that displays the response.

When you run the project, clicking on the button will pass the text, “Hello World” to the “HelloWorld()” method of the web service via AJAX. There is no postback.

Tags: ,

.NET | C#

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading


Repliques Montres haute qualite vous aider a economiser beaucoup tout en appreciant la valeur du nom de marque. Vous voulez avoir cette montre luxe haut de gamme suisses sur votre main, mais replica uhren l'achat d'une voiture semble une meilleure option. Oui, c'est la realite. Ces montres symbole de statut social co?tent quelque chose replicas de relojes qui est au-dela pour permettre pour une personne normale. Mais ne desesperez hublot replique montres pas si vous ne pouvez pas vous permettre d'acheter ces montres de luxe. Vous avez une belle occasion d'acheter des montres de prestige nom Replique de la marque. Certaines personnes aiment l'aspect d'un veritable montres au poignet, ou replique rolex montres voulez juste nombreuses montres de marque pour correspondre a leur habillement, mais evitez patek philippe replica orologi de depenser des milliers de dollars que une vraie montre mai cots, ils se tournent vers les replique montre montres bonne replique. Si vous ne voulez pas acheter des montres de veritables montres de haute qualite puis de replicas sont votre choix ideal.