My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
PluginUsage_v02  

Introduction

Here is a very short example of the usage of the jquery.dotNet-0.2.js plugin version. The new jquery.dotNet-0.3.js version CHANGES HIS FUNCTION SIGNATURE, please review the file comments to find out the new signature.

JS

<script language="javascript" type="text/javascript">
    // I use no conflict because the AJAX ASP.NET Framework Client API
    // overrides the $ function.
    jQuery.noConflict();
	// Execute all when the document is ready.
	jQuery(document).ready(function($) {

	    // private function for debugging
	    function debug(log) {
	        if (window.console && window.console.log)
	            window.console.log(log);
	    };

	    /*
	    * Simpliest call
	    */
	    $.callDotNet("Service.asmx/GetExample", function(result) {
	        debug(result);
	    });

	    /*
	    * Here we can define a global error handler to the plugin, and data
	    */
	    // WebService URL
	    var wsUrl = "Service.asmx/GetExampleData";
	    // Data
	    var data = "{pageNumber: 1, pageSize: 20, sortColumn: \"FirstName\", sortDirection: \"ASC\"}";
	    // Global Error Handler for AJAX requests
	    $.callDotNet.onError = function(error) {
	        alert("Run away!!!");
	    }
	    // The call back method.
	    function onSuccess(result) {
	        debug(result);
	    }
	    // And fanally the call to the web service
	    $.callDotNet(wsUrl, data, onSuccess);
	});
</script>

Web Service

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 Service : System.Web.Services.WebService
{

    [WebMethod]
    public string GetExampleData(int pageNumber, int pageSize, string sortColumn, string sortDirection)
    {
        List<Example> list = new List<Example>();
        for(int i = 0; i < 30; i++)
        {
            list.Add(new Example());
        }
        return JavaScriptConvert.SerializeObject(list, new IsoDateTimeConverter());
    }

    [WebMethod]
    public string GetExample()
    {
        return JavaScriptConvert.SerializeObject(new Example());
    }

    
    [JsonObject(MemberSerialization.OptIn)]
    public class Example
    {
        [JsonProperty]
        public string FirstName;
        [JsonProperty]
        public string LastName;
        [JsonProperty]
        public string Address;
        [JsonProperty]
        public DateTime Created = DateTime.Now;

        public Example()
        {
            FirstName = new String('-', 10);
            LastName = new String('*', 10);
            Address = new String(':', 10);
        }
    }
}
Comment by victor.k...@gmail.com, Jul 14, 2009

SDsdSDsfadfadsfadf


Sign in to add a comment
Powered by Google Project Hosting