Click here to Skip to main content
6,628,613 members and growing! (14,795 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Utilities     Advanced License: The Code Project Open License (CPOL)

Javascript DIRECTLY call ASP.NET c# method. Introduction to Pixysoft.Ajax

By reborn_zhang

By using the pixysoft.ajax webcontrols, javascript can directly call asp.net c# server method.
C#, Windows, ASP.NET, Dev
Posted:16 Nov 2009
Views:1,490
Bookmarked:13 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
2 votes for this article.
Popularity: 0.75 Rating: 2.50 out of 5

1
1 vote, 50.0%
2
1 vote, 50.0%
3

4

5

Introduction

This technique is based on .net framework 2.0. It provide a webControl to help Javascript 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 a 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 follow.

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 a 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 a int value.

3. I add a javasctipt function named: ServerMethodCallback(value) on aspx page. Then after server site method return, 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 inhereted 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 code project, and I am not very familiar with the posting system.... So Please see the source code for the rest answer. I write it very simple.

History

This technique is one part of my json framework, named pixysoft.framework.noebe.jsons. The framework provide the json interface for other website to query the database. (Like provide 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)

About the Author

reborn_zhang


Member
Working on database for 5 years, and develop many framework.

1. pixysoft.framework.noebe: provide the transparent and consistent visit to different database (oracle/sqlserver/mysql/access/sqlite)

2. pixysoft.framework.noebe.orm: provide the orm based on noebe.

3. pixysoft.framework.noebe.consistency: synchronization between different databases.

4. pixysoft.framwork.noebe.comets: provide the comet visit to database.
Occupation: Engineer
Company: Pixysoft.net
Location: China China

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralMy vote of 2 PinmemberPuchko Vasili23:59 16 Nov '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 16 Nov 2009
Editor:
Copyright 2009 by reborn_zhang
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project