alt
Advertisement
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow ASP.NET 2.0 arrow ASP.NET 2.0 : Adding Sorting and Paging in GridView
Site Search


ASP.NET 2.0 : Adding Sorting and Paging in GridView
Article Index
ASP.NET 2.0 : Adding Sorting and Paging in GridView
Page 2
Page 3

.

.

.

To add an event handler for the Sorting event

1. If you are working with a single-file page, switch to Source view and then add the following directives to the top of the code page:

< % @ Import Namespace="System.Data" % >
< % @ Import Namespace="System.Data.SqlClient" % >

2. Importing the namespaces will make it easier to write the code needed.

3. If the application is using a code-behind page, switch to the code-behind page Default.aspx.cs and add the following lines to the top of the code file, outside the class declaration:

using System.Data;
using System.Data.SqlClient;

4. In the Default.aspx page, switch to Design view, right-click the DropDownList control, and then click Properties.

5. In the Properties window toolbar, click the Events icon, and then double-click SelectedIndexChanged to create a skeleton event handler.

6. Insert the following highlighted code into the event handler.

Click here to view sample code

This code intercepts the SelectedIndexChanged event for DropDownList1 to specify the second sort parameter based on the value selected. Then, it retrieves the sortorder from the Session state and adds the " ASC" or " DESC" to the Sort property for the DataView. Finally, it rebinds the GridView control to the data.

7. Switch to the Default.aspx page and select the DropDownList control.

8. In the Properties window toolbar, click the Events icon, and then double-click Load to create a skeleton event handler.

9. Insert the following highlighted code into the Page_ Load event handler.

Click here to view sample code

10. This code handles the Page Load event and retrieves the ConnectionString connection string from the Web.config file to execute a SELECT statement using a sqlDataAdapter control. Then, it fills a dataset with the data returned, selects the first table of the dataset as a DataView object, for sort operations, and then saves the DataView object in the Session state such that subsequent sorts do not require a round trip to the database. Finally, it sets the default sort order to Ascending in Session state.

11. Switch to Design view.

12. Right-click the GridView1 control, click Properties, and then set DataSourceID to (None) to remove it.

13. Because you are setting the data source in code, the GridView control cannot also have a DataSourceID property setting.

14. When prompted to refresh the fields and keys for the GridView control, click No.

15. Although you are changing the data source for the GridView control (by removing it), you want to retain the existing field bindings.

16. Right-click the GridView1 control, and then click Properties.

17. In Properties, click Events, and then double-click Sorting to create a skeleton event handler.

18. Add the following highlighted code to the Sorting event handler:

Click here to view sample code

 

 

 

 

This code handles the sorting event of the GridView control to customize the sort functionality of the GridView control. It uses the sorting functionality of the underlying DataView control. It retrieves the DataView control saved in the Session state. Then, it retrieves the sort order value from Session state and adds " ASC" or " DESC" to theSortExpression property passed to the event handler. It saves the new sort order and the SortExpression value in the Session state. Finally, it displays the SortExpression value in a Label control and rebinds the GridView control to the data. The page can now be tested.



 
< Prev   Next >
Sponsored Links
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape