Click here to Skip to main content
15,884,176 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 966.5K   2.7K   251  
How to apply AJAX technologies to your web pages without replacing ASP.NET controls and/or writing JavaScript code.
using System;
using System.Web;

namespace Ajax
{
	/// <summary>
	/// Includes the CallBack event, Context, Request and Response, for convenience.
	/// </summary>
	/// <remarks>
	/// Inhering from AjaxPage is not required to apply AJAX on your pages. You can
	/// implement the ICallBackEventHandler on your page to handle the CallBack event, and
	/// use the HttpContext.Current, CallBackHelper.Request and CallBackHelper.Response
	/// properties.
	/// 
	/// You cannot use the Context, Request and Response of the System.Web.UI.Page
	/// because, during a CallBack, the Context of the page is invalid. If you don't want
	/// to replace the Page.Request and Page.Response from your code for
	/// CallBackHelper.Request and CallBackHelper.Response, and your page is inherited
	/// from System.Web.UI.Page, you can use AjaxPage for convenience.
	/// </remarks>
	public class AjaxPage : System.Web.UI.Page, ICallBackEventHandler
	{
		/// <summary>
		/// Raised by AjaxHttpModule when a CallBack occurs
		/// </summary>
		public event EventHandler CallBack;

		/// <summary>
		/// The base Page Request property doesn't work during a CallBack. Use this instead.
		/// </summary>
		public new HttpRequest Request
		{
			get
			{
				return Context.Request;
			}
		}

		/// <summary>
		/// The base Page Response property doesn't work during a CallBack. Use this instead.
		/// </summary>
		public new HttpResponse Response
		{
			get
			{
				return Context.Response;
			}
		}

		/// <summary>
		/// Raises the CallBack event.
		/// </summary>
		/// <remarks>
		/// Called by AjaxHttpModule.
		/// </remarks>
		public void RaiseCallBackEvent()
		{
			OnCallBack (EventArgs.Empty);
		}

		protected override HttpContext Context
		{
			get
			{
				return HttpContext.Current;
			}
		}

		protected virtual void OnCallBack(EventArgs e)
		{
			if (CallBack != null)
				CallBack(this, e);
		}
	}
}

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