|
<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= "blue"> public class</font> Hierarchical : System.Web.UI.Page
<font color= "blue"> </font>{
<font color= "blue"> protected </font>System.Web.UI.WebControls.DataGrid TerritoryGrid;
<font color= "blue"> protected </font>System.Web.UI.WebControls.DataGrid EmployeeTerritoryGrid;
<font color= "blue"> protected </font>System.Web.UI.WebControls.DataGrid EmployeeGrid;
<font color= "blue"></font>
<font color= "blue"> protected </font>System.Web.UI.WebControls.Label ETHeader;
<font color= "blue"> protected </font>System.Web.UI.WebControls.Label EmployeeName;
<font color= "blue"> </font>
<font color= "blue"> protected </font>System.Web.UI.WebControls.Label CurrPage_Terr;
<font color= "blue"> protected </font>System.Web.UI.WebControls.Label TotPages_Terr;
<font color= "blue"></font>
<font color= "blue"> protected </font>System.Web.UI.WebControls.Label CurrPage_EmpTerr;
<font color= "blue"> protected </font>System.Web.UI.WebControls.Label TotPages_EmpTerr;
<font color= "blue"></font>
<font color= "blue"> protected </font>System.Web.UI.HtmlControls.HtmlTable ETTableColumn;
<font color= "blue"> protected </font>System.Web.UI.HtmlControls.HtmlTable EmpTableColumn;
<font color= "blue"></font>
<font color= "blue"> protected </font>string _styleSheet;
<font color= "blue"> protected </font>Int32 _currentPageNumber_Terr = 1;
<font color= "blue"> protected </font>System.Web.UI.WebControls.DropDownList ddlYear;
<font color= "blue"> protected </font>System.Web.UI.WebControls.HyperLink PrintButton;
<font color= "blue"> protected </font>Int32 _currentPageNumber_EmpTerr = 1;
<font color= "blue"> </font>
<font color= "green"> //*********************************************************************</font>
<font color= "green"> //</font>
<font color= "green"> // Hierarchical.aspx</font>
<font color= "green"> //</font>
<font color= "green"> // The Hierarchical.aspx page shows a basic way to filter related data </font>
<font color= "green"> // in a grid based on information selected from another grid, which</font>
<font color= "green"> // in turn was filtered and grouped based on information selected from </font>
<font color= "green"> // yet another grid. This is a very common representation of a hierarchical</font>
<font color= "green"> // data structure.</font>
<font color= "green"> //</font>
<font color= "green"> // This is accomplished by displaying three different datagrids, each bound </font>
<font color= "green"> // by separate stored procedures. The second and third stored procedures</font>
<font color= "green"> // take in input parameters to filter the data, which are determined by </font>
<font color= "green"> // which data item is selected in the prior, or parent, datagrid.</font>
<font color= "green"> //</font>
<font color= "green"> //*********************************************************************</font>
<font color= "blue"> </font>
<font color= "green"> //****************</font>
<font color= "green"> // PROPERTIES</font>
<font color= "green"> //****************</font>
<font color= "blue"> </font>string SortField_Terr
<font color= "blue"> </font>{
<font color= "green"> // SortField property is tracked in ViewState for both the</font>
<font color= "green"> // TerritoryGrid and the EmployeeTerritoryGrid </font>
<font color= "blue"> get</font>
<font color= "blue"> </font>{
<font color= "blue"> </font>object o = ViewState["SortField_Terr"];
<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_Terr) <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_Terr = !SortAscending_Terr;
<font color= "blue"> </font>}
<font color= "blue"> </font>ViewState["SortField_Terr"] = value;
<font color= "blue"> </font>}
<font color= "blue"> </font>}
<font color= "blue"> </font>string SortField_EmpTerr
<font color= "blue"> </font>{
<font color= "blue"> get</font>
<font color= "blue"> </font>{
<font color= "blue"> </font>object o = ViewState["SortField_EmpTerr"];
<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_EmpTerr) <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_EmpTerr = !SortAscending_EmpTerr;
<font color= "blue"> </font>}
<font color= "blue"> </font>ViewState["SortField_EmpTerr"] = value;
<font color= "blue"> </font>}
<font color= "blue"> </font>}
<font color= "blue"> </font>
<font color= "green"> // SortAscending property is tracked in ViewState for both the</font>
<font color= "green"> // TerritoryGrid and the EmployeeTerritoryGrid </font>
<font color= "blue"> </font>bool SortAscending_Terr
<font color= "blue"> </font>{
<font color= "blue"> get</font>
<font color= "blue"> </font>{
<font color= "blue"> </font>object o = ViewState["SortAscending_Terr"];
<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_Terr"] = value;
<font color= "blue"> </font>}
<font color= "blue"> </font>}
<font color= "blue"> </font>bool SortAscending_EmpTerr
<font color= "blue"> </font>{
<font color= "blue"> get</font>
<font color= "blue"> </font>{
<font color= "blue"> </font>object o = ViewState["SortAscending_EmpTerr"];
<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_EmpTerr"] = value;
<font color= "blue"> </font>}
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "green"> //****************</font>
<font color= "green"> // PAGE_LOAD</font>
<font color= "green"> //****************</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 Territory Name for Territory Grid, and </font>
<font color= "green"> // EmployeeName for Employee Grid</font>
<font color= "blue"></font><font color= "blue"> if </font>(SortField_Terr == "") <font color= "blue"></font>
<font color= "blue"> </font>SortField_Terr = "Territory";
<font color= "blue"></font>
<font color= "blue"></font><font color= "blue"> if </font>(SortField_EmpTerr == "") <font color= "blue"></font>
<font color= "blue"> </font>SortField_EmpTerr = "EmployeeName";
<font color= "blue"></font>
<font color= "blue"> </font>BindList_Terr();
<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= "green"> //****************</font>
<font color= "green"> // DATA BINDING</font>
<font color= "green"> //****************</font>
<font color= "blue"></font>
<font color= "green"> //*********************************************************************</font>
<font color= "green"> //</font>
<font color= "green"> // The BindList_Terr method retrieves the list of Sales per Territory</font>
<font color= "green"> // and then databinds them to the Territory Grid control (1st control)</font>
<font color= "green"> //</font>
<font color= "green"> //*********************************************************************</font>
<font color= "blue"> private void</font> BindList_Terr()
<font color= "blue"> </font>{
<font color= "blue"> </font>HierarchicalReportCollection territoryList = HierarchicalReport.GetSalesByTerritory(Convert.ToInt32(ddlYear.SelectedItem.Value));
<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>(territoryList.Count > 0) <font color= "blue"></font>
<font color= "blue"> </font>SortGridData(territoryList, SortField_Terr, SortAscending_Terr);
<font color= "blue"></font>
<font color= "blue"> </font>TerritoryGrid.DataSource = territoryList;
<font color= "blue"> </font>TerritoryGrid.DataBind();
<font color= "blue"></font>
<font color= "green"> // Update paging labels</font>
<font color= "blue"> </font>CurrPage_Terr.Text = Convert.ToString(TerritoryGrid.CurrentPageIndex + 1);
<font color= "blue"> </font>TotPages_Terr.Text = TerritoryGrid.PageCount.ToString();
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "green"> //*********************************************************************</font>
<font color= "green"> //</font>
<font color= "green"> // The BindList_EmpTerr method retrieves the list of Sales per </font>
<font color= "green"> // Employee within a certain Territory, and then databinds them to the </font>
<font color= "green"> // EmployeeTerritory Grid control (2nd control)</font>
<font color= "green"> //</font>
<font color= "green"> //*********************************************************************</font>
<font color= "blue"> private void</font> BindList_EmpTerr(string territoryName)
<font color= "blue"> </font>{
<font color= "blue"> </font>HierarchicalReportCollection empTerritoryList = HierarchicalReport.GetEmployeeSalesByTerritory(territoryName, Convert.ToInt32(ddlYear.SelectedItem.Value));
<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>(empTerritoryList.Count > 0) <font color= "blue"></font>
<font color= "blue"> </font>{
<font color= "blue"> </font>SortGridData(empTerritoryList, SortField_EmpTerr, SortAscending_EmpTerr);
<font color= "blue"></font>
<font color= "blue"> </font>EmployeeTerritoryGrid.DataSource = empTerritoryList;
<font color= "blue"> </font>EmployeeTerritoryGrid.DataBind();
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "green"> // Update paging labels</font>
<font color= "blue"> </font>CurrPage_EmpTerr.Text = Convert.ToString(EmployeeTerritoryGrid.CurrentPageIndex + 1);
<font color= "blue"> </font>TotPages_EmpTerr.Text = EmployeeTerritoryGrid.PageCount.ToString();
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "green"> //**************************************************************************</font>
<font color= "green"> //</font>
<font color= "green"> // The BindList_Emp method retrieves the information for a single Employee, </font>
<font color= "green"> // and then databinds them to the Employee grid control (3rd control).</font>
<font color= "green"> //</font>
<font color= "green"> // There is no paging or sorting in this control.</font>
<font color= "green"> //</font>
<font color= "green"> //**************************************************************************</font>
<font color= "blue"> private void</font> BindList_Emp(int employeeID)
<font color= "blue"> </font>{
<font color= "blue"> </font>HierarchicalReportCollection employeeInfo = HierarchicalReport.GetEmployeeInfo(employeeID);
<font color= "blue"> </font>
<font color= "blue"> </font>EmployeeGrid.DataSource = employeeInfo;
<font color= "blue"> </font>EmployeeGrid.DataBind();
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "green"> //****************</font>
<font color= "green"> // PAGING</font>
<font color= "green"> //****************</font>
<font color= "blue"></font>
<font color= "green"> //**************************************************************************</font>
<font color= "green"> //</font>
<font color= "green"> // The Page_* methods move the CurrentPageIndex to the appropriate</font>
<font color= "green"> // previous or next page for the 2 grid controls. </font>
<font color= "green"> //</font>
<font color= "green"> //**************************************************************************</font>
<font color= "blue"></font>
<font color= "blue"> protected void</font> Page_Terr(Object sender, DataGridPageChangedEventArgs e)
<font color= "blue"> </font>{
<font color= "green"> // Hide the 2nd and 3rd table if they were visible</font>
<font color= "blue"> </font>ETTableColumn.Visible = false;
<font color= "blue"> </font>EmpTableColumn.Visible = false;
<font color= "blue"></font>
<font color= "blue"> </font>TerritoryGrid.CurrentPageIndex = e.NewPageIndex;
<font color= "blue"> </font>BindList_Terr();
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "blue"> protected void</font> Page_EmpTerr(Object sender, DataGridPageChangedEventArgs e)
<font color= "blue"> </font>{
<font color= "green"> // Hide the 3rd table if it was visible</font>
<font color= "blue"> </font>EmpTableColumn.Visible = false;
<font color= "blue"></font>
<font color= "blue"> </font>EmployeeTerritoryGrid.CurrentPageIndex = e.NewPageIndex;
<font color= "blue"> </font>BindList_EmpTerr(ETHeader.Text);
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "blue"></font>
<font color= "green"> //****************</font>
<font color= "green"> // SORTING</font>
<font color= "green"> //****************</font>
<font color= "blue"></font>
<font color= "green"> //***********************************************************************</font>
<font color= "green"> //</font>
<font color= "green"> // The SortGridData methods sorts the array list bound to the datagrid </font>
<font color= "green"> // based on which sort field is being selected. This also handles reverse </font>
<font color= "green"> // sorting based on the boolean.</font>
<font color= "green"> //</font>
<font color= "green"> // This method is used for both the Territory Grid control and the </font>
<font color= "green"> // EmployeeTerritory Grid control.</font>
<font color= "green"> //</font>
<font color= "green"> //***********************************************************************</font>
<font color= "blue"> private void</font> SortGridData(HierarchicalReportCollection list, string sortField, bool asc)
<font color= "blue"> </font>{
<font color= "blue"> </font>HierarchicalReportCollection.HierarchicalReportFields sortCol = HierarchicalReportCollection.HierarchicalReportFields.InitValue;
<font color= "blue"></font>
<font color= "blue"> </font>switch(sortField)
<font color= "blue"> </font>{
<font color= "blue"> case</font> "Territory":
<font color= "blue"> </font>sortCol = HierarchicalReportCollection.HierarchicalReportFields.Territory;
<font color= "blue"> </font>break;
<font color= "blue"> case</font> "SalesTotals":
<font color= "blue"> </font>sortCol = HierarchicalReportCollection.HierarchicalReportFields.SalesTotals;
<font color= "blue"> </font>break;
<font color= "blue"> case</font> "EmployeeName":
<font color= "blue"> </font>sortCol = HierarchicalReportCollection.HierarchicalReportFields.EmployeeName;
<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"> // The SortGrid event handlers change the sortfield for the Territory </font>
<font color= "green"> // grid and the EmployeeTerritory grid, respectively, and re-binds them.</font>
<font color= "green"> //</font>
<font color= "green"> //*********************************************************************</font>
<font color= "blue"> protected void</font> SortGrid_Terr(Object sender, DataGridSortCommandEventArgs e)
<font color= "blue"> </font>{
<font color= "green"> // Hide the 2nd and 3rd table if they were visible</font>
<font color= "blue"> </font>ETTableColumn.Visible = false;
<font color= "blue"> </font>EmpTableColumn.Visible = false;
<font color= "blue"></font>
<font color= "green"> // change sort field</font>
<font color= "blue"> </font>SortField_Terr = (string)e.SortExpression;
<font color= "blue"></font>
<font color= "green"> // move back to the first page</font>
<font color= "blue"> </font>TerritoryGrid.CurrentPageIndex = 0;
<font color= "blue"></font>
<font color= "green"> // re-bind to display new sorting</font>
<font color= "blue"> </font>BindList_Terr();
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "blue"> protected void</font> SortGrid_EmpTerr(Object sender, DataGridSortCommandEventArgs e)
<font color= "blue"> </font>{
<font color= "green"> // Hide the 3rd table if it was visible</font>
<font color= "blue"> </font>EmpTableColumn.Visible = false;
<font color= "blue"> </font>
<font color= "green"> // change sort field</font>
<font color= "blue"> </font>SortField_EmpTerr = (string)e.SortExpression;
<font color= "blue"></font>
<font color= "green"> // move back to the first page</font>
<font color= "blue"> </font>EmployeeTerritoryGrid.CurrentPageIndex = 0;
<font color= "blue"></font>
<font color= "green"> // re-bind to display new sorting</font>
<font color= "blue"> </font>BindList_EmpTerr(ETHeader.Text);
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "blue"></font>
<font color= "green"> //****************</font>
<font color= "green"> // DRILLING DOWN</font>
<font color= "green"> //****************</font>
<font color= "blue"> protected void</font> TerritoryGrid_Click(Object sender, CommandEventArgs e)
<font color= "blue"> </font>{
<font color= "blue"> </font>string territoryName = e.CommandArgument.ToString().Trim();
<font color= "blue"></font>
<font color= "green"> // Put the second grid back to the first page</font>
<font color= "blue"> </font>EmployeeTerritoryGrid.CurrentPageIndex = 0;
<font color= "blue"></font>
<font color= "green"> // Show the table with the second grid, and update the headers</font>
<font color= "blue"> </font>ETTableColumn.Visible = true;
<font color= "blue"> </font>ETHeader.Text = territoryName;
<font color= "blue"> </font>BindList_EmpTerr(territoryName);
<font color= "blue"></font>
<font color= "green"> // Hide the 3rd table if it was visible</font>
<font color= "blue"> </font>EmpTableColumn.Visible = false;
<font color= "blue"> </font>}
<font color= "blue"> </font>
<font color= "blue"> protected void</font> ETGrid_Click(Object sender, CommandEventArgs e)
<font color= "blue"> </font>{
<font color= "blue"> </font>string[] cmdArgs = e.CommandArgument.ToString().Split(',');
<font color= "blue"></font>
<font color= "blue"> </font>string employeeName = cmdArgs[0];
<font color= "blue"> </font>int employeeID = Convert.ToInt32(cmdArgs[1]);
<font color= "blue"></font>
<font color= "green"> // Show the table with the 3rd grid, and update the headers</font>
<font color= "blue"> </font>EmpTableColumn.Visible = true;
<font color= "blue"> </font>EmployeeName.Text = employeeName;
<font color= "blue"> </font>BindList_Emp(employeeID);
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "green"> //****************</font>
<font color= "green"> // DESIGNER CODE</font>
<font color= "green"> //****************</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.ddlYear.SelectedIndexChanged += new System.EventHandler(this.ddlYear_SelectedIndexChanged);
<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= "blue"> private void</font> ddlYear_SelectedIndexChanged(object sender, System.EventArgs e)
<font color= "blue"> </font>{
<font color= "green"> // Put the first grid back to the first page</font>
<font color= "blue"> </font>TerritoryGrid.CurrentPageIndex = 0;
<font color= "blue"></font>
<font color= "green"> // Hide the 2nd and 3rd table if they were visible</font>
<font color= "blue"> </font>ETTableColumn.Visible = false;
<font color= "blue"> </font>EmpTableColumn.Visible = false;
<font color= "blue"> </font>
<font color= "blue"> </font>BindList_Terr();
<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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.