Click here to Skip to main content
6,822,613 members and growing! (20,676 online)
Email Password   helpLost your password?
Web Development » Ajax and Atlas » Libraries     Intermediate License: The Mozilla Public License 1.1 (MPL 1.1)

nxAjax - An Ajax Library

By Fernando Escolar

nxAjax is a .Net easy use Ajax Framework
C++/CLI, C# (C#1.0, C#2.0, C#3.0), VB (VB7.x, VB8.0, VB9.0), Javascript, CSS, HTML, .NET (.NET1.0, .NET1.1, .NET2.0, .NET3.0, .NET3.5), ASP.NET, Ajax, jQuery, Architect, Dev, Design
Revision:8 (See All)
Posted:6 Aug 2008
Updated:9 Feb 2010
Views:18,097
Bookmarked:72 times
Unedited contribution
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
11 votes for this article.
Popularity: 4.91 Rating: 4.71 out of 5

1

2
1 vote, 9.1%
3
2 votes, 18.2%
4
8 votes, 72.7%
5

Check new versions in codeplex: http://nxajax.codeplex.com/.


Introduction

An easy use asp.net jquery ajax library.

  • jQuery javascript core
  • Ajax WebControl PostbackModes: Async or Sync.
    • AjaxQueue (by Pat Nakajima) used by Sync PostBacks
    • Auto "loading message" in Sync PostBacks (in MainPage div -> id="loading")
    • Auto Loading Image in Async PostBacks (each control: LoadingImg or LoadingImgId attribute)
  • MainPage with container loads ContainedPage.
  • Multilanguage System
  • Template based Web Render
  • Low bandwidth waste: Only post the current control value
    • Only post all form content on ISubmit control is clicked
  • DOCTYPE XHTML 1.1 W3C compliant

Begining with nxAjax

Add a reference to "nxAjax.dll" assembly.

For a more convenient use of controls within the Toolbox, you can click with the second mouse button and select "Choose Items…." In the new form browse "nxAjax.dll" assembly and mark all its components.


So we will have our Toolbar as follows:

Quick Example

Create a new WebProject in Visual Studio 2008 and add reference to nxAjax binary.
Add to Web.Config file:

<system.web />
...
   <pages />
      <controls />
         ...
         <add assembly="nxAjax" namespace="Framework.Ajax.UI.Controls" tagprefix="ajax" />
      </controls />
   </pages />
   <httphandlers />
       ....
      <add validate="false" type="Framework.Ajax.HttpHandlers.ScriptResourceHandler, nxAjax" path="AjaxScriptResource.axd" verb="GET,HEAD" />
      <add validate="false" type="Framework.Ajax.HttpHandlers.FileUploadHandler, nxAjax" path="AjaxUploadFile.axd" verb="*" />
   </httphandlers />
...
</system.web />

Edit default.aspx like:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
    <title>My nxAjax Test</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <ajax:Container ID="container" runat="server"></ajax:Container>
    </div>
    </form>
</body>
</html>

Edit default.aspx.cs:

using System;
using System.Collections.Generic;
using Framework.Ajax.UI;
using Framework.Ajax.UI.Controls;

public partial class _Default : Framework.Ajax.UI.MainPage 
{
    protected void Page_Load(object sender, EventArgs e)
    {
         if (!IsPostBack)
            container.ContainedPage = "Contained1.aspx";
    }
}

Create a new WebForm named "Contained1.aspx":

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Contained1.aspx.cs" Inherits="Contained1" %>
<form id="form1" runat="server">
<div>
    <ajax:FileUpload ID="btnFile" runat="server" 
            UploadingMessage="Uploading..." 
            ExtensionNotAllowedMessage="Only image files!" 
            Text="Upload image" 
            LoadingImg="ajax-loader.gif" 
            OnServerComplete="btnFile_ServerComplete"></ajax:FileUpload>
    <ajax:CheckBox ID="chkTest" Checked="true" runat="server"></ajax:CheckBox>
    <ajax:Submit ID="btnSubmit" Value="Send" runat="server" 
            OnServerClick="btnSubmit_ServerClick"></ajax:Submit>
</div>
</form>

Edit Contained1.aspx.cs:

using System;
using System.Collections.Generic;
using Framework.Ajax.UI;
using Framework.Ajax.UI.Controls;

public partial class Contained1 : Framework.Ajax.UI.ContainedPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btnFile.AllowedExtensions.Add("jpg");
            btnFile.AllowedExtensions.Add("gif");
            btnFile.AllowedExtensions.Add("png");
            btnFile.AllowedExtensions.Add("bmp");

            btnSubmit.Disabled = true;
        }
    }
    protected void btnSubmit_ServerClick(AjaxControl sender, string value)
    {
        System.Web.HttpPostedFile file = btnFile.File;
        if (!chkTest.Checked)
            return;

        if (file == null)
            DocumentAlert("not file uploaded");
        else
        {
            DocumentAlert(file.FileName + " saved!");
        }
    }
    protected void btnFile_ServerComplete(AjaxControl sender, string value)
    {
        DocumentAlert(value + "  uploaded, you can click on 'send' button...");
        btnSubmit.Disabled = false;
    }
}

Launch and test it ;)

How it works

The client events stores a javascript that will be evaluated when they will be raised. Always on the client side. And the server events, are handled by the ASP.Net, running on the Web server and returned the amendments.

Container Control is a special control on management and operation. Its function is to be a container of ContainedPages. These are nxAjax Web pages (as MainPage) displayed as a piece of code and type of entity in a single form.

In this way we can navigate between different Web pages without having to return to reload all Ajax libraries, or re-create the Web format main container. It can resemble a UserControl, but that is loaded dynamically without being Instance within the main container.

MainPage

For now MainPage is the requirement nxAjax library has to work as it does.

MainPage inherits from AjaxPage (How inherits from Microsoft framework System.Web.UI.Page). And it overwrites much of its functionality.

Clears all javascripts from Microsoft, transforms the format of the form, add the javascript library (jQuery, internals, ...) and processes requests in a way that the library can understand.

The viewstate is stored in session varibles so it is no longer on the served page. MainPage removes doPostBack functions and __VIEWSTATE hidden input.

And it also provides a new template and multilanguage systems.

ContainedPage

A ContainedPage only be loaded into a container control (Framework.Ajax.UI.Controls.Container). And again in a MainPage.

It inherits from AjaxPage (How inherits from Microsoft framework System.Web.UI.Page). But unlike her older sister MainPage not load any scripts. So it is dependent.

ContainedPage is the tool we use to dynamically load content from Ajax on our website.

AjaxUserControl

AjaxUserControl works like System.Web.UI.UserControl. But if you want load dynamically content, it is better to use ContainedPage.

AjaxControl

AjaxControl server control is the base of the entire nxAjax library. Every control inherits from this.

To render an AjaxControl we use two methods: RenderHTML and RenderJS. The first writes the HTML code that we see on the Web. And the second renders the javascripts used for initialization and status changes.

To avoid overload, the HTML rendering uses an internal cache for each instance of the control. And only it is written once, when the page is loaded the first time.

Then rendering javascripts comes into play. It is launched the first time and then on each postback which change its status.

To force an status change or otherwise use the funcitons AjaxUpdate and AjaxNotUpdate.

Every AjaxControl postback has two methods, synchronous and asynchronous. The default is Sync.

In the synchronous call context is used an ajax queue. Here it is adding requests to be loaded sequentially. If you want during the server process, a loading screen is showed, add in MainPage a div object with id="loading".

Asynchronous calls are made for each of the controls at the time of requesting. PostBackMode property specifies it. And if you want to add a loading image while processing the request, you can set the properties: LoadingImg (Image source) or LoadingImgID (Image Control ID).

AjaxControl postback only posts the current control value. If you want to post all form content you should use ISubmit control (like Submit or LinkSubmit).

Finally, AjaxPage property gets the container page: ContainedPage or MainPage.

Why DOCTYPE XHTML1.1?

Using nxAjax Template System involves converting all the htmls code to xml files. So, in order to meet the standards, we have chosen the option to be W3C compliant in the XHTML 1.1 context.

Using Multi-Language System

Open the Quick Example project. Create a new file named: "english.language". You can add here all application strings

#
# This is a coment, you can write what you want here...
#
# 
# English Language File
#
# Information section:
[INFO]
name=English
id=ENG
#
#Dictionary section:
[DICTIONARY]
# Format:
# [MyKey]=[Literal]
Test.ErrorMessage01=not file uploaded
Test.SavedMessage=%s saved!
Test.UploadedMessage=%s uploaded, you can click on 'send' button...

Now go to Menu -> Website -> Add New File, and select "Global Application Class". Open the Global.asax file:
You can do this:

...
    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
        Application["Language"] = new Framework.Ajax.Language(Server.MapPath("english.language"));
    }
...

Or this:

...
    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started
        Session["Language"] = new Framework.Ajax.Language(Server.MapPath("english.language"));
    }
...

Edit Contained1.aspx.cs:

public partial class Contained1 : Framework.Ajax.UI.ContainedPage
{
...
    protected void btnSubmit_ServerClick(AjaxControl sender, string value)
    {
        System.Web.HttpPostedFile file = btnFile.File;
        if (!chkTest.Checked)
            return;

        if (file == null)
            DocumentAlert(lang["Test.ErrorMessage01"]);
        else
        {
            DocumentAlert(lang["Test.SavedMessage"].Replace("%s", file.FileName));
        }
    }
    protected void btnFile_ServerComplete(AjaxControl sender, string value)
    {
        DocumentAlert(lang["Test.UploadedMessage"].Replace("%s", value));
        btnSubmit.Disabled = false;
    }
}

Template Based Render

Template in MainPage

With the Quick Example project we are going to use nxAjax templates.
Open default.aspx and delete all html tags:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<form id="form1" runat="server"></form>
<ajax:Container ID="container" runat="server"></ajax:Container>

Asp.Net needs a form with the attribute runat="server" in order to works...
Create a new "main.template" empty text file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
    <title>My nxAjax Test</title>
</head>
<body>
    <div>
        <ajax:Container ID="container" runat="server"></ajax:Container>
    </div>
</body>
</html>

Replace the ajax control aspx code with a tag like "<$ControlID$>". And you must add 2 special tags in the MainPage: <$PreScript$> and <$PostScript$>. The first in the header section, and the second at the end of the document body:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
    <title>My nxAjax Test</title>
    <$PreScript$>
</head>
<body>
    <div>
        <$container$>
    </div>
    <$PostScript$>
</body>
</html>

Edit default.aspx.cs:

public partial class _Default : Framework.Ajax.UI.MainPage 
{
    protected void Page_Load(object sender, EventArgs e)
    {
         this.TemplateFile = "main.template";
         if (!IsPostBack)
            container.ContainedPage = "Contained1.aspx";
    }
}

Inside a MainPage template you can use 2 optional special tags if you need it: <$InitForm$> and <$EndForm$>, in order to submit one main form.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
    <title>My nxAjax Test</title>
    <$PreScript$>
</head>
<body>
    <$InitForm$>
    <div>
        <$container$>
    </div>
    <$EndForm$>
    <$PostScript$>
</body>
</html>

Template in ContainedPage

Open "Contained1.aspx" and delete all html tags (except server form):

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Contained1.aspx.cs" Inherits="Contained1" %>
<form id="form1" runat="server"></form>
<ajax:FileUpload ID="btnFile" runat="server" 
            UploadingMessage="Uploading..." 
            ExtensionNotAllowedMessage="Only image files!" 
            Text="Upload image" 
            LoadingImg="ajax-loader.gif" 
            OnServerComplete="btnFile_ServerComplete"></ajax:FileUpload>
<ajax:CheckBox ID="chkTest" Checked="true" runat="server"></ajax:CheckBox>
<ajax:Submit ID="btnSubmit" Value="Send" runat="server" 
            OnServerClick="btnSubmit_ServerClick"></ajax:Submit>

Create a new "container1.template" empty text file, with <$ControlID$> tags where you want show each control:

<div>
    <$btnFile$>
    <$chkTest$>
    <$btnSubmit$>
</div>

You can optionaly add <$PostBack$> tag if you want to put the postback controls in a specified postion into the document.

Add into Contained1.aspx.cs this line:

...
public partial class Contained1 : Framework.Ajax.UI.ContainedPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.TemplateFile = "container1.template";
        if (!IsPostBack)
        {
...

Inside ContainedPage template you do not need declare a HTML form. It is beacuse of a ContainedPage is a form, and the render will ever write the begin and end form tags.

Template with XML source

In oder hand you can create Xml aspx structures. It is not compatible with the previous way.
Open "container1.template" file and modify it to something like:

<div>
    <$File$>
    <$Submit$>
</div>

To fill this template we need modify "Contained1.aspx":

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Contained1.aspx.cs" Inherits="Contained1" %>
<form id="form1" runat="server"></form>
<file>
     <ajax:FileUpload ID="btnFile" runat="server" 
            UploadingMessage="Uploading..." 
            ExtensionNotAllowedMessage="Only image files!" 
            Text="Upload image" 
            LoadingImg="ajax-loader.gif" 
            OnServerComplete="btnFile_ServerComplete"></ajax:FileUpload>
</file>
<submit>
     <ajax:CheckBox ID="chkTest" Checked="true" runat="server"></ajax:CheckBox>
     <ajax:Submit ID="btnSubmit" Value="Send" runat="server" OnServerClick="btnSubmit_ServerClick"></ajax:Submit>
</submit>

Note you can nest 2 or more AjaxControls inside a Xml element.

If you run the project the result is like the previous example.

When the tmplate system processes an aspx document, It checks document begin with "

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Contained1.aspx.cs" Inherits="Contained1" %>
<?xml version="1.0" encoding="utf-8"?>
<page>
     <form id="form1" runat="server"></form>
     <file>
          <ajax:FileUpload ID="btnFile" runat="server" 
                 UploadingMessage="Uploading..." 
                 ExtensionNotAllowedMessage="Only image files!" 
                 Text="Upload image" 
                 LoadingImg="ajax-loader.gif" 
                 OnServerComplete="btnFile_ServerComplete"></ajax:FileUpload>
     </file>
     <submit>
          <ajax:CheckBox ID="chkTest" Checked="true" runat="server"></ajax:CheckBox>
          <ajax:Submit ID="btnSubmit" Value="Send" runat="server" OnServerClick="btnSubmit_ServerClick"></ajax:Submit>
     </submit>
</page>

And it works too.

Template Areas

Going into the modeling templates, we found a series of special commands to create template parts. These template parts are named "areas". Defining an area you must use 2 special tags: <!$Begin AreaName> and <!$End AreaName>.

<!$Begin divArea>
  <div>
    <$object$>
  </div>
<!$End divArea>
<$divs$>

Or also:

<!$Begin divArea>
  <div>
    <$file$>
    <$submit$>
  </div>
<!$End divArea>
<$divs$>

Inside the aspx xml code, you must define an area tag with the attributes:

  • Id: Area Name
  • Place: Template tag where the area will be rendered
  • Method: "Add" || "Allocate". Optional. By default "Add".
    • Add for more than one area in the same position.
    • Allocate for only one area in this position

To fill this new template we need modify "Contained1.aspx":

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Contained1.aspx.cs" Inherits="Contained1" %>
<form id="form1" runat="server"></form>
<area id="divArea" place="divs" method="allocate">
     <object>
          <ajax:FileUpload ID="btnFile" runat="server" 
                 UploadingMessage="Uploading..." 
                 ExtensionNotAllowedMessage="Only image files!" 
                 Text="Upload image" 
                 LoadingImg="ajax-loader.gif" 
                 OnServerComplete="btnFile_ServerComplete"></ajax:FileUpload>
          <ajax:CheckBox ID="chkTest" Checked="true" runat="server"></ajax:CheckBox>
          <ajax:Submit ID="btnSubmit" Value="Send" runat="server" OnServerClick="btnSubmit_ServerClick"></ajax:Submit>
     </object>
</area>

Or also:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Contained1.aspx.cs" Inherits="Contained1" %>
<form id="form1" runat="server"></form>
<area id="divArea" place="divs">
     <file>
          <ajax:FileUpload ID="btnFile" runat="server" 
                 UploadingMessage="Uploading..." 
                 ExtensionNotAllowedMessage="Only image files!" 
                 Text="Upload image" 
                 LoadingImg="ajax-loader.gif" 
                 OnServerComplete="btnFile_ServerComplete"></ajax:FileUpload>
     </file>
     <submit>
          <ajax:CheckBox ID="chkTest" Checked="true" runat="server"></ajax:CheckBox>
          <ajax:Submit ID="btnSubmit" Value="Send" runat="server" OnServerClick="btnSubmit_ServerClick"></ajax:Submit>
     </submit>
</area>

And if we want create 2 div blocks instead one (with the first example):

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Contained1.aspx.cs" Inherits="Contained1" %>
<form id="form1" runat="server"></form>
<area id="divArea" place="divs">
     <object>
          <ajax:FileUpload ID="btnFile" runat="server" 
                 UploadingMessage="Uploading..." 
                 ExtensionNotAllowedMessage="Only image files!" 
                 Text="Upload image" 
                 LoadingImg="ajax-loader.gif" 
                 OnServerComplete="btnFile_ServerComplete"></ajax:FileUpload>
     </object>
</area>
<area id="divArea" place="divs">
     <object>
          <ajax:CheckBox ID="chkTest" Checked="true" runat="server"></ajax:CheckBox>
          <ajax:Submit ID="btnSubmit" Value="Send" runat="server" OnServerClick="btnSubmit_ServerClick"></ajax:Submit>
     </object>
</area>

Template Includes

Another special command is "<!$Include TemplatePartFileName>". It will be replaced by the template file referenced. The template file name is the relative path from the current document to the document we want include.

Create a new empty text file named "divpart.template":

<!$Begin divArea>
  <div>
    <$object$>
  </div>
<!$End divArea>

And modify "container1.template" file:

<!$Include divpart.template>
<$divs$>

And it works like the others.

Template with language

The template based render system can auto replace strings using the language file. You have to write another special tag with the language key: <&LanguageKey&>

If you have create a language file you can try to add the page title to "english.language":

...
[DICTIONARY]
...
Title=My nxAjax Test

And replace in the "main.template" file, the page title by the language tag:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
--- <title><&Title&></title>
    <$PreScript$>
</head>
<body>
    <div>
        <$container$>
    </div>
    <$PostScript$>
</body>
</html>

Template in GridView

The nxAjax GridView control contains the attribute ContentTemplateFileName. It allows set a template file name and the grid will render based in this template file.

<ajax:GridView ID="gridImages" runat="server" ContentTemplateFileName="grid.template">
  <ajax:GridImageColumn DataColumn="img" />
  <ajax:GridLabelColumn DataColumn="name" />
</ajax:GridView>

The template file have to contain 3 areas: "header", "content", "footer".

  • Header: The first (no repeat) code you want render.
  • Content: Repetitive code, for each row it will render a content area by replace each <$Column{N}$>.
  • Footer: The last (no repeat) code you want render.

<!$Begin header>
<div class="imageListDiv">
<!$End header>

<!$Begin content>
   <div class="imageDiv">
      <$Column{0}$><br />
      <span><$Column{1}$></span>
   </div>
<!$End content>

<!$Begin footer>
</div>
<!$End footer>

More information

You can also read this in nxAjax codeplex page.

License

This article, along with any associated source code and files, is licensed under The Mozilla Public License 1.1 (MPL 1.1)

About the Author

Fernando Escolar


Member
I have been working since 2002, in some companies:

- Mutuavenir (Pamplona)
- Entel IT: Outsourcing (Barcelona)
A3Software
Telefónica
- Altran CIS: Outsourcing (Barcelona)
Byrom plc (currently)

+ MCP (Microsoft Certified Profesional)
Occupation: Software Developer (Senior)
Location: Spain Spain

Other popular Ajax and Atlas articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
GeneralVB usage problem with jquery updated library PinmemberMember 334412914:41 5 Jan '10  
GeneralRe: VB usage problem with jquery updated library PinmemberFernando Escolar23:33 7 Jan '10  
Generalnot W3C compliance.. PinmemberMember 34202131:48 5 Jan '10  
GeneralRe: not W3C compliance.. PinmemberFernando Escolar1:09 4 Feb '10  
GeneralFantastic PinmemberJonathan C Dickinson10:18 28 Dec '09  
GeneralRe: Fantastic PinmemberFernando Escolar23:20 7 Jan '10  
GeneralRe: Fantastic PinmemberJonathan C Dickinson21:16 10 Jan '10  
GeneralCalling a web service PinmemberWaleed Eissa5:01 13 Aug '08  
GeneralRe: Calling a web service [modified] PinmembernullEX_12:06 24 Aug '08  
GeneralPossibility to use this dll in wss 3.0 PinmemberVasssek19:10 12 Aug '08  
GeneralRe: Possibility to use this dll in wss 3.0 [modified] PinmembernullEX_12:11 24 Aug '08  

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

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

PermaLink | Privacy | Terms of Use
Last Updated: 9 Feb 2010
Editor:
Copyright 2008 by Fernando Escolar
Everything else Copyright © CodeProject, 1999-2010
Web19 | Advertise on the Code Project