Click here to Skip to main content
15,896,153 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.2K   328   15  
Reports Starter Kit port to Linux using Mono
<html><head><link rel=stylesheet href=style.css></head><body><div class=SourcePanel style='font-size:12'><pre style='background-color:white'>
<font color= "blue">using</font> System;
<font color= "blue">using</font> System.Web.UI.WebControls;
<font color= "blue">using</font> ASPNET.StarterKit.Reports.Components;
<font color= "blue"></font>
<font color= "blue">namespace</font> ASPNET.StarterKit.Reports
<font color= "blue"></font>{
<font color= "green">    //*********************************************************************</font>
<font color= "green">    //</font>
<font color= "green">    // Tabular.aspx</font>
<font color= "green">    //</font>
<font color= "green">    // The Tabular.aspx page shows a basic technique for grouping related data </font>
<font color= "green">    // (in this case, grouping products by category). This is accomplished by nesting a </font>
<font color= "green">    // DataGrid control inside a DataList control. Additionally, this report demonstrates </font>
<font color= "green">    // how easy it is to implement interactive sorting in the report using ASP.NET.</font>
<font color= "green">    //</font>
<font color= "green">    //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">    public class</font> Tabular : System.Web.UI.Page
<font color= "blue">    </font>{
<font color= "blue">        protected </font>System.Web.UI.WebControls.DataList CategoriesList;
<font color= "blue">        protected </font>System.Web.UI.WebControls.HyperLink PrintButton;
<font color= "blue">        protected </font>string _styleSheet;
<font color= "blue">    </font>
<font color= "blue">        private void</font> Page_Load(object sender, System.EventArgs e)
<font color= "blue">        </font>{
<font color= "blue"></font><font color= "blue">            if </font>(!IsPostBack)<font color= "blue"></font>
<font color= "blue">            </font>{
<font color= "green">                // default sort will be by product name</font>
<font color= "blue"></font><font color= "blue">                if </font>(SortField == "") <font color= "blue"></font>
<font color= "blue">                    </font>SortField = "ProductName";
<font color= "blue"></font>
<font color= "blue">                </font>BindList();
<font color= "blue">            </font>}
<font color= "blue"></font>
<font color= "green">            // switches the style sheet based on printer friendly view or not</font>
<font color= "blue"></font><font color= "blue">            if </font>(Request.QueryString["Print"]=="true")<font color= "blue"></font>
<font color= "blue">            </font>{
<font color= "blue">                </font>_styleSheet = "stylesPrint.css";
<font color= "blue">                </font>PrintButton.Visible = true;
<font color= "blue">            </font>}
<font color= "blue"></font><font color= "blue">            else </font><font color= "blue"></font>
<font color= "blue">            </font>{
<font color= "blue">                </font>_styleSheet = "styles.css";
<font color= "blue">            </font>}
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "blue">        </font>#region Web Form Designer generated code
<font color= "blue">        </font>override protected void OnInit(EventArgs e)
<font color= "blue">        </font>{
<font color= "green">            //</font>
<font color= "green">            // CODEGEN: This call is required by the ASP.NET Web Form Designer.</font>
<font color= "green">            //</font>
<font color= "blue">            </font>InitializeComponent();
<font color= "blue">            </font>base.OnInit(e);
<font color= "blue">        </font>}
<font color= "blue">        </font>
<font color= "green">        /// <summary></font>
<font color= "green">        /// Required method for Designer support - do not modify</font>
<font color= "green">        /// the contents of this method with the code editor.</font>
<font color= "green">        /// </summary></font>
<font color= "blue">        private void</font> InitializeComponent()
<font color= "blue">        </font>{    
<font color= "blue">            </font>this.Load += new System.EventHandler(this.Page_Load);
<font color= "blue"></font>
<font color= "blue">        </font>}
<font color= "blue">        </font>#endregion
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // The BindList method retrieves the list of categories</font>
<font color= "green">        // and then data binds them to the ProductsByCategoryList</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> BindList()
<font color= "blue">        </font>{
<font color= "blue">            </font>CategoriesList.DataSource = TabularReport.GetCategories();
<font color= "blue">            </font>CategoriesList.DataBind();
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // The GetDetails method calls the BLL to retrieve product for a category given</font>
<font color= "green">        // a category id.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        protected </font>TabularReportCollection GetDetails(int categoryID)
<font color= "blue">        </font>{
<font color= "blue">            </font>TabularReportCollection detailList = TabularReport.GetProducts(categoryID);
<font color= "blue"></font>
<font color= "green">            // do the sorting if there are data returned</font>
<font color= "blue"></font><font color= "blue">            if </font>(detailList.Count > 0) <font color= "blue"></font>
<font color= "blue">                </font>SortGridData(detailList, SortField, SortAscending);
<font color= "blue"></font>
<font color= "blue">            return</font> detailList;
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*******************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // SortGridData methods sorts the datagrid based on which</font>
<font color= "green">        // sort field is being selected.  Also does reverse sorting based on the boolean.</font>
<font color= "green">        //</font>
<font color= "green">        //*******************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> SortGridData(TabularReportCollection list, string sortField, bool asc)
<font color= "blue">        </font>{
<font color= "blue">            </font>TabularReportCollection.TabularReportFields sortCol = TabularReportCollection.TabularReportFields.InitValue;
<font color= "blue"></font>
<font color= "blue">            </font>switch(sortField)
<font color= "blue">            </font>{
<font color= "blue">                case</font> "ProductName":
<font color= "blue">                    </font>sortCol = TabularReportCollection.TabularReportFields.ProductName;
<font color= "blue">                    </font>break;
<font color= "blue">                case</font> "UnitPrice":
<font color= "blue">                    </font>sortCol = TabularReportCollection.TabularReportFields.UnitPrice;
<font color= "blue">                    </font>break;
<font color= "blue">                case</font> "UnitsInStock":
<font color= "blue">                    </font>sortCol = TabularReportCollection.TabularReportFields.UnitsInStock;
<font color= "blue">                    </font>break;
<font color= "blue">            </font>}
<font color= "blue"></font>
<font color= "blue">            </font>list.Sort(sortCol, asc);
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*******************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // CalculateExtendedPrice event handler is tied to the OnItemDataBound of the Datagrid.</font>
<font color= "green">        // It retrieves the unit price and multiplies that to the units in stock to calculate the extended price of a product.</font>
<font color= "green">        // The extended price is displayed as the right most column.</font>
<font color= "green">        //</font>
<font color= "green">        //*******************************************************</font>
<font color= "blue"></font>
<font color= "blue">        protected void</font> CalculateExtendedPrice(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
<font color= "blue">        </font>{
<font color= "blue"></font><font color= "blue">            if </font>(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)<font color= "blue"></font>
<font color= "blue">            </font>{
<font color= "blue">                </font>double unitsInStock = Convert.ToDouble(e.Item.Cells[1].Text);
<font color= "blue">                </font>double unitPrice =  Convert.ToDouble(e.Item.Cells[4].Text);
<font color= "blue">                </font>e.Item.Cells[5].Text = string.Format("{0:c}", unitPrice * unitsInStock);
<font color= "blue"></font>
<font color= "green">                // format unit price column as currency</font>
<font color= "blue">                </font>e.Item.Cells[4].Text = string.Format("{0:c}", unitPrice);
<font color= "blue"></font>
<font color= "green">                // if there are no units in stock, color the line item as red by changing the style class</font>
<font color= "blue"></font><font color= "blue">                if </font>(unitsInStock == 0)<font color= "blue"></font>
<font color= "blue">                    </font>e.Item.CssClass = "OutOfStock";
<font color= "blue">            </font>}
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // The SortGrid event handler changes the sortfield for the Projects grid </font>
<font color= "green">        // and re-binds it.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        protected void</font> SortGrid(Object sender, DataGridSortCommandEventArgs e) 
<font color= "blue">        </font>{
<font color= "green">            // change sort field</font>
<font color= "blue">            </font>SortField = (string)e.SortExpression;
<font color= "blue"></font>
<font color= "green">            // re-bind to display new sorting</font>
<font color= "blue">            </font>BindList();
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "blue">        </font>string SortField 
<font color= "blue">        </font>{
<font color= "blue">            get</font> 
<font color= "blue">            </font>{
<font color= "blue">                </font>object o = ViewState["SortField"];
<font color= "blue"></font><font color= "blue">                if </font>(o == null) <font color= "blue"></font>
<font color= "blue">                </font>{
<font color= "blue">                    return</font> String.Empty;
<font color= "blue">                </font>}
<font color= "blue">                return</font> (string)o;
<font color= "blue">            </font>}
<font color= "blue">            set</font> 
<font color= "blue">            </font>{
<font color= "blue"></font><font color= "blue">                if </font>(value == SortField) <font color= "blue"></font>
<font color= "blue">                </font>{
<font color= "green">                    // same as current sort file, toggle sort direction</font>
<font color= "blue">                    </font>SortAscending = !SortAscending;
<font color= "blue">                </font>}
<font color= "blue">                </font>ViewState["SortField"] = value;
<font color= "blue">            </font>}
<font color= "blue">        </font>}
<font color= "blue"> </font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // SortAscending property is tracked in ViewState</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        </font>bool SortAscending 
<font color= "blue">        </font>{
<font color= "blue">            get</font> 
<font color= "blue">            </font>{
<font color= "blue">                </font>object o = ViewState["SortAscending"];
<font color= "blue"></font>
<font color= "blue"></font><font color= "blue">                if </font>(o == null) <font color= "blue"></font>
<font color= "blue">                </font>{
<font color= "blue">                    return</font> true;
<font color= "blue">                </font>}
<font color= "blue">                return</font> (bool)o;
<font color= "blue">            </font>}
<font color= "blue">            set</font> 
<font color= "blue">            </font>{
<font color= "blue">                </font>ViewState["SortAscending"] = value;
<font color= "blue">            </font>}
<font color= "blue">        </font>}
<font color= "blue">    </font>}
<font color= "blue"></font>}
</pre>

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