Click here to Skip to main content
13,253,863 members (66,289 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » Howto » Downloads


123 bookmarked
Posted 2 Aug 2004

Implement Script Callback Framework in ASP.NET 1.x

, 2 Aug 2004
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>
	ToolboxData("<{0}:AgeCalculator runat=server></{0}:AgeCalculator>")]
	public class AgeCalculator : Control, IClientCallbackEventHandler, IPostBackEventHandler, IPostBackDataHandler
		#region OnInit

		protected override void OnInit(EventArgs e)
			if (Page != null)
				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);
		#region OnLoad
		protected override void OnLoad(EventArgs e)
			Label l = new Label();
			l.Text = "Please enter your year of birth : ";
			TextBox txtBox = new TextBox();
			txtBox.ID = "txtAge";
			txtBox.Width = 50;
			HtmlInputButton btn = new HtmlInputButton();
			btn.ID = "CalculateAgeButton";
			btn.Value = "Calculate Age";
			btn.Attributes["onclick"] = PageTemplate.GetAsyncCallbackEventReference
					String.Format("document.getElementById('{0}').value", txtBox.UniqueID),
					String.Format("document.getElementById('{0}')", txtBox.UniqueID),
			base.OnLoad (e);
		#region IClientCallbackEventHandler Members

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

		#region IPostBackEventHandler Members

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


		#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;


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.


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


About the Author

Elvin Cheng
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.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 3 Aug 2004
Article Copyright 2004 by Elvin Cheng
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid