Click here to Skip to main content
12,698,024 members (24,970 online)
Click here to Skip to main content

Stats

68K views
1.2K downloads
78 bookmarked
Posted

Add a Tree Menu to DataGrid Header Text

, 9 Nov 2005 CPOL
This article shows how to add a JavaScript tree menu to a DataGrid head text.
//------------------------------------
//Florence Li
//10/14/2005
//fzli@csi-soft.com
//------------------------------------
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.Configuration;
using System.Text;


namespace DataGridWithTreeMesu
{
	/// <summary>
	/// Summary description for HeaderTextWithTreeMenu.
	/// </summary>
	public class HeaderTextWithTreeMenu : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
		protected System.Web.UI.WebControls.Button bnDate;
		private bool bCheckIE = false;
		private string strChangeDateFormat = string.Empty;
		private string strBrowser = string.Empty;
		private string strDateFormat = string.Empty;
		private string strTimeFormat = string.Empty;
		private ArrayList alCells  = new ArrayList();
		private ArrayList alData = new ArrayList();
		private ArrayList al = new ArrayList();
		private string strMsg = "";
		

	
		private void Page_Load(object sender, System.EventArgs e)
		{
			//User Agent
			string strBrowser = Request.UserAgent;
			//Check if it's IE
			bCheckIE = CheckIE(strBrowser);

			//Get date format
			GetDateFormat();

			//strScriptDir from AppSettings["ScriptDir"]
			string strScriptDir = ConfigurationSettings.AppSettings["ScriptDir"];
			
			//Date Menu
			DateMenu(strBrowser, strScriptDir);

			//Create datasource
			string str1= "Smealy,Joe,2001.303,11:10,101";
			string str2= "Tom,Blankensmith,2004.140,23:01,102";
			string str3= "Mary,James,2005.200,6:03,103";

			al.Add(str1);
			al.Add(str2);
			al.Add(str3);

			for(int i = 0; i < al.Count; i++)
			{
				alData = GetCellData(al[i].ToString());
			}

			this.BindDataGid(alData);

			//strDateFormat from AppSettings["DateFormat"]
			strDateFormat  =  ConfigurationSettings.AppSettings["DateFormat"].ToLower();

		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			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.bnDate.Click += new System.EventHandler(this.bnDate_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion


		/// <summary>
		/// BindDataGid
		/// </summary>
		/// <PARAM name="alData">ArrayList</PARAM>
		private void BindDataGid(ArrayList alData)
		{
			this.SetHeadText(); 

			DataGrid1.DataSource = alData;
			DataGrid1.DataBind();
		}

		#region Text Headings
		/// <summary>
		/// SetHeadText: "Date"
		/// </summary>
		private void SetHeadText()
		{
			string headtext = "";

			//date from AppSettings["HeaderDate"]
			string date = ConfigurationSettings.AppSettings["HeaderDate"];
			
			if(date.Length > 0)
			{
				//Note add an "onMouseOver" event
				headtext =
					"<a href='#'"+" onMouseOver='HM_f_PopUp(\"elMenu1\",event)'"+" onclick='javascript:document.getElementById(\"{0}\").click();'>" + date +"</a>";
			}
			else
			{
				headtext =
					"<a href='#'"+" onMouseOver='HM_f_PopUp(\"elMenu1\",event)'"+" onclick='javascript:document.getElementById(\"{0}\").click();'>Date</a>";
			}
			//Set Header Text
			this.DataGrid1.Columns[2].HeaderText = string.Format(headtext,this.bnDate.ClientID);
		}
		#endregion

		/// <summary>
		/// GetCellData
		/// </summary>
		/// <PARAM name="strRow">string</PARAM>
		private ArrayList GetCellData(string strRow)
		{
			
			string [] cells = null;
			Person myPerson = new Person();
			cells = strRow.Split(',');

			//Get cells
			for(int iCel=0; iCel<cells.Length; iCel++)
			{
				//Date format
				string strDate = "";

				MyDateTime dateTime = new MyDateTime();
				
				string strTempDate =cells[2].ToString();
					
				if(strDateFormat == "yyyy.ddd")
				{ 
					strDate =strTempDate;
				}
				else
					strDate = dateTime.JulianToGregorian(strTempDate, strDateFormat);
				
				myPerson.LastName  = cells[0].ToString();
				myPerson.FirstName = cells[1].ToString();
				myPerson.Date      = strDate;
				myPerson.Time      = cells[3].ToString();
				myPerson.PersonID  = cells[4].ToString();

			}

			alData.Add(myPerson);
	
			//return row data
			return alData;
			
		}

		/// <summary>
		/// DateMenu
		/// </summary>
		/// <PARAM name="strBrowser">string</PARAM>
		/// <PARAM name="strScriptDir">string</PARAM>
		private void DateMenu(string strBrowser, string strScriptDir)
		{
			StringBuilder sb = new StringBuilder();

			bool IsSecure;
			string strAddress = "";
			IsSecure = Request.IsSecureConnection;
			Uri MyUrl = Request.Url;
			string strUri = MyUrl.AbsoluteUri;
			string strScriptName = MyUrl.AbsoluteUri;
			if (IsSecure==true )
			{
				strAddress = MyUrl.AbsoluteUri.Replace("http", "https");
			}
			else
			{
				strAddress = MyUrl.AbsoluteUri;
			}

			string strHrefParam,strSortDate;
			string strGregorian1,strGregorian2,strGregorian3,strGregorian4;
			string strJulian;

			strHrefParam = "ChangeDateFormat=" + strChangeDateFormat;
			
			strSortDate   = strAddress + "?" + strHrefParam +
				"&DateFormat=" + strDateFormat;
			strGregorian1 = strAddress + "?" + strHrefParam +
				"&DateFormat=" + "mm/dd/yyyy" ;
			strGregorian2 =strAddress + "?" + strHrefParam +
				"&DateFormat=" + "dd/mm/yyyy";
			strGregorian3 = strAddress + "?" + strHrefParam +
				"&DateFormat=" + "mm/dd/yy";
			strGregorian4 = strAddress + "?" + strHrefParam +
				"&DateFormat=" + "dd/mm/yy";
			strJulian = strAddress + "?" + strHrefParam +
				"&DateFormat=" + "yyyy.ddd";

			sb.Append("<script language='JavaScript1.2'>" + "\n\r");	
			sb.Append("var varDate ='" + strSortDate +  "'; " +"\n\r");
			sb.Append("var varGregorian1 ='" + strGregorian1 +  "'; " + "\n\r");	 	
			sb.Append("var varGregorian2 ='" + strGregorian2 +  "'; " + "\n\r");	 	
			sb.Append("var varGregorian3 ='" + strGregorian3 +  "'; " + "\n\r");	 	 
			sb.Append("var varGregorian4 ='" + strGregorian4 +  "'; "+ "\n\r");	 	
			sb.Append("var varJulian ='" + strJulian +  "'; " + "\n\r");	
			
			if(bCheckIE==true) 
			{
				sb.Append ("HM_Array1 = [");
				sb.Append ("	[90],         ");
				sb.Append ("	['Others', varDate, 1,0,0],");
				sb.Append ("	['Format Date','',1,0,1] ");
				sb.Append ("	];" + "\n\r");	
				sb.Append ("HM_Array1_2 = [");
				sb.Append ("	[],");
				sb.Append ("	['MM/DD/YYYY', varGregorian1 ,1,0,0],");
				sb.Append ("	['DD/MM/YYYY', varGregorian2 ,1,0,0],");
				sb.Append ("	['MM/DD/YY', varGregorian3 ,1,0,0],");
				sb.Append ("	['DD/MM/YY', varGregorian4 ,1,0,0],");
				sb.Append ("	['YYYY.DDD', varJulian ,1,0,0] ");
				sb.Append ("	] " + "\n\r");
			}															  
			else
			{
				sb.Append ("HM_Array1 = [");
				sb.Append ("	[90],         ");
				sb.Append ("	['Sort Date', varDate, 1,0,0],");
				sb.Append ("	['MM/DD/YYYY', varGregorian1 ,1,0,0],");
				sb.Append ("	['DD/MM/YYYY', varGregorian2 ,1,0,0],");
				sb.Append ("	['MM/DD/YY', varGregorian3 ,1,0,0],");
				sb.Append ("	['DD/MM/YY', varGregorian4 ,1,0,0],");
				sb.Append ("	['YYYY.DDD', varJulian ,1,0,0] ");
				sb.Append ("	]" + "\n\r");
			}

					 
			sb.Append ("HM_DOM = (document.getElementById) ? true : false;" + "\n\r");
			sb.Append ("HM_NS4 = (document.layers) ? true : false;" + "\n\r");
			sb.Append ("HM_IE = (document.all) ? true : false;" + "\n\r");
			sb.Append ("HM_IE4 = HM_IE && !HM_DOM;" + "\n\r");
			sb.Append ("HM_Mac = (navigator.appVersion.indexOf('Mac') != -1);" + "\n\r");
			sb.Append ("HM_IE4M = HM_IE4 && HM_Mac;" + "\n\r");
			sb.Append ("HM_IsMenu = (HM_DOM || HM_NS4 || (HM_IE4 && !HM_IE4M));" + "\n\r");
			sb.Append ("HM_BrowserString = HM_NS4 ? 'NS4' : HM_DOM ? 'DOM' : 'IE4';" + "\n\r");

			sb.Append ("if(window.event + '' == 'undefined') event = null;" + "\n\r");	
			sb.Append ("function HM_f_PopUp(){return false};" + "\n\r") ;
			sb.Append ("popUp = HM_f_PopUp;" + "\n\r");	
			sb.Append ("</script>" + "\n\r");
			sb.Append ("\n\r");
			
			sb.Append ("<script language='JavaScript' src='"+strScriptDir+ "/web_menu.js'></script>" + "\n\r");
			sb.Append("<script language='JavaScript' src='Script/web_scriptiens6.js'></script>" + "\n\r");

			Response.Write( sb.ToString());
		}

		/// <summary>
		/// GetDateFormat
		/// </summary>
		private void GetDateFormat()
		{
			//tempDate from AppSettings
			string tempDate = ConfigurationSettings.AppSettings["DateFormat"].ToLower();
			if(tempDate == "" || tempDate == null)
				tempDate = "mm/dd/yyyy";

			//strDateFormat from QueryString
			strDateFormat= Request.QueryString["DateFormat"];

			string [] arTempDateFormat = null;
			if(strDateFormat == null || strDateFormat == "")
				strDateFormat= tempDate;
			else if(strDateFormat.IndexOf(',') != -1)
			{
				arTempDateFormat = strDateFormat.Split(',');

				int len = arTempDateFormat.Length;
				for(int iDateFormat=0; iDateFormat<len; iDateFormat++)
				{
					strDateFormat = arTempDateFormat[len-1];
				}
			}

			//Display an error message when appSettings "DateFormat" key is invalid.
			if (strDateFormat != "mm/dd/yyyy" && strDateFormat != "dd/mm/yyyy" &&
				strDateFormat != "mm/dd/yyyy" && strDateFormat != "mm/dd/yy" && 
				strDateFormat != "dd/mm/yy" && strDateFormat != "yyyy.ddd")
			{
				strMsg = "";
				strMsg = "Date Format is incorrect:( " + strDateFormat + ")";
				strMsg = doErrorMsg(strMsg);
				Response.Write(strMsg);
			}
		}

		/// <summary>
		/// CheckIE
		/// </summary>
		/// <PARAM name="strBrowser">string</PARAM>
		private bool CheckIE(string strBrowser)
		{
			strBrowser = Request.UserAgent;
			if (strBrowser.IndexOf("MSIE 6.0") > -1 || strBrowser.IndexOf("MSIE 5.0") > -1) 
			{
				// The browser is Microsoft Internet Explorer Version 6.0.
				bCheckIE = true;
			}
			else 
			{
				bCheckIE = false;
			}

			return bCheckIE;
		}


		/// <summary>
		/// doErrorMsg: Error message box
		/// </summary>
		/// <PARAM name="strMsg">string</PARAM>
		public string doErrorMsg(string strMsg)
		{
			string strError="";

			strMsg = strMsg.Replace("\"", "'");

			strError += "<script>" + "\r\n";
			strError += "var obj=\"" + strMsg + "\";" + "\r\n";
			strError += "alert(obj);" + "\r\n";
			strError += "</script>"  + "\r\n";

			return strError;
		}

		/// <summary>
		/// bnDate_Click
		/// </summary>
		///<PARAM name="sender">object</PARAM>
		///<PARAM name="e">System.EventArgs</PARAM>
		private void bnDate_Click(object sender, System.EventArgs e)
		{
			Response.Write("You Clicked the Header Text \"Date\" ");
		}

	}//class
}//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)

Share

About the Author

Florence FZ Li
Web Developer
United States United States
M.S.: Computer Science, B.S.: Physics, MCSD: .NET, MCSD: VS 6

Florence currently works at Confident Software, Inc. Atlanta, U.S.A. Besides programming, during her spare time she enjoys opera.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 9 Nov 2005
Article Copyright 2005 by Florence FZ Li
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid