Click here to Skip to main content
15,893,814 members
Articles / Web Development / IIS

Implement Script Callback Framework in ASP.NET 1.x

Rate me:
Please Sign up or sign in to vote.
4.84/5 (56 votes)
2 Aug 20048 min read 484.7K   4.2K   122  
It allows calls to server events from client script code without causing the page to post back and refresh.
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;


namespace ScriptCallbackFramework
{
	/// <summary>
	/// Implemenet IClientCallbackEventHandler in the Server Control
	/// </summary>
	[DefaultProperty("Text"),
	ToolboxData("<{0}:AgeCalculator runat=server></{0}:AgeCalculator>")]
	public class AgeCalculator : Control, IClientCallbackEventHandler, IPostBackEventHandler, IPostBackDataHandler
	{	
		#region OnInit

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			if (Page != null)
			{
				Page.RegisterRequiresPostBack(this);
				
				string script = String.Empty;

				//	ShowAgeHandler JavaScript function
				script = "<script language=\"javascript\">function ShowAgeHandler(responseText, context) { alert(\"Your age is \" + responseText);  } </script>";
				Page.RegisterClientScriptBlock("ShowAgeHandler", script);
				
				//	ShowAgeErrorHandler JavaScript function
				script = "<script language=\"javascript\">function ShowAgeErrorHandler(responseText, context) { alert(\"Which year you were born?\"); if (context != null) context.value = \"\";  }</script>";
				Page.RegisterClientScriptBlock("ShowAgeErrorHandler", script);
			}		
		}
		
		#endregion
		
		#region OnLoad
		protected override void OnLoad(EventArgs e)
		{	
			Label l = new Label();
			l.Text = "Please enter your year of birth : ";
			Controls.Add(l);
			
			TextBox txtBox = new TextBox();
			txtBox.ID = "txtAge";
			txtBox.Width = 50;
			Controls.Add(txtBox);
			
			HtmlInputButton btn = new HtmlInputButton();
			btn.ID = "CalculateAgeButton";
			btn.Value = "Calculate Age";
			btn.Attributes["onclick"] = PageTemplate.GetAsyncCallbackEventReference
				(
					this,
					String.Format("document.getElementById('{0}').value", txtBox.UniqueID),
					"ShowAgeHandler",
					String.Format("document.getElementById('{0}')", txtBox.UniqueID),
					"ShowAgeErrorHandler"
				);
			Controls.Add(btn);
			
			base.OnLoad (e);
		}
		#endregion
		
		#region IClientCallbackEventHandler Members

		public string RaiseClientCallbackEvent(string eventArgument)
		{
			return Convert.ToString(DateTime.Now.Year - Convert.ToInt32(eventArgument));
		}
		
		#endregion

		#region IPostBackEventHandler Members

		public void RaisePostBackEvent(string eventArgument)
		{
			// TODO:  Add AgeCalculator.RaisePostBackEvent implementation
		}

		#endregion

		#region IPostBackDataHandler Members

		public void RaisePostDataChangedEvent()
		{
			// TODO:  Add AgeCalculator.RaisePostDataChangedEvent implementation
		}

		public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
		{
			// TODO:  Add AgeCalculator.LoadPostData implementation
			return false;
		}

		#endregion
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Singapore Singapore
Elvin Cheng is currently living in Woodlands, Singapore. He has been developing applications with the .NET Framework, using C# and ASP.NET since October 2002. Elvin specializes in building Real-time monitoring and tracking information system for Semi-conductor manufacturing industry. During his spare time, he enjoys reading books, watching movie and gym.

Comments and Discussions