Click here to Skip to main content
15,886,362 members
Articles / Web Development / ASP.NET

Race to Linux - Race 3: Reports Starter Kit using Mono SqlServer/Firebird

Rate me:
Please Sign up or sign in to vote.
2.33/5 (2 votes)
30 Sep 20052 min read 52.7K   328   15  
Reports Starter Kit port to Linux using Mono
using System;
using System.Web.UI.WebControls;
using ASPNET.StarterKit.Reports.Components;

namespace ASPNET.StarterKit.Reports
{
	public class Hierarchical : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataGrid TerritoryGrid;
		protected System.Web.UI.WebControls.DataGrid EmployeeTerritoryGrid;
		protected System.Web.UI.WebControls.DataGrid EmployeeGrid;

		protected System.Web.UI.WebControls.Label ETHeader;
		protected System.Web.UI.WebControls.Label EmployeeName;
	
		protected System.Web.UI.WebControls.Label CurrPage_Terr;
		protected System.Web.UI.WebControls.Label TotPages_Terr;

		protected System.Web.UI.WebControls.Label CurrPage_EmpTerr;
		protected System.Web.UI.WebControls.Label TotPages_EmpTerr;

		protected System.Web.UI.HtmlControls.HtmlTable ETTableColumn;
		protected System.Web.UI.HtmlControls.HtmlTable EmpTableColumn;

		protected string _styleSheet;
		protected Int32 _currentPageNumber_Terr = 1;
		protected System.Web.UI.WebControls.DropDownList ddlYear;
		protected System.Web.UI.WebControls.HyperLink PrintButton;
		protected Int32 _currentPageNumber_EmpTerr = 1;
	
		//*********************************************************************
		//
		// Hierarchical.aspx
		//
		// The Hierarchical.aspx page shows a basic way to filter related data 
		// in a grid based on information selected from another grid, which
		// in turn was filtered and grouped based on information selected from 
		// yet another grid.  This is a very common representation of a hierarchical
		// data structure.
		//
		// This is accomplished by displaying three different datagrids, each bound 
		// by separate stored procedures.  The second and third stored procedures
		// take in input parameters to filter the data, which are determined by 
		// which data item is selected in the prior, or parent, datagrid.
		//
		//*********************************************************************
	
		//****************
		//	PROPERTIES
		//****************
		string SortField_Terr
		{
			// SortField property is tracked in ViewState for both the
			// TerritoryGrid and the EmployeeTerritoryGrid	
			get 
			{
				object o = ViewState["SortField_Terr"];
				if (o == null) 
				{
					return String.Empty;
				}
				return (string)o;
			}
			set 
			{
				if (value == SortField_Terr) 
				{
					// same as current sort file, toggle sort direction
					SortAscending_Terr = !SortAscending_Terr;
				}
				ViewState["SortField_Terr"] = value;
			}
		}
		string SortField_EmpTerr 
		{
			get 
			{
				object o = ViewState["SortField_EmpTerr"];
				if (o == null) 
				{
					return String.Empty;
				}
				return (string)o;
			}
			set 
			{
				if (value == SortField_EmpTerr) 
				{
					// same as current sort file, toggle sort direction
					SortAscending_EmpTerr = !SortAscending_EmpTerr;
				}
				ViewState["SortField_EmpTerr"] = value;
			}
		}
 
		// SortAscending property is tracked in ViewState for both the
		// TerritoryGrid and the EmployeeTerritoryGrid			
		bool SortAscending_Terr 
		{
			get 
			{
				object o = ViewState["SortAscending_Terr"];

				if (o == null) 
				{
					return true;
				}
				return (bool)o;
			}
			set 
			{
				ViewState["SortAscending_Terr"] = value;
			}
		}
		bool SortAscending_EmpTerr 
		{
			get 
			{
				object o = ViewState["SortAscending_EmpTerr"];

				if (o == null) 
				{
					return true;
				}
				return (bool)o;
			}
			set 
			{
				ViewState["SortAscending_EmpTerr"] = value;
			}
		}

		//****************
		//	PAGE_LOAD
		//****************
		private void Page_Load(object sender, System.EventArgs e)
		{
			if (!IsPostBack)
			{
				// default sort will be by Territory Name for Territory Grid, and 
				// EmployeeName for Employee Grid
				if (SortField_Terr == "") 
					SortField_Terr = "Territory";

				if (SortField_EmpTerr == "") 
					SortField_EmpTerr = "EmployeeName";

				BindList_Terr();
			}

			// switches the style sheet based on printer friendly view or not
			if (Request.QueryString["Print"]=="true")
			{
				_styleSheet = "stylesPrint.css";
				PrintButton.Visible = true;
			}
			else 
			{
				_styleSheet = "styles.css";
			}
		}

		//****************
		//	DATA BINDING
		//****************

		//*********************************************************************
		//
		// The BindList_Terr method retrieves the list of Sales per Territory
		// and then databinds them to the Territory Grid control (1st control)
		//
		//*********************************************************************
		private void BindList_Terr()
		{
			HierarchicalReportCollection territoryList = HierarchicalReport.GetSalesByTerritory(Convert.ToInt32(ddlYear.SelectedItem.Value));
			
			// do the sorting if there are data returned
			if (territoryList.Count > 0) 
				SortGridData(territoryList, SortField_Terr, SortAscending_Terr);

			TerritoryGrid.DataSource = territoryList;
			TerritoryGrid.DataBind();

			// Update paging labels
			CurrPage_Terr.Text = Convert.ToString(TerritoryGrid.CurrentPageIndex + 1);
			TotPages_Terr.Text = TerritoryGrid.PageCount.ToString();
		}

		//*********************************************************************
		//
		// The BindList_EmpTerr method retrieves the list of Sales per 
		// Employee within a certain Territory, and then databinds them to the 
		// EmployeeTerritory Grid control (2nd control)
		//
		//*********************************************************************
		private void BindList_EmpTerr(string territoryName)
		{
			HierarchicalReportCollection empTerritoryList = HierarchicalReport.GetEmployeeSalesByTerritory(territoryName, Convert.ToInt32(ddlYear.SelectedItem.Value));
			
			// do the sorting if there are data returned
			if (empTerritoryList.Count > 0) 
			{
				SortGridData(empTerritoryList, SortField_EmpTerr, SortAscending_EmpTerr);

				EmployeeTerritoryGrid.DataSource = empTerritoryList;
				EmployeeTerritoryGrid.DataBind();
			}

			// Update paging labels
			CurrPage_EmpTerr.Text = Convert.ToString(EmployeeTerritoryGrid.CurrentPageIndex + 1);
			TotPages_EmpTerr.Text = EmployeeTerritoryGrid.PageCount.ToString();
		}

		//**************************************************************************
		//
		// The BindList_Emp method retrieves the information for a single Employee, 
		// and then databinds them to the Employee grid control (3rd control).
		//
		// There is no paging or sorting in this control.
		//
		//**************************************************************************
		private void BindList_Emp(int employeeID)
		{
			HierarchicalReportCollection employeeInfo = HierarchicalReport.GetEmployeeInfo(employeeID);
			
			EmployeeGrid.DataSource = employeeInfo;
			EmployeeGrid.DataBind();
		}

		//****************
		//	PAGING
		//****************

		//**************************************************************************
		//
		// The Page_* methods move the CurrentPageIndex to the appropriate
		// previous or next page for the 2 grid controls. 
		//
		//**************************************************************************

		protected void Page_Terr(Object sender,  DataGridPageChangedEventArgs e)
		{
			// Hide the 2nd and 3rd table if they were visible
			ETTableColumn.Visible = false;
			EmpTableColumn.Visible = false;

			TerritoryGrid.CurrentPageIndex = e.NewPageIndex;
			BindList_Terr();
		}

		protected void Page_EmpTerr(Object sender, DataGridPageChangedEventArgs e)
		{
			// Hide the 3rd table if it was visible
			EmpTableColumn.Visible = false;

			EmployeeTerritoryGrid.CurrentPageIndex = e.NewPageIndex;
			BindList_EmpTerr(ETHeader.Text);
		}


		//****************
		//	SORTING
		//****************

		//***********************************************************************
		//
		// The SortGridData methods sorts the array list bound to the datagrid 
		// based on which sort field is being selected.  This also handles reverse 
		// sorting based on the boolean.
		//
		// This method is used for both the Territory Grid control and the 
		// EmployeeTerritory Grid control.
		//
		//***********************************************************************
		private void SortGridData(HierarchicalReportCollection list, string sortField, bool asc)
		{
			HierarchicalReportCollection.HierarchicalReportFields sortCol = HierarchicalReportCollection.HierarchicalReportFields.InitValue;

			switch(sortField)
			{
				case "Territory":
					sortCol = HierarchicalReportCollection.HierarchicalReportFields.Territory;
					break;
				case "SalesTotals":
					sortCol = HierarchicalReportCollection.HierarchicalReportFields.SalesTotals;
					break;
				case "EmployeeName":
					sortCol = HierarchicalReportCollection.HierarchicalReportFields.EmployeeName;
					break;
			}

			list.Sort(sortCol, asc);
		}

		//*********************************************************************
		//
		// The SortGrid event handlers change the sortfield for the Territory  
		// grid and the EmployeeTerritory grid, respectively, and re-binds them.
		//
		//*********************************************************************
		protected void SortGrid_Terr(Object sender, DataGridSortCommandEventArgs e) 
		{
			// Hide the 2nd and 3rd table if they were visible
			ETTableColumn.Visible = false;
			EmpTableColumn.Visible = false;

			// change sort field
			SortField_Terr = (string)e.SortExpression;

			// move back to the first page
			TerritoryGrid.CurrentPageIndex = 0;

			// re-bind to display new sorting
			BindList_Terr();
		}

		protected void SortGrid_EmpTerr(Object sender, DataGridSortCommandEventArgs e) 
		{
			// Hide the 3rd table if it was visible
			EmpTableColumn.Visible = false;
			
			// change sort field
			SortField_EmpTerr = (string)e.SortExpression;

			// move back to the first page
			EmployeeTerritoryGrid.CurrentPageIndex = 0;

			// re-bind to display new sorting
			BindList_EmpTerr(ETHeader.Text);
		}


		//****************
		//	DRILLING DOWN
		//****************
		protected void TerritoryGrid_Click(Object sender, CommandEventArgs e)
		{
			string territoryName = e.CommandArgument.ToString().Trim();

			// Put the second grid back to the first page
			EmployeeTerritoryGrid.CurrentPageIndex = 0;

			// Show the table with the second grid, and update the headers
			ETTableColumn.Visible = true;
			ETHeader.Text = territoryName;
			BindList_EmpTerr(territoryName);

			// Hide the 3rd table if it was visible
			EmpTableColumn.Visible = false;
		}
		
		protected void ETGrid_Click(Object sender, CommandEventArgs e)
		{
			string[] cmdArgs =  e.CommandArgument.ToString().Split(',');

			string employeeName = cmdArgs[0];
			int employeeID = Convert.ToInt32(cmdArgs[1]);

			// Show the table with the 3rd grid, and update the headers
			EmpTableColumn.Visible = true;
			EmployeeName.Text = employeeName;
			BindList_Emp(employeeID);			
		}

		//****************
		//	DESIGNER CODE
		//****************

		#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.ddlYear.SelectedIndexChanged += new System.EventHandler(this.ddlYear_SelectedIndexChanged);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void ddlYear_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			// Put the first grid back to the first page
			TerritoryGrid.CurrentPageIndex = 0;

			// Hide the 2nd and 3rd table if they were visible
			ETTableColumn.Visible = false;
			EmpTableColumn.Visible = false;
			
			BindList_Terr();
		}
	}
}

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
Web Developer
Uruguay Uruguay
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions