Click here to Skip to main content
15,891,248 members
Articles / Containers / Virtual Machine

ASP.NET Report Kit Grasshoper (Race to Linux)

Rate me:
Please Sign up or sign in to vote.
2.38/5 (5 votes)
2 Oct 20057 min read 38K   397   19  
Porting and deploying the report starter kit to Linux (RH7.3/Tomcat5.0.28/Grasshoper1.61)
<html>
	<head>
		<title>ASP.NET Reports Starter Kit Documentation</title><link href="style.css" type="text/css" rel="stylesheet"></head>
	<body class="NormalIndent">
		<h1>HierarchicalReport
		</h1>
		<p><b>Description:</b> &nbsp;&nbsp;&nbsp;The&nbsp;hierarchical report displays 3 
			different grids.&nbsp; The first grid displays sales totals, grouped by 
			territory, and filtered by year. The second grid displays sales totals grouped 
			by employee, within a&nbsp;specific territory - namely, the territory selected 
			in the first grid.&nbsp; The third grid shows&nbsp;information&nbsp;on a 
			particular employee - namely, the employee selected in the second grid.
		</p>
		<p><strong>Overview:</strong> &nbsp;&nbsp;&nbsp;&nbsp;Three DataGrids are set up on 
			this page, each with their own data source, retrieved from three separate 
			stored procedures.&nbsp; Only the data source for the first grid,&nbsp;"Sales 
			By Territory" is bound at Page Load and does not change; the other 2 grids are 
			hidden until the user selects the Territory, and then the Employee.&nbsp; When 
			the user chooses which Territory to view, the second grid, "Sales for&nbsp;[<em>Territory</em>]" 
			becomes visible, and is bound to the results of the stored procedure 
			"GetEmployeeSalesByTerritory", which accepts the Territory Name&nbsp;as a 
			parameter.&nbsp; Similarly, when the user chooses which Employee to view, the 
			third grid, "Employee Info for [<em>Employee</em> ]" becomes visible, and is 
			bound to the results of the stored procedure "GetEmployeeByID", which accepts 
			the Employee's ID as a parameter.&nbsp; Should the user click on the Territory 
			grid&nbsp;when the Employee grid&nbsp;is visible, the Employee grid will hide, 
			and the Employee Sales by Territory grid&nbsp;will refresh with the new data.
		</p>
		<p>Additionally, this report demonstrates how to implement interactive sorting and 
			paging&nbsp;using ASP.NET.
		</p>
		<p>
			Sorting is enabled for each column in the first two DataGrids. The sortable 
			columns can be sorted in ascending or descending order. Sortable columns are 
			displayed as red and underlined when the user hovers over the column header.
		</p>
		<p><img src="./images/1x1.gif" width="25"><img src="images/HierarchicalSorting.gif"></p>
		<p>Paging is also enabled&nbsp;in the first two DataGrids, so that a limited number 
			of records can be displayed on each page, with the ability to navigate through 
			the pages of data.&nbsp; Links to previous and next pages are 
			displayed&nbsp;just above the footer section of the DataGrids.&nbsp; In the 
			footer section, below the&nbsp;navigation links, a text field shows how many 
			pages total there are, as well as which page&nbsp;the user is on.&nbsp;</p>
		<p><img src="./images/1x1.gif" width="25"><img src="images/HierarchicalPaging.gif"> 
			&nbsp;
			<br>
		</p>
		<p><strong>Implementation Notes:&nbsp;</strong> &nbsp;&nbsp;
		</p>
		<p>In order for the user to be able to make a selection from one of the grids, and 
			to be able to pass that information to the codebehind, a Template column is 
			used as&nbsp;the first column in the top two DataGrids.&nbsp; A Template 
			column&nbsp;is used&nbsp;to create a column with a customized control layout, 
			including custom appearances for the heading section, the footer section, and 
			the items section of the column. In this example,&nbsp;a LinkButton&nbsp;within 
			the DataGrid Template column&nbsp;captures the user input - namely, the 
			Territory&nbsp;or Employee&nbsp;selected.</p>
		<pre>	&lt;<font color=blue>asp:templatecolumn</font> sortexpression="Territory" headertext="Territory Name"&gt;
	    &lt;itemtemplate&gt;
	        &lt;asp:linkbutton <font color=blue>oncommand="TerritoryGrid_Click" commandname="TerritoryDrill" 
				commandargument='&lt;%# DataBinder.Eval(Container, "DataItem.TerritoryDescription") %&gt;'</font> 
				cssclass="hier" runat="server" id="Territorylnk" 
				text='&lt;%# DataBinder.Eval(Container, "DataItem.TerritoryDescription") %&gt;'
	        &lt;/asp:linkbutton&gt;
	    &lt;/itemtemplate&gt;
	    &lt;itemstyle width="200px"&gt;&lt;/itemstyle&gt;
	&lt;/asp:templatecolumn&gt;
		</pre>
		<p>There are three main parts to the implementation of the LinkButton within the 
			Template column: the <em>OnCommand</em> attribute, the <em>CommandName</em> attribute, 
			and the <em>CommandArgument</em> attribute.&nbsp;The OnCommand attribute 
			specifies which method should be run when the LinkButton's text&nbsp;is 
			pressed.&nbsp; The CommandName and CommandArgument attributes hold data that 
			can be passed as parameters to the method.&nbsp; In the above code snippet, the 
			CommandArgument receives the value of the "TerritoryDescription" field of the 
			item selected by the user.&nbsp; The TerritoryGrid_Click method receives this 
			value and passes it to the BindList_EmpTerr method, which is a method that 
			binds the data for the second grid.</p>
		<pre>

    Protected Sub TerritoryGrid_Click(ByVal sender As [Object], ByVal e As CommandEventArgs)
        Dim territoryName As String = 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
    End Sub 'TerritoryGrid_Click
		</pre>
		<p>In the second grid, both the EmployeeID and the EmployeeName are passed into the 
			function ETGrid_Click.&nbsp; The EmployeeName field is used for display in the 
			third grid's Header section, and the EmployeeID field is used to pass in to the 
			BindList_Emp method, which is a method that binds the data for the third grid.</p>
		<pre>    &lt;asp:templatecolumn sortexpression="EmployeeName" headertext="Employee Name"&gt;
	&lt;itemtemplate&gt;
		&lt;asp:linkbutton oncommand="ETGrid_Click" 
				<font color="blue">commandargument='&lt;%# (String) DataBinder.Eval(Container, "DataItem.EmployeeName") + 
				"," + DataBinder.Eval(Container, "DataItem.EmployeeID").ToString() %&gt;'</font>
		cssclass="hier" runat="server" 
		id= "lnkEmployeeTerritory" text='&lt;%# 
		DataBinder.Eval(Container, "DataItem.EmployeeName") %&gt;'&gt; &lt;/asp:linkbutton&gt;
		&lt;/itemtemplate&gt;
	&lt;itemstyle
	width= "150px"&gt;&lt;/itemstyle&gt;
    &lt;/asp:templatecolumn&gt;
		</pre>
		<pre>
		
    Protected Sub ETGrid_Click(ByVal sender As [Object], ByVal e As CommandEventArgs)
        Dim cmdArgs As String() = e.CommandArgument.ToString().Split(","c)

        Dim emploName As String = cmdArgs(0)
        Dim employeeID As Integer = Convert.ToInt32(cmdArgs(1))

        ' Show the table with the 3rd grid, and update the headers
        EmpTableColumn.Visible = True
        EmployeeName.Text = emploName
        BindList_Emp(employeeID)
    End Sub 'ETGrid_Click
		</pre>
		<p>
			Binding the data to the top two&nbsp;DataGrids includes&nbsp;four 
			easy&nbsp;steps to account for paging and sorting&nbsp;- retrieving the data, 
			sorting the data, binding the data to the DataGrid's DataSource, and updating 
			the paging text labels.</p>
		<pre>
		
    Private Sub BindList_EmpTerr(ByVal territoryName As String)
        Dim empTerritoryList As HierarchicalReportCollection = HierarchicalReport.GetEmployeeSalesByTerritory(territoryName, Convert.ToInt32(ddlYear.SelectedItem.Value))

        ' do the sorting if there are data returned
        If empTerritoryList.Count > 0 Then
            SortGridData(empTerritoryList, SortField_EmpTerr, SortAscending_EmpTerr)

            EmployeeTerritoryGrid.DataSource = empTerritoryList
            EmployeeTerritoryGrid.DataBind()
        End If

        ' Update paging labels
        CurrPage_EmpTerr.Text = Convert.ToString((EmployeeTerritoryGrid.CurrentPageIndex + 1))
        TotPages_EmpTerr.Text = EmployeeTerritoryGrid.PageCount.ToString()
    End Sub 'BindList_EmpTerr
		</pre>
		<p>Sorting is a simple task to set up and&nbsp;implement with ASP.NET. The 
			DataGrid's AllowSorting property is set to True and an event handler for the 
			sorting is added to the DataGrid. The event handler then catches the sorting 
			criteria, where it is then used to sort the appropriate DataGrid's column.
		</p>
		<pre>   &lt;asp:datagrid id="TerritoryGrid" runat="server"  ... <FONT color=blue> AllowSorting="True" OnSortCommand="SortGrid_Terr"</FONT> ...&gt;
		</pre>
		<pre>		
    Private Sub SortGridData(ByVal list As HierarchicalReportCollection, ByVal sortField As String, ByVal asc As Boolean)
        Dim sortCol As HierarchicalReportCollection.HierarchicalReportFields = HierarchicalReportCollection.HierarchicalReportFields.InitValue

        Select Case sortField
            Case "Territory"
                sortCol = HierarchicalReportCollection.HierarchicalReportFields.Territory
            Case "SalesTotals"
                sortCol = HierarchicalReportCollection.HierarchicalReportFields.SalesTotals
            Case "EmployeeName"
                sortCol = HierarchicalReportCollection.HierarchicalReportFields.EmployeeName
        End Select

        list.Sort(sortCol, asc)
    End Sub 'SortGridData
		</pre>
		<p>The SortGridData method in the codebehind&nbsp;determines which field to sort 
			on, and passes that value to the Sort method of the 
			HierarchicalReportCollection object.&nbsp; The Sort method in the 
			HierarchicalReportCollection component calls the appropriate IComparer method, 
			which simply compares&nbsp;items in order to sort the list.
		</p>
		<pre>	
    Public Overloads Sub Sort(ByVal sortField As HierarchicalReportFields, ByVal isAscending As Boolean)
        Select Case sortField
            Case HierarchicalReportFields.Territory
                MyBase.Sort(New TerritoryNameComparer())
            Case HierarchicalReportFields.SalesTotals
                MyBase.Sort(New SalesTotalsComparer())
            Case HierarchicalReportFields.EmployeeName
                MyBase.Sort(New EmployeeNameComparer())
        End Select

        If Not isAscending Then
            MyBase.Reverse()
        End If
    End Sub 'Sort
		</pre>
		<p>
			Implementing ASP.NET paging is even simpler.&nbsp; The AllowPaging property is 
			set to True, a PageSize is set, and a short&nbsp;event handler is 
			defined.&nbsp; The only thing that typically has to be done in the 
			event&nbsp;handler is to set the DataGrid's CurrentPageIndex to the 
			DataGridPageChangedEventArgs.NewPageIndex, and to re-bind.&nbsp; Here, in 
			addition, the second and third table are hidden when the page is changed.&nbsp;</p>
		<pre>    &lt;asp:datagrid id="TerritoryGrid" runat="server" ... <font color=blue>AllowPaging="True" PageSize="5" OnPageIndexChanged="Page_Terr"</font> ... /&gt;
		</pre>
		<pre>		
    Protected Sub Page_Terr(ByVal sender As [Object], ByVal e As DataGridPageChangedEventArgs)
        ' Hide the 2nd and 3rd table if they were visible
        ETTableColumn.Visible = False
        EmpTableColumn.Visible = False

        TerritoryGrid.CurrentPageIndex = e.NewPageIndex
        BindList_Terr()
    End Sub 'Page_Terr
		</pre>
		<p>Finally,&nbsp;a style can be set for the Previous and Next text labels.</p>
		<pre>    &lt;pagerstyle CssClass="PagerHier" PrevPageText="&amp;lt; Prev" NextPageText="Next &amp;gt;" 
		Height="30px" VerticalAlign="middle"&gt;&lt;/pagerstyle&gt;
		</pre>
	</body>
</html>

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
Software Developer (Senior)
United States United States
I love to code! Working in C# is my passion, visit my github

Comments and Discussions