GridViewSortEventHandler Delegate
Assembly: System.Web (in system.web.dll)
'Declaration Public Delegate Sub GridViewSortEventHandler ( _ sender As Object, _ e As GridViewSortEventArgs _ ) 'Usage Dim instance As New GridViewSortEventHandler(AddressOf HandlerMethod)
/** @delegate */ public delegate void GridViewSortEventHandler ( Object sender, GridViewSortEventArgs e )
JScript supports the use of delegates, but not the declaration of new ones.
Parameters
- sender
The source of the event.
- e
A GridViewSortEventArgs object that contains the event data.
The Sorting event is raised when the hyperlink to sort a column is clicked, but before the GridView control handles the sort operation. This allows you to provide an event-handling method that performs a custom routine, such as canceling the sorting operation, whenever this event occurs.
When you create a GridViewSortEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event-handler delegates, see Events and Delegates.
The following example demonstrates how to programmatically add a GridViewSortEventHandler delegate to the Sorting event of a GridView control.
<%@ Page language="VB" %> <script runat="server"> Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) ' Create a new GridView object. Dim authorGridView As GridView = New GridView ' Set the GridView object's properties. authorGridView.ID = "AuthorGridView" authorGridView.DataSourceID = "AuthorsSqlDataSource" authorGridView.AutoGenerateColumns = True authorGridView.AllowSorting = True ' Programmatically register the event-handling methods. AddHandler authorGridView.Sorting, AddressOf AuthorsGridView_Sorting ' Add the GridView object to the Controls collection ' of the PlaceHolder control. GridViewPlaceHolder.Controls.Add(authorGridView) End Sub Sub AuthorsGridView_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs) ' Cancel the sorting operation if the user attempts to sort ' the Contract column. If e.SortExpression = "contract" Then e.Cancel = True Message.Text = "You cannot sort the Contract column." Else ' Clear the message label. Message.Text = "" End If End Sub </script> <html> <body> <form runat="server"> <h3>GridViewSortEventHandler Example</h3> <asp:label id="Message" forecolor="Red" runat="server"/> <br/> <asp:placeholder id="GridViewPlaceHolder" runat="Server"/> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Pubs sample database. --> <asp:sqldatasource id="AuthorsSqlDataSource" selectcommand="SELECT [au_id], [au_lname], [au_fname], [address], [city], [state], [zip], [contract] FROM [authors]" updatecommand="UPDATE authors SET au_lname=@au_lname, au_fname=@au_fname, address=@address, city=@city, state=@state, zip=@zip, contract=@contract WHERE (authors.au_id = @au_id)" connectionstring="server=localhost;database=pubs;integrated security=SSPI" runat="server"> </asp:sqldatasource> </form> </body> </html>
The following example demonstrates how to declaratively add a GridViewSortEventHandler delegate to the Sorting event of a GridView control.
<%@ Page language="VB" %> <script runat="server"> Sub CustomersGridView_Sorting(sender As Object, e As GridViewSortEventArgs) ' Cancel the sorting operation if the user attempts ' to sort by address. If e.SortExpression = "Address" Then e.Cancel = True Message.Text = "You cannot sort by address." SortInformationLabel.Text = "" Else Message.Text = "" End If End Sub Sub CustomersGridView_Sorted(ByVal sender As Object, ByVal e As EventArgs) ' Display the sort expression and sort direction. SortInformationLabel.Text = "Sorting by " & _ CustomersGridView.SortExpression.ToString() & _ " in " & CustomersGridView.SortDirection.ToString() & _ " order." End Sub </script> <html> <body> <form runat="server"> <h3>GridView Sorted and Sorting Example</h3> <asp:label id="Message" forecolor="Red" runat="server"/> <br/> <asp:label id="SortInformationLabel" forecolor="Navy" runat="server"/> <br/> <asp:gridview id="CustomersGridView" datasourceid="CustomersSource" autogeneratecolumns="true" allowpaging="true" emptydatatext="No data available." allowsorting="true" onsorting="CustomersGridView_Sorting" onsorted="CustomersGridView_Sorted" runat="server"> </asp:gridview> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the Web.config file. --> <asp:sqldatasource id="CustomersSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.