Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


The Baker's Dozen: 13 Steps for Building an ASP.NET Database Lookup Page : Page 4

Build a database search Web page using new features in ASP.NET 2.0, SQL Server 2005, and .NET generics in C#.

Tip 8: Handling Quick-lookup Navigation and Paging
The Load event of the page populates the dropdown list of one-character values that the user can select for quick navigation:

string[] alphabet = new string[] { " ", "A", "B", "C",..., "0", "1", "2", "3"...; for (int i = 0; i < alphabet.Length; i++) this.cboAlphaIndex.Items.Add(alphabet[i].Trim());

The code also has four methods for navigation, corresponding to the Click events for the four navigation buttons. These methods set the StartRowIndex accordingly, based on the navigation direction.

private void NavBegin() { // set the startrowindex to zero, and make sure // we're not specifying a letter Session["startRowIndex"] = 0; // set alpha index pulldown back to nothing this.cboAlphaIndex.SelectedIndex = 0; this.GetData(); } private void NavPrevious() { // set the startrowindex to the row number for the // first record in the current page, minus 1, and // minus maxrows // so if we're looking at rows 200-249, and we go // back one page, the new start row index would be // 200-1-50, or 149....and we'd get back 149-199 Session["startRowIndex"] = (int)Session["CurrentFirstRow"] - (int)Session["MaxRows"]; this.cboAlphaIndex.SelectedIndex = 0; this.GetData(); } private void NavNext() { // startrow index becomes the value of the last // row [the stored proc does a 'greater than'] Session["startRowIndex"] = (int)Session["CurrentLastRow"] + 1; this.cboAlphaIndex.SelectedIndex = 0; this.GetData(); } private void NavEnd() { // -1 is the 'magic number', it tells the stored // proc to just grab everything from // rowcount-maxrows, to rowcount Session["startRowIndex"] = -1; this.cboAlphaIndex.SelectedIndex = 0; this.GetData(); }

Tip 9: Handling Sorting on Any Column
To sort on a column, you can tap into the Sorting event, which exposes the SortExpression for the column heading that the user selected. If you have not explicitly set the SortExpression programmatically, the SortExpression will be the name of the data column.

protected void grdResults_Sorting (object sender, GridViewSortEventArgs e) { Session["SortCol"] = e.SortExpression.ToString().Trim(); this.lblAlphaNav.Text = e.SortExpression.ToString().Trim() + " starting with..."; this.GetData(); }

Tip 10: Displaying Grid Result Set Information
Once you call GetData to return the result set, you can pass the result set to SetInfo. This method will first strip out the tally row, which represents the total number of rows that met the filter criteria, as follows:

using daCustomer; private void SetInfo(dsCustomer odsCustomer) { DataRow[] aRows = odsCustomer.dtCustomer.Select ("customerid = -1"); int nTotalCount = 0; if( aRows.Length> 0) { dsCustomer.dtCustomerRow oRow = (dsCustomer.dtCustomerRow)aRows[0]; nTotalCount = oRow.RowNum; oRow.Delete(); odsCustomer.dtCustomer.AcceptChanges(); } ...

Then SetInfo will determine the actual number of rows to be displayed. In most but not all instances, this will be the same as MaxRows. If the number is at least one, the method will read the RowNum column of the first and last row of the result set, and assign those values to the session variables CurrentFirstRow and CurrentLastRow.

int nResultCount = odsCustomer.dtCustomer.Rows.Count; if (nResultCount > 0) { Session["CurrentFirstRow"] = odsCustomer.dtCustomer[0].RowNum; Session["CurrentLastRow"] = odsCustomer.dtCustomer [nResultCount - 1].RowNum; } this.grdResults.Caption = "Number of matching records: " + nTotalCount.ToString().Trim() + "...click on any column heading to sort";

Tip 11: Setting up Columns to Link to Another Page
A common beginner ASP.NET question is how to set up a link for each row in a GridView—additionally, how to add a select icon for each row, like the ones shown in Figures 1, 2, and 3.

Just follow three basic steps to implement this. First, you need to identify a key field from the list of columns bound to the GridView. You can do this by setting the DataKeyNames property of the GridView. The key field (or combination of fields) must represent a unique value for each row. The example project uses CustomerID as the unique identifier, so a developer can define the GridView's unique identifier as follows:

// set the DataKeyNames property // to uniquely determine a selected row this.grdResults.DataKeyNames = new string[] { "CustomerID" };

Second, you can add an icon column by creating a ButtonField object, setting some properties, and adding the object to the GridView.

// Insert a button field to the GridView // so that the user can select a row by clicking // on the button icon ButtonField obt = new ButtonField(); obt.CommandName = "Select"; obt.ButtonType = ButtonType.Image; obt.ImageUrl = "openfolder.ico"; this.grdResults.Columns.Clear(); this.grdResults.Columns.Add(obt);

Finally, you can tap into the GridView's SelectedIndexChanged event, which will fire when the user clicks on the icon. In the event, you can cast the value of the SelectedDataKey property to the valid type for the key. In this example, the code grabs the value of the current customer ID and sets up a call to another Web page that includes a query string with the ID.

protected void grdResults_SelectedIndexChanged (object sender, EventArgs e) { // this reads from the DataKeyNames property int nCustomerID = (int)this.grdResults. SelectedDataKey.Values[0]; Response.Redirect("CustomerPage.aspx?CUSTID=" + nCustomerID.ToString().Trim()); }

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.