Click here to Skip to main content
15,891,136 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 478.3K   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.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;

namespace ScriptCallbackFramework
{
	/// <summary>
	/// Implement the in IClientCallbackEventHandler Page Control
	/// </summary>
	public class index : PageTemplate, IClientCallbackEventHandler
	{
		#region Member Vars
		protected System.Web.UI.HtmlControls.HtmlInputButton IncreaseButton;
		protected System.Web.UI.HtmlControls.HtmlInputText txtValue;
		protected ScriptCallbackFramework.AgeCalculator AgeCalculator1;
		protected System.Web.UI.HtmlControls.HtmlForm Form1;
		#endregion
		
		#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.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
		
		#region Page_Load
		private void Page_Load(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				//	Callback synchronously
				IncreaseButton.Attributes["onclick"] = GetSyncCallbackEventReference
					(
						Form1,
						String.Format("document.getElementById('{0}').value", txtValue.UniqueID),
						"IncreaseValueHandler",
						String.Format("document.getElementById('{0}')", txtValue.UniqueID),
						null
					);
			}
		}
		#endregion

		#region IClientCallbackEventHandler Members

		public string RaiseClientCallbackEvent(string eventArgument)
		{
			if (eventArgument == null || eventArgument == String.Empty)
				return "1";
			return Convert.ToString(Convert.ToInt32(eventArgument) + 1);
		}
		
		#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