Click here to Skip to main content
15,893,381 members
Articles / Web Development / HTML

C# and AJAX WhiteBoard

Rate me:
Please Sign up or sign in to vote.
4.71/5 (14 votes)
26 Oct 2009CPOL7 min read 99.9K   2.9K   95  
This is a web-based WhiteBoard. It uses C# and AJAX to communicate between the server and clients. Data sharing between different users is made possible using AJAX. Drawings can be shared in real time over multiple clients.
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 System.Text;

namespace board
{
	/// <summary>
	/// Summary description for HandleData.
	/// </summary>
	public class HandleData : System.Web.UI.Page
	{
		
		#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 and control events

		private void Page_Load(object sender, System.EventArgs e)
		{

			//To identify the reason of the call
			string strParam = Request.QueryString.Get(Constants.CONST_QUERY_PARAM);

			//Check the param
			if (strParam != null && strParam.Length > 0)
			{
				try
				{

					//Check why the page was called
					if (strParam.Equals(Constants.CONST_QUERY_PARAM_SETDATA)) 
					{
						setData();
					} 
					else 
					{
						getData();

					} //End of checking the param string
				} 
				catch 
				{
					//Write the error with the first word set to identify as error
					Response.Write(Constants.CONST_ERROR + Constants.CONST_INTERNAL_SEPERATOR + 
						Constants.CONST_ERROR_UNKNOWN);
				}
			} 
			else 
			{				
				//Write the error with the first word set to identify as error
				Response.Write(Constants.CONST_ERROR + Constants.CONST_INTERNAL_SEPERATOR + 
								Constants.CONST_ERROR_PARAM);

			}//End of checking if the param exists

		} //End of pageload

		#endregion

		#region Private Functions

		//Functino to get the data and set it to the page
		private void getData() 
		{

			string strReadFrom = Request.QueryString.Get(Constants.CONST_QUERY_READ_FROM);
			
			//Check the read value
			if (strReadFrom != null && strReadFrom.Length > 0) 
			{
				StringBuilder strText = new StringBuilder("");
				int iFrom = Int32.Parse(strReadFrom);
				//Get the array list from the application object
				ArrayList arrData = (ArrayList)Application[Constants.CONST_APP_DATA_ARRAY_NAME];

				//Check if ifrom is equal to the length of the arraylist
				//Means no new data to read
				if (iFrom >= arrData.Count) 
				{
					//Screen is already updated
					//Write the error with the first word set to identify as error
					Response.Write(Constants.CONST_ERROR + Constants.CONST_INTERNAL_SEPERATOR + 
						Constants.CONST_ERROR_ALREADY_UPDATED);
					
					return;

				} //End of checking the length

				//Add the first element
				strText.Append(arrData[iFrom]);

				//Do the process to create a response string 
				for (int i = iFrom + 1; i < arrData.Count; i++) 
				{
					strText.Append(Constants.CONST_LINE_SEPERATOR + arrData[i]);
				
				} //End of appending the data to the string builder
				
				//Write the string builder to the page
				Response.Write(strText.ToString());
			} 
			else 
			{
				//Write the error with the first word set to identify as error
				Response.Write(Constants.CONST_ERROR + Constants.CONST_INTERNAL_SEPERATOR + 
					Constants.CONST_ERROR_PARAM);

			}//End of checking the read value

		} //End of getData

		//Function to set the data on the server
		private void setData() 
		{
			//Get the array list from the application object
			ArrayList arrData = (ArrayList)Application[Constants.CONST_APP_DATA_ARRAY_NAME];

			//Get all the drawing values from the request querstring			
			string strAction = Request.QueryString.Get(Constants.CONST_QUERY_ACTION);
			string strColor = Request.QueryString.Get(Constants.CONST_QUERY_COLOR);
			string strStroke = Request.QueryString.Get(Constants.CONST_QUERY_STROKE);
			string strStartX = Request.QueryString.Get(Constants.CONST_QUERY_STARTX);
			string strStartY = Request.QueryString.Get(Constants.CONST_QUERY_STARTY);
			string strEndX = Request.QueryString.Get(Constants.CONST_QUERY_ENDX);
			string strEndY = Request.QueryString.Get(Constants.CONST_QUERY_ENDY);

			if (strAction != null && strAction.Length > 0)
			{ 
				if (strAction.Equals(Constants.CONST_ACTION_TOOL_CLEAR)) 
				{
					arrData.Add(strAction + Constants.CONST_INTERNAL_SEPERATOR);
				} 
				else 
				{
					//Check if all are present
					if (strColor != null && strStroke != null && 
						strStartX != null && strStartY != null && strEndX != null && strEndY != null &&
						strColor.Length > 0 && strStroke.Length > 0 &&
						strStartX.Length > 0 && strStartY.Length > 0 && strEndX.Length > 0 && strEndY.Length > 0) 
					{

						//Set the data in the application array
						arrData.Add(strAction + Constants.CONST_INTERNAL_SEPERATOR + 
							strColor + Constants.CONST_INTERNAL_SEPERATOR + 
							strStroke + Constants.CONST_INTERNAL_SEPERATOR + 
							strStartX + Constants.CONST_INTERNAL_SEPERATOR +
							strStartY + Constants.CONST_INTERNAL_SEPERATOR + 
							strEndX + Constants.CONST_INTERNAL_SEPERATOR + 
							strEndY);

						//Write a blank string as an identification that no error occured
						Response.Write("");

					} 
					else 
					{
						//Write the error with the first word set to identify as error
						Response.Write(Constants.CONST_ERROR + Constants.CONST_INTERNAL_SEPERATOR + 
							Constants.CONST_ERROR_PARAM);

					}//End of Checking if all querystrings are valid
				}

			} 
			else 
			{
				//Write the error with the first word set to identify as error
				Response.Write(Constants.CONST_ERROR + Constants.CONST_INTERNAL_SEPERATOR + 
					Constants.CONST_ERROR_PARAM);

			}//Check if the action is not null

		} //End of setData

		#endregion
		
	} //End of class

} //End of Namespace

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 Code Project Open License (CPOL)


Written By
Architect
Japan Japan
My computer career started as an Avid Gamer Smile | :) . I loved the old times Xatax and Wolf. But, curiosity took over my gaming side and I started to search on ways to create all the mesmerising programs that I used.
I started programming in Qbasic and then moved on to every possible programming language I could lay my hands on. Qbasic, Pascal, C, C++, Win32 SDK, VB 6.0, VB.NET, C#, Struts, Core Java, Javascript, VbScript, Unix Shell Programming, SQL SERVER PROGRAMMING, WMI scripting....are some of the languages I can boast of being proficient in.
My formal introduction to computers was when I took the Bachelor of Computer Sciences (Pune, India) degree.Later on I got Masters from the same university.
My Professional Certifications include
MCP SQL Server 2000 (70-229)
Brainbench Certified MS SQL Server Programmer
Brainbench Certified MS SQL Server Administrator
Brainbench Certified ASP.NET Programmer
Brainbench Certified Javascript Programmer
Brainbench Certified RDBMS Concepts

Comments and Discussions