Click here to Skip to main content
Click here to Skip to main content

Javascript DIRECTLY Calls ASP.NET C# Method. Introduction to Pixysoft.Ajax

, 16 Nov 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
By using the pixysoft.ajax webcontrols, JavaScript can directly call ASP.NET C# server method.

Introduction

This technique is based on .NET Framework 2.0. It provides a webControl to help JavaScript to DIRECTLY call ASP.NET server based methods.

The source code is totally open source, and free to use. But please keep the copyright information in the code. Thank you!

Background

  1. Reflection is required.
  2. ASP.NET Callback technique (ICallbackEventHandler) is required.

Using the Code

I would like to show you how wonderful the idea is. First, we need an aspx page, call Default.aspx, like the following one:

<%@ Page Language="C#" AutoEventWireup="true" 
	CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="Pixysoft.Ajax" 
	Namespace="Pixysoft.Framework.Noebe.Jsons.WebControls"
    TagPrefix="cc1" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title>pixysoft.ajax</title>

    <script type="text/javascript">
    var code=1;
    function ServerMethodCallback(value)
    {
    code = value;
    Message1.innerText = value;
    }    
    </script>

</head>
<body>
    <form id="form1"  runat="server">
            <cc1:PixysoftAjaxBase ID="PixysoftAjaxBase1"  runat="server" />
            result: <span id="Message1">0</span>
            <input type="button" value="Let's callback"  
		önclick="ServerMethod(code)" id="Button1" />
    </form>
</body>
</html>

Then we need the server side code(CodeBehind), as follows:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public int ServerMethod(int value)
    {
        return value * 2;
    }
}

The code above means:

  1. I add an HTML button in the aspx page, and add a function named: ServerMethod to the click event.
  2. On server side, I create a method named: ServerMethod, the same in aspx page. The method would return an int value.
  3. I add a JavaScript function named: ServerMethodCallback(value) on aspx page. Then after server site method returns, this function would be called, and get the server side return as parameter 'value'.

Points of Interest

The magic above comes from the web control 'PixysoftAjaxBase'. It is inherited from interface 'ICallbackEventHandler'. And of course, some other tricks are needed (such as reflection... em... you know).

Well... because it is my first post on CodeProject, and I am not very familiar with the posting system... please see the source code for the rest of the answer. I write it in a very simple way.

History

This technique is one part of my JSON framework, named pixysoft.framework.noebe.jsons. The framework provides the JSON interface for other website to query the database (like providing the API...).

By using this technique, we can easily integrate the ASP.NET with other JS library like extjs / jquery.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

reborn_zhang
Engineer Pixysoft.net
China China
Working on database for 5 years, and develop many framework.

Skilled on persistency layer/ ORM / database consistency ..

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberyasirabbas01322-Dec-11 2:48 
GeneralMy vote of 2 PinmemberPuchko Vasili16-Nov-09 23:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150331.1 | Last Updated 16 Nov 2009
Article Copyright 2009 by reborn_zhang
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid