Click here to Skip to main content
15,888,579 members
Articles / Web Development / HTML

Magic AJAX: Applying AJAX to your existing Web Pages

Rate me:
Please Sign up or sign in to vote.
4.82/5 (72 votes)
28 May 2007MIT12 min read 971.6K   2.7K   251  
How to apply AJAX technologies to your web pages without replacing ASP.NET controls and/or writing JavaScript code.
#region LGPL License
/*
MagicAjax.NET Framework
Copyright (C) 2005  MagicAjax Project Team

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#endregion

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace MagicAjax.UI
{
	/// <summary>
	///	The base control for controls to get notified for AjaxCall events from the client.
	/// </summary>
	public abstract class AjaxControl : System.Web.UI.WebControls.WebControl, IAjaxCallEventHandler, IPreWriteScriptEventHandler
	{
		private MagicAjaxContext _magicAjaxContext;
		private bool _isAjaxCall;
		private bool _isPageNoStoreMode;
		private bool _isInAjaxScope;

		/// <summary>
		/// Raised by the MagicAjaxModule and at Load event during an AjaxCall.
		/// </summary>
		public event EventHandler AjaxCallStart;

		/// <summary>
		/// Raised by the MagicAjaxModule and at PreRender event during an AjaxCall.
		/// </summary>
		public event EventHandler PreWriteScript;

		/// <summary>
		/// Raised by the MagicAjaxModule and at Unload event during an AjaxCall.
		/// </summary>
		public event EventHandler AjaxCallEnd;

		/// <summary>
		/// Implements the IAjaxCallEventHandler interface. It is called by the MagicAjaxModule.
		/// </summary>
		public void RaiseAjaxCallStartEvent()
		{
			SetAjaxIntrinsics();
			OnAjaxCallStart(EventArgs.Empty);
		}

		/// <summary>
		/// Implements the IPreWriteScriptEventHandler interface. It is called by the MagicAjaxModule.
		/// </summary>
		public void RaisePreWriteScriptEvent()
		{
			OnPreWriteScript (EventArgs.Empty);
		}

		/// <summary>
		/// Implements the IAjaxCallEventHandler interface. It is called by the MagicAjaxModule.
		/// </summary>
		public void RaiseAjaxCallEndEvent()
		{
			OnAjaxCallEnd(EventArgs.Empty);
		}

		[Browsable(false),DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public bool IsPageNoStoreMode
		{
			get { return _isPageNoStoreMode; }
		}

		/// <summary>
		/// Determines if the control is being processed during an AjaxCall.
		/// </summary>
		[Browsable(false),DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public bool IsAjaxCall
		{
			get { return _isAjaxCall; } 
		}

		[Browsable(false),DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public bool IsInAjaxScope
		{
			get { return _isInAjaxScope; }
		}

		public AjaxControl()
		{
		}
		public AjaxControl(HtmlTextWriterTag tag) : base(tag)
		{
		}

		protected MagicAjaxContext MagicAjaxContext
		{
			get { return _magicAjaxContext; }
		}

		protected override void OnLoad(EventArgs e)
		{
			SetAjaxIntrinsics();

			base.OnLoad (e);
			if ( IsAjaxCall )
				OnAjaxCallStart (e);
		}

		protected override void OnPreRender(EventArgs e)
		{
			// Register 'AjaxCallObject.js' script
			MagicAjaxModule.EnableAjaxOnPage(this.Page);

#if NET_2_0
			// add WebPartManager script (AddWebPartClientFunctions will check if this page is a WebPartPage)
			MagicAjaxModule.AddWebPartClientFunctions(this.Page);
#endif

			base.OnPreRender (e);
			if ( IsAjaxCall )
				OnPreWriteScript (e);
		}

		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			base.AddAttributesToRender (writer);
			AddAjaxAttributesToRender (writer);
		}

		protected override void OnUnload(EventArgs e)
		{
			base.OnUnload (e);
			if ( IsAjaxCall )
				OnAjaxCallEnd (e);
		}

		protected virtual void OnAjaxCallStart(EventArgs e)
		{
			if (AjaxCallStart != null)
				AjaxCallStart(this, e);
		}
		
		protected virtual void OnPreWriteScript(EventArgs e)
		{
			if (PreWriteScript != null)
				PreWriteScript(this, e);
		}

		protected virtual void OnAjaxCallEnd(EventArgs e)
		{
			_magicAjaxContext = null;

			if (AjaxCallEnd != null)
				AjaxCallEnd(this, e);
		}

		protected virtual void SetAjaxIntrinsics()
		{
			_magicAjaxContext = MagicAjaxContext.Current;
			_isAjaxCall = ( HttpContext.Current != null && this.Page != null && MagicAjaxContext.Current.IsAjaxCallForPage(this.Page) );
			_isPageNoStoreMode = ( HttpContext.Current != null && MagicAjaxContext.Current.IsPageNoStoreMode );

			string ajaxScopeID = Context.Request.Form["__AJAXSCOPE"];
			_isInAjaxScope = ( ajaxScopeID == null || IsInAjaxScopeRecursive(this, ajaxScopeID) );
		}

		/// <summary>
		/// It is empty for the AjaxControl class. It is meant to be overriden
		/// by subclasses so that they render their MagicAjax attributes.
		/// </summary>
		protected virtual void AddAjaxAttributesToRender (HtmlTextWriter writer)
		{
		}

		private bool IsInAjaxScopeRecursive (Control control, string ajaxScopeID)
		{
			if (control.ClientID == ajaxScopeID)
				return true;

			if (control.Parent == null || control.Parent == control.Page)
				return false;

			return IsInAjaxScopeRecursive(control.Parent, ajaxScopeID);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Web Developer
Greece Greece
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions