GridViewSortEventHandler 委托
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
public delegate void GridViewSortEventHandler(System::Object ^ sender, GridViewSortEventArgs ^ e);
public delegate void GridViewSortEventHandler(object sender, GridViewSortEventArgs e);
type GridViewSortEventHandler = delegate of obj * GridViewSortEventArgs -> unit
Public Delegate Sub GridViewSortEventHandler(sender As Object, e As GridViewSortEventArgs)
参数
- sender
- Object
事件源。
包含事件数据的 GridViewSortEventArgs 对象。
示例
下面的示例演示如何以编程方式向 控件的 GridView 事件添加GridViewSortEventHandler委托Sorting。
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
// Create a new GridView object.
GridView authorGridView = 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.
authorGridView.Sorting += new GridViewSortEventHandler(this.AuthorsGridView_Sorting);
// Add the GridView object to the Controls collection
// of the PlaceHolder control.
GridViewPlaceHolder.Controls.Add(authorGridView);
}
void AuthorsGridView_Sorting(Object sender, GridViewSortEventArgs e)
{
// Cancel the sorting operation if the user attempts to sort
// the Contract column.
if(e.SortExpression == "contract")
{
e.Cancel = true;
Message.Text = "You cannot sort the Contract column.";
}
else
{
// Clear the message label.
Message.Text = "";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridViewSortEventHandler Example</title>
</head>
<body>
<form id="form1" 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>
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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 xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridViewSortEventHandler Example</title>
</head>
<body>
<form id="form1" 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>
以下示例演示如何以声明方式向 控件的 GridView 事件添加GridViewSortEventHandler委托Sorting。
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void CustomersGridView_Sorting(Object sender, GridViewSortEventArgs e)
{
// Cancel the sorting operation if the user attempts
// to sort by address.
if (e.SortExpression == "Address")
{
e.Cancel = true;
Message.Text = "You cannot sort by address.";
SortInformationLabel.Text = "";
}
else
{
Message.Text = "";
}
}
void CustomersGridView_Sorted(Object sender, EventArgs e)
{
// Display the sort expression and sort direction.
SortInformationLabel.Text = "Sorting by " +
CustomersGridView.SortExpression.ToString() +
" in " + CustomersGridView.SortDirection.ToString() +
" order.";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView Sorting Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView 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>
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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 xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView Sorted and Sorting Example</title>
</head>
<body>
<form id="form1" 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>
注解
当 Sorting 单击用于对列进行排序的超链接时,但在控件处理排序操作之前 GridView ,将引发 该事件。 这允许你提供一个事件处理方法,该方法执行自定义例程,例如,每当发生此事件时取消排序操作。
创建 GridViewSortEventHandler 委托时,需要标识将处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 处理和引发事件。
扩展方法
GetMethodInfo(Delegate) |
获取指示指定委托表示的方法的对象。 |