Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » Howto » Downloads
 
Add your own
alternative version

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.
namespace ScriptCallbackFramework
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Text;
	using System.Threading;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	/// <summary>
	///	Implement IClientCallbackEventHandler interface in UserControl
	/// </summary>
	public class Employee : System.Web.UI.UserControl, IClientCallbackEventHandler
	{
		#region Member Vars
		protected System.Web.UI.WebControls.Label LabelTitle;
		protected System.Web.UI.HtmlControls.HtmlSelect employeeList;
		protected System.Web.UI.HtmlControls.HtmlGenericControl txtEmployeeDetail;
		protected System.Web.UI.WebControls.Label Label1;
		#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)
		{
			DataSet employeeData = new DataSet();
			employeeData.ReadXml(Server.MapPath("./Employees.xml"));

			if (!Page.IsPostBack)
			{	
				employeeList.Attributes["onchange"] = PageTemplate.GetAsyncCallbackEventReference
					(
						this,
						String.Format("document.getElementById('{0}').options[document.getElementById('{0}').selectedIndex].value", employeeList.UniqueID),
						"ShowEmployeeDetailHandler",
						String.Format("document.getElementById('{0}')", txtEmployeeDetail.ClientID),
						"ShowEmployeeErrorHandler"
					);

				BindToEmployeeList(employeeData);
			}
		}
		#endregion
		
		#region BindToEmployeeList

		/// <summary>
		/// Bind the Employee data to the Dropdown List
		/// </summary>
		/// <param name="employeeData"></param>
		private void BindToEmployeeList(DataSet employeeData)
		{
			employeeList.DataSource		= employeeData;
			employeeList.DataTextField	= "LastName";
			employeeList.DataValueField = "EmployeeID";
			employeeList.DataBind();
		}
		#endregion

		#region IClientCallbackEventHandler Members

		public string RaiseClientCallbackEvent(string eventArgument)
		{	
			DataSet employeeData = new DataSet();
			employeeData.ReadXml(Server.MapPath("./Employees.xml"));
			
			DataView dv = employeeData.Tables[0].DefaultView;
			dv.RowFilter = String.Format("EmployeeID = '{0}'", eventArgument); 
			if (dv.Count > 0)
			{
				StringBuilder sb = new StringBuilder();

				//	Employee Information
				sb.Append("<TABLE>");
				sb.Append(String.Format("<TR><TD bgcolor=\"Gainsboro\">Name</TD><TD>{0} {1}, {2}</TD></TR>",	dv[0]["TitleOfCourtesy"], dv[0]["LastName"], dv[0]["FirstName"]));
				sb.Append(String.Format("<TR><TD bgcolor=\"Gainsboro\">Title</TD><TD>{0}</TD></TR>",			dv[0]["Title"]));
				sb.Append(String.Format("<TR><TD bgcolor=\"Gainsboro\">Birth Date</TD><TD>{0}</TD></TR>",		dv[0]["BirthDate"].ToString()));
				sb.Append(String.Format("<TR><TD bgcolor=\"Gainsboro\">Hire Date</TD><TD>{0}</TD></TR>",		dv[0]["HireDate"].ToString()));
				sb.Append(String.Format("<TR><TD bgcolor=\"Gainsboro\">Address</TD><TD>{0}</TD></TR>",			dv[0]["Address"]));
				sb.Append(String.Format("<TR><TD bgcolor=\"Gainsboro\">City</TD><TD>{0}</TD></TR>",				dv[0]["City"]));
				sb.Append(String.Format("<TR><TD bgcolor=\"Gainsboro\">Region</TD><TD>{0}</TD></TR>",			dv[0]["Region"]));
				sb.Append(String.Format("<TR><TD bgcolor=\"Gainsboro\">Postal Code</TD><TD>{0}</TD></TR>",		dv[0]["PostalCode"]));
				sb.Append(String.Format("<TR><TD bgcolor=\"Gainsboro\">Country</TD><TD>{0}</TD></TR>",			dv[0]["Country"]));
				sb.Append(String.Format("<TR><TD bgcolor=\"Gainsboro\">Home Phone</TD><TD>{0}</TD></TR>",		dv[0]["HomePhone"]));
				sb.Append("</TABLE>");
				
				return sb.ToString();
			}
			return String.Empty;
		}

		#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

Share

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.
Follow on   Twitter

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