Click here to Skip to main content
15,891,951 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 53K   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
{
	//*********************************************************************
	//
	// Tabular.aspx
	//
	// The Tabular.aspx page shows a basic technique for grouping related data 
	// (in this case, grouping products by category). This is accomplished by nesting a 
	// DataGrid control inside a DataList control. Additionally, this report demonstrates 
	// how easy it is to implement interactive sorting in the report using ASP.NET.
	//
	//*********************************************************************

	public class Tabular : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataList CategoriesList;
		protected System.Web.UI.WebControls.HyperLink PrintButton;
		protected string _styleSheet;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if (!IsPostBack)
			{
				// default sort will be by product name
				if (SortField == "") 
					SortField = "ProductName";

				BindList();
			}

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

		}
		#endregion

		//*********************************************************************
		//
		// The BindList method retrieves the list of categories
		// and then data binds them to the ProductsByCategoryList
		//
		//*********************************************************************

		private void BindList()
		{
			CategoriesList.DataSource = TabularReport.GetCategories();
			CategoriesList.DataBind();
		}

		//*********************************************************************
		//
		// The GetDetails method calls the BLL to retrieve product for a category given
		// a category id.
		//
		//*********************************************************************

		protected TabularReportCollection GetDetails(int categoryID)
		{
			TabularReportCollection detailList = TabularReport.GetProducts(categoryID);

			// do the sorting if there are data returned
			if (detailList.Count > 0) 
				SortGridData(detailList, SortField, SortAscending);

			return detailList;
		}

		//*******************************************************
		//
		// SortGridData methods sorts the datagrid based on which
		// sort field is being selected.  Also does reverse sorting based on the boolean.
		//
		//*******************************************************

		private void SortGridData(TabularReportCollection list, string sortField, bool asc)
		{
			TabularReportCollection.TabularReportFields sortCol = TabularReportCollection.TabularReportFields.InitValue;

			switch(sortField)
			{
				case "ProductName":
					sortCol = TabularReportCollection.TabularReportFields.ProductName;
					break;
				case "UnitPrice":
					sortCol = TabularReportCollection.TabularReportFields.UnitPrice;
					break;
				case "UnitsInStock":
					sortCol = TabularReportCollection.TabularReportFields.UnitsInStock;
					break;
			}

			list.Sort(sortCol, asc);
		}

		//*******************************************************
		//
		// CalculateExtendedPrice event handler is tied to the OnItemDataBound of the Datagrid.
		// It retrieves the unit price and multiplies that to the units in stock to calculate the extended price of a product.
		// The extended price is displayed as the right most column.
		//
		//*******************************************************

		protected void CalculateExtendedPrice(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				double unitsInStock = Convert.ToDouble(e.Item.Cells[1].Text);
				double unitPrice =  Convert.ToDouble(e.Item.Cells[4].Text);
				e.Item.Cells[5].Text = string.Format("{0:c}", unitPrice * unitsInStock);

				// format unit price column as currency
				e.Item.Cells[4].Text = string.Format("{0:c}", unitPrice);

				// if there are no units in stock, color the line item as red by changing the style class
				if (unitsInStock == 0)
					e.Item.CssClass = "OutOfStock";
			}
		}

		//*********************************************************************
		//
		// The SortGrid event handler changes the sortfield for the Projects grid 
		// and re-binds it.
		//
		//*********************************************************************

		protected void SortGrid(Object sender, DataGridSortCommandEventArgs e) 
		{
			// change sort field
			SortField = (string)e.SortExpression;

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

		string SortField 
		{
			get 
			{
				object o = ViewState["SortField"];
				if (o == null) 
				{
					return String.Empty;
				}
				return (string)o;
			}
			set 
			{
				if (value == SortField) 
				{
					// same as current sort file, toggle sort direction
					SortAscending = !SortAscending;
				}
				ViewState["SortField"] = value;
			}
		}
 
		//*********************************************************************
		//
		// SortAscending property is tracked in ViewState
		//
		//*********************************************************************

		bool SortAscending 
		{
			get 
			{
				object o = ViewState["SortAscending"];

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

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