Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » Ajax » General » Downloads
 
Add your own
alternative version

Magic AJAX: Applying AJAX to your existing Web Pages

, 28 May 2007
How to apply AJAX technologies to your web pages without replacing ASP.NET controls and/or writing JavaScript code.
magicajax-030-net11.zip
magicajax
Examples
ExampleSite (.NET 2.0 only)
Web.sitemap
webparts
images
first.GIF
last.GIF
next.gif
pow_by_aspnet2.0.gif
prev.GIF
examples
App_Data
App_Code
Docs
Core
script
bin
MagicAjax.dll
magicajax-030-net20.zip
MagicAjax.dll
Web.sitemap
first.GIF
last.GIF
next.gif
pow_by_aspnet2.0.gif
prev.GIF
magicajax-030-source.zip
Web.sitemap
CVS
Root
Repository
Entries.Old
Entries
Entries.Extra.Old
Entries.Extra
first.GIF
last.GIF
next.gif
pow_by_aspnet2.0.gif
prev.GIF
CVS
Root
Repository
Entries.Old
Entries
Entries.Extra.Old
Entries.Extra
CVS
Root
Repository
Entries.Old
Entries
Entries.Extra.Old
Entries.Extra
CVS
Root
Repository
Entries.Old
Entries
Entries.Extra.Old
Entries.Extra
CVS
Root
Repository
Entries.Old
Entries
Entries.Extra.Old
Entries.Extra
CVS
Root
Repository
Entries
Entries.Extra
Entries.Old
Entries.Extra.Old
CVS
Root
Repository
Entries.Old
Entries.Extra.Old
Entries
Entries.Extra
CVS
Root
Repository
Entries
Entries.Extra
Entries.Old
Entries.Extra.Old
MagicAjax.snk
CVS
Root
Repository
Entries.Old
Entries.Extra.Old
Entries
Entries.Extra
UI
Design
CVS
Root
Repository
Entries.Old
Entries.Extra.Old
Entries
Entries.Extra
Controls
ClientEventControls
CVS
Root
Repository
Entries.Old
Entries
Entries.Extra.Old
Entries.Extra
CVS
Root
Repository
Entries.Extra
Entries.Old
Entries
Entries.Extra.Old
CVS
Root
Repository
Entries.Old
Entries.Extra.Old
Entries
Entries.Extra
Interfaces
CVS
Root
Repository
Entries.Old
Entries.Extra.Old
Entries
Entries.Extra
Configuration
CVS
Root
Repository
Entries.Old
Entries.Extra.Old
Entries
Entries.Extra
CVS
Root
Repository
Entries
Entries.Extra
Entries.Old
Entries.Extra.Old
CVS
Root
Repository
Entries.Old
Entries.Extra.Old
Entries
Entries.Extra
magicajax.zip
AJAXTest
AJAXTest.csproj.webinfo
Global.asax
Ajax
Ajax.csproj.user
Controls
Design
script
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ComponentModel;

namespace MagicAjax.UI.Controls
{
	/// <summary>
	/// Captures a client event of a control.
	/// </summary>
	/// <remarks>
	///	The EventName property must be set to the client event (i.e. "focus","change", etc.)
	///	and the ControlID property must be set to the ID of the control whose event we want to
	///	capture. The AjaxCall that will be invoked is dependent at the ClientEventTrigger's
	///	placement, not the placement of ControlID's control. For example, if the ClientEventTrigger
	///	is inside an AjaxPanel and AjaxCall will be invoked even if the ControlID's control is
	///	not inside an AjaxPanel.
	///	
	///	The ClientEventTrigger must be inside the same NamingContainer (i.e. UserControl)
	///	as the ControlID's control.
	/// </remarks>
	[Designer("MagicAjax.UI.Design.ClientEventTriggerDesigner, MagicAjax"),
		ParseChildrenAttribute(true),
		PersistChildren(false),
		ToolboxData("<{0}:ClientEventTrigger runat=server></{0}:ClientEventTrigger>")]
	public class ClientEventTrigger : ClientEventControl, INonHtmlHolder
	{
		private string _controlID = String.Empty;
		private string _eventName = String.Empty;

		public event EventHandler Invoke;

		[Bindable(false),
			Category("Behaviour"), 
			DefaultValue("")]
		public string ControlID
		{
			get { return _controlID; }
			set { _controlID = value; }
		}

		[Bindable(false),
			Category("Behaviour"), 
			DefaultValue("")]
		public string EventName
		{
			get { return _eventName; }
			set { _eventName = value; }
		}

		protected override void HandleEvent(string eventName, string argument)
		{
			OnInvoke (EventArgs.Empty);
		}

		protected override void Render(HtmlTextWriter writer)
		{
			if ( Page != null )
			{
				string attachScriptFormat = @"<script type='text/javascript'>
				<!--
				elem=document.getElementById('{0}');
				fn=function(e) {{__doPostBack('{1}',e.type+';');}};
				if (elem.addEventListener)
					elem.addEventListener('{2}',fn,false);
				else
					elem.attachEvent('on{2}',fn);
				// -->
				</script>";

				if (ControlID == null || ControlID == String.Empty)
					throw new MagicAjaxException(String.Format("ControlID property for trigger {0} is not set.", ClientID));

				if (EventName == null || EventName == String.Empty)
					throw new MagicAjaxException(String.Format("EventName property for trigger {0} is not set.", ClientID));

				Control con = this.NamingContainer.FindControl(ControlID);
				if ( con == null )
					throw new MagicAjaxException(String.Format("The ControlID '{0}' does not exist or is not in the same NamingContainer as trigger {1}", ControlID, ClientID));

				if ( ! Page.IsStartupScriptRegistered("__TRIGGER" + this.ClientID) )
					Page.RegisterStartupScript ("__TRIGGER" + this.ClientID, String.Format(attachScriptFormat, con.ClientID, this.UniqueID, EventName));

				writer.Write("<span id=\"{0}\"></span>", ClientID);
			}
		}
		
		protected virtual void OnInvoke (EventArgs e)
		{
			if (Invoke != null)
				Invoke (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

About the Author

Argiris Kirtzidis
Web Developer
Greece Greece
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 28 May 2007
Article Copyright 2005 by Argiris Kirtzidis
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid