Click here to Skip to main content
15,895,011 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 53.1K   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
{
	//********************************************************************************
	//
	// MasterDetail.aspx
	//
	// The MasterDetail.aspx page shows a basic way to filter and group related data (in
	// chis case, filtering by year, and grouping by quarters).  This is accomplished by
	// showing two different datagrids, each bound by separate stored procedures.
	//
	//********************************************************************************

	public class MasterDetail : System.Web.UI.Page
	{
		private const string _masterDetailYear = "master_detail_year";
		private const string _masterDetailQuarter = "master_detail_quarter";
		protected System.Web.UI.WebControls.DropDownList YearDropDownList;
		protected System.Web.UI.WebControls.DropDownList QuarterDropDownList;
		protected System.Web.UI.WebControls.DataGrid SummaryDataGrid;
		protected System.Web.UI.WebControls.DataGrid DetailsDataGrid;
		protected System.Web.UI.WebControls.Label SummaryLabel;
		protected System.Web.UI.WebControls.Label DetailsLabel;
		protected System.Web.UI.WebControls.HyperLink PrintButton;

		private double _salesTotal;
		protected string _styleSheet;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			_salesTotal = 0;
			if (!IsPostBack)
			{
				if (YearDropDownList.Items.FindByValue(Convert.ToString(Session[_masterDetailYear])) != null)
					YearDropDownList.Items.FindByValue(Convert.ToString(Session[_masterDetailYear])).Selected = true;
				if (QuarterDropDownList.Items.FindByValue(Convert.ToString(Session[_masterDetailQuarter])) != null)
					QuarterDropDownList.Items.FindByValue(Convert.ToString(Session[_masterDetailQuarter])).Selected = true;

				BindSummary();
				BindDetails();
			}

			// 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";
			}
		}

		#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.YearDropDownList.SelectedIndexChanged += new System.EventHandler(this.YearDropDownList_SelectedIndexChanged);
			this.QuarterDropDownList.SelectedIndexChanged += new System.EventHandler(this.QuarterDropDownList_SelectedIndexChanged);
			this.SummaryDataGrid.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.SumItems);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		//********************************************************************************
		//
		// The BindSummary method retrieves the OrderSummary for a given year, and
		// then databinds the results to the SummaryDataGrid.
		//
		//********************************************************************************

		private void BindSummary()
		{
			SummaryLabel.Text = YearDropDownList.SelectedItem.Text + " Summary";
			SummaryDataGrid.DataSource = GetSales(Convert.ToInt32(YearDropDownList.SelectedItem.Value));
			SummaryDataGrid.DataBind();
		}

		//********************************************************************************
		//
		// The BindDetails method retrieves the OrderDetails for a given year and quarter, and
		// then databinds the results to the DetailsDataGrid.
		//
		//********************************************************************************
		
		private void BindDetails()
		{
			DetailsLabel.Text = YearDropDownList.SelectedItem.Text;
			if (QuarterDropDownList.SelectedItem.Value == "0")
				DetailsLabel.Text += " (All Quarters) Details";
			else
				DetailsLabel.Text += " (Quarter " + QuarterDropDownList.SelectedItem.Text + ") Details";

			DetailsDataGrid.DataSource = GetSalesDetails(Convert.ToInt32(YearDropDownList.SelectedItem.Value), Convert.ToInt32(QuarterDropDownList.SelectedItem.Value));
			DetailsDataGrid.DataBind();
		}

		//********************************************************************************
		//
		// The GetSales uses the MasterDetail BLL component to query the database to retrieve
		// the summary of sales for a given year.
		//
		//********************************************************************************

		protected MasterDetailReportCollection GetSales(int year)
		{
			return MasterDetailReport.GetSummary(year);
		}

		//********************************************************************************
		//
		// The GetSalesDetails uses the MasterDetail BLL component to query the database to
		// retrieve the details of sales for a given year and quarter.
		//
		//********************************************************************************
		
		protected MasterDetailReportCollection GetSalesDetails(int year, int quarter)
		{
			return MasterDetailReport.GetDetails(year, quarter);
		}

		//********************************************************************************
		//
		// DropDownList event handlers for the Year and Quarter DropDownLists.  They make
		// calls to rebind the DetailsDataGrid (and if applicable) the SummaryDataGrid.
		//
		//********************************************************************************

		private void YearDropDownList_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			Session[_masterDetailYear] = YearDropDownList.SelectedItem.Value;
			BindSummary();
			BindDetails();
		}

		private void QuarterDropDownList_SelectedIndexChanged(object sender, System.EventArgs e) 
		{
			Session[_masterDetailQuarter] = QuarterDropDownList.SelectedItem.Value;
			BindDetails();
		} 

		//********************************************************************************
		//
		// The SumItems event handler is called after the masterDataGrid has been databound.
		// It iterates thru the list to calculate the sum of sales, and then adds the
		// results (as HTML) to the datagrid table.
		//
		//********************************************************************************

		private void SumItems(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				CalcTotal( e.Item.Cells[2].Text );
				e.Item.Cells[2].Text = string.Format("{0:c}", Convert.ToDouble(e.Item.Cells[2].Text));
			}
			else if (e.Item.ItemType == ListItemType.Footer )
			{
				e.Item.Cells[0].Text="Sales Total";
				e.Item.Cells[2].Text = string.Format("{0:c}", _salesTotal);
			}
		}

		private void CalcTotal(string _price)
		{
			_salesTotal += Double.Parse(_price);
		}
	}
}

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