Click here to Skip to main content
11,583,859 members (46,562 online)
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 MIT 682.6K 2.4K 251
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.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Ajax;

namespace AJAXTest
{
	/// <summary>
	/// Summary description for BubisChat.
	/// </summary>
	public class AjaxBubisChat : Ajax.AjaxPage	// Not required, read the remarks on AjaxPage
	{
		protected System.Web.UI.WebControls.TextBox txtName;
		protected System.Web.UI.WebControls.TextBox txtInput;
		protected System.Web.UI.WebControls.TextBox txtMsg;
		protected System.Web.UI.WebControls.ListBox lstNames;
		protected System.Web.UI.WebControls.Button btnChangeName;
		protected System.Web.UI.WebControls.Button btnClear;
		protected System.Web.UI.WebControls.Button btnSend;
		protected Ajax.Controls.AjaxPanel ajaxPanel;

		private BubisChatData chatData;
		private string currentName
		{
			get { return (string) chatData.names[Session.SessionID]; }
		}
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			//--- Additions for converting to an AJAX page ---
			// For automatic CallBack every 3 seconds.
			CallBackHelper.SetCallBackTimerInterval(3000);
			//-----------------------------------------------

			chatData = (BubisChatData) Cache["ajaxbubis_chat_data"];
			if (chatData == null)
			{
				chatData = new BubisChatData();
				Cache.Add("ajaxbubis_chat_data", chatData, null, DateTime.Now.AddDays(1),
					System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);
			}

			txtMsg.Text = chatData.msgText.ToString();

			string name = currentName;
			if (name == null)
			{
				name = txtName.Text;
				int i = 1;
				while (!AddName(name + i))
					i++;

				name += i;
				AddName(name);
			}

			txtName.Text = name;
			ShowNames();

			PutSessionIDInCache();
		}

		private void PutSessionIDInCache()
		{
			string key = "bubischat_sessionid_" + Session.SessionID;
			Context.Cache.Add(key, Session.SessionID, null, DateTime.Now.AddMinutes(Context.Session.Timeout),
				System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, new System.Web.Caching.CacheItemRemovedCallback(chatData.RemovedCallback));
		}

		private bool AddName(string name)
		{
			name = name.Trim();
			if (name == "" || chatData.names.ContainsValue(name))
				return false;

			chatData.names.Add (Session.SessionID, name);

			return true;
		}

		private void ChangeName(string newname)
		{
			chatData.names.Remove (Session.SessionID);
			AddName (newname.Trim());
		}

		private void ShowNames()
		{
			lstNames.Items.Clear();
			SortedList list = new SortedList();
			foreach (string name in chatData.names.Values)
				list.Add (name, null);

			foreach (string name in list.Keys)
				lstNames.Items.Add (name);
		}

		//--- Additions for converting to an AJAX page ---
		// It's called every time a CallBack is invoked by the client,
		// because the user clicked on a button or by the CallBackTimer
		protected override void OnCallBack(EventArgs e)
		{
			base.OnCallBack (e);

			// Refreshes the sessionID in the cache
			PutSessionIDInCache();

			txtMsg.Text = chatData.msgText.ToString();
			ShowNames();
		}
		//---------------------------------------------



		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
			this.btnChangeName.Click += new System.EventHandler(this.btnChangeName_Click);
			this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnSend_Click(object sender, System.EventArgs e)
		{
			if (txtInput.Text.Trim() != "")
			{
				if (chatData.msgText.Length > 10000)
					chatData.msgText.Remove (0, 3000);

				chatData.msgText.Append (currentName + ": " + txtInput.Text + "\r\n\r\n");
				txtMsg.Text = chatData.msgText.ToString();

				txtInput.Text = "";
			}
		}

		private void btnChangeName_Click(object sender, System.EventArgs e)
		{
            string name = txtName.Text.Trim();
			if (name == "")
			{
				// Using alert boxes to norify the user
				CallBackHelper.WriteAlert ("Type a name in the box.");
				return;
			}

			if (chatData.names.ContainsValue(name))
			{
				// Using alert boxes to norify the user
				CallBackHelper.WriteAlert ("This name already exists in the list.");
				return;
			}

			chatData.msgText.Append (String.Format("------ {0} changed his name to '{1}' -------\r\n\r\n", currentName, name));
			txtMsg.Text = chatData.msgText.ToString();

			ChangeName(name);
			ShowNames();
		}

		private void btnClear_Click(object sender, System.EventArgs e)
		{
			chatData.msgText.Length = 0;
			txtMsg.Text = "";
		}

		public class BubisChatData
		{
			public System.Text.StringBuilder msgText = new System.Text.StringBuilder();
			public Hashtable names = new Hashtable();

			public void RemovedCallback(String k, Object sessionID, System.Web.Caching.CacheItemRemovedReason r)
			{
				names.Remove (sessionID);
			}
		}
	}
}

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

Share

About the Author

Argiris Kirtzidis
Web Developer
Greece Greece
No Biography provided

You may also be interested in...

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