获取或设置一个值,该值指示客户端回调是否用于排序和分页操作。
true 表示客户端回调用于排序和分页操作;否则为 false。默认值为 false。
?
?
异常类型 条件SupportsCallback
当 Columns 集合包含不支持回调的列(如 TemplateField)时,此属性设置为 true。
?
?
默认情况下,在执行排序或分页操作时,GridView 控件回发至服务器以执行该操作。当 EnableSortingAndPagingCallbacks 属性设置为 true 时,会在客户端上调用服务以执行排序和分页操作,从而无需回发给服务器。
?
然而,并不是所有的浏览器都支持此功能。若要确定浏览器是否支持此功能,请使用 SupportsCallback 属性。
?
若要此功能工作,Columns 集合中的所有列都必须支持回调。如果 Columns 集合包含某个不支持回调的列(如 TemplateField),则将引发 NotSupportedException 异常。
?
示例:
?
VB:
<%@ Page language="VB" %>
<html>
? <body>
??? <form id="Form1" runat="server">
???????
????? <h3>GridView EnableSortingAndPagingCallbacks Example</h3>
????? <asp:gridview id="CustomersGridView"
??????? datasourceid="CustomersSource"
??????? emptydatatext="No data available."
??????? allowpaging="True"
??????? allowsorting="True"
??????? enablesortingandpagingcallbacks="True"???????
??????? runat="server">
????? <%--allowsorting设为true,enablesortingandpagingcallbacks才起作用--%>??????
????? </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:ConnectionString %>"
??????? runat="server"/>
???????
??? </form>
? </body>
</html>
C#:
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
??? "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
? <head runat="server">
??? <title>GridView EnableSortingAndPagingCallbacks Example</title>
</head>
<body>
??? <form id="form1" runat="server">
???????
????? <h3>GridView EnableSortingAndPagingCallbacks Example</h3>
????? <asp:gridview id="CustomersGridView"
??????? datasourceid="CustomersSource"
??????? autogeneratecolumns="true"
??????? emptydatatext="No data available."
??????? allowpaging="true"
??????? allowsorting="true"
??????? enablesortingandpagingcallbacks="true"???????
??????? 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>
?
?
?
VB:
NoDataSource.aspx:
?
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="NoDataSource.aspx.vb" Inherits="GridView20070201.NoDataSource" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
??? <title>無題のページ</title>
</head>
<body>
??? <form id="form1" runat="server">
??? <div>
??????? <asp:GridView ID="GridView1" runat="server"
??????????? OnPageIndexChanging="GridView1_PageIndexChanging">
??????? </asp:GridView>
???? <%--加了OnPageIndexChanging="GridView1_PageIndexChanging",则要求后台Private Sub GridView1_PageIndexChanging...--%>
???? <%--必须改为Protected Sub GridView1_PageIndexChanging...,否则出错--%>
??????? <asp:Button ID="Button4" runat="server" Text="前一页" />
??????? <asp:Button ID="Button3" runat="server" Text="后一页" />
??????? <asp:Button ID="Button2" runat="server" Text="最后一页" />
??????? <asp:Button ID="Button1" runat="server" Text="第一页" />
??? </div>
??? </form>
</body>
</html>
?
NoDataSource.aspx.vb:
?
Imports System.Data.SqlClient
Imports System.Data
Partial Public Class NoDataSource
??? Inherits System.Web.UI.Page
??? Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
??????? If Not IsPostBack Then
??????????? GridViewDataBind()
??????? End If
??? End Sub
??? Public Sub GridViewDataBind()
??????? Button3.Enabled = True
??????? Button4.Enabled = True
??????? Dim conn As SqlConnection
??????? Dim sda As SqlDataAdapter
??????? Dim ds As DataSet
??????? conn = New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ToString())
??????? Try
??????????? conn.Open()
??????????? sda = New SqlDataAdapter("select * from customers where 1=1", conn)
??????????? 'sda = New SqlDataAdapter("select * from customers where 1=2", conn)
??????????? ds = New DataSet()
??????????? sda.Fill(ds, "customers")
??????????? '没有数据时,增加一个有标题的空行
??????????? If ds.Tables(0).Rows.Count = 0 Then
??????????????? AddDummyData(ds)
??????????? End If
??????????? GridView1.DataSource = ds.Tables(0)
??????????? GridView1.PageSize = 5
??????????? 'GridView1.AllowPaging = True,在指定了GridView1.EnableSortingAndPagingCallbacks = false时,没有处理GridView的PageIndexChanging事件时,报错
??????????? GridView1.AllowPaging = True
??????????? 'GridView1.AllowSorting = True,在指定了GridView1.EnableSortingAndPagingCallbacks = false时,没有处理GridView的Sorting事件时,报错
??????????? GridView1.AllowSorting = True
??????????? 'EnableSortingAndPagingCallbacks设为true后,就可以进行异步处理了,
??????????? '但是数据源是在后台绑定的, 所以点击字段名的排序链接进行异步排序时, 前台找不到数据源, 所以不能排序,点击导航异步换页时显示的是空数据
??????????? '如果是在前台绑定的数据源,如sqlDataSource,则没有上面的问题,例子程序EnableSortingAndPagingCallbacks(如上)
??????????? 'GridView1.EnableSortingAndPagingCallbacks = True
??????????? GridView1.DataBind()
??????????? conn.Close()
??????????? If GridView1.PageIndex = 0 Then
??????????????? Button4.Enabled = False
??????????? End If
??????????? If GridView1.PageIndex = GridView1.PageCount - 1 Then
??????????????? Button3.Enabled = False
??????????? End If
??????? Catch ex As Exception
??????? Finally
??????????? conn.Close()
??????? End Try
??? End Sub
??? Protected Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
??????? GridView1.PageIndex = GridView1.PageIndex - 1
??????? GridViewDataBind()
??? End Sub
??? Protected Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
??????? GridView1.PageIndex = GridView1.PageIndex + 1
??????? GridViewDataBind()
??? End Sub
??? Protected Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
??????? GridView1.PageIndex = GridView1.PageCount - 1
??????? GridViewDataBind()
??? End Sub
??? Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
??????? GridView1.PageIndex = 0
??????? GridViewDataBind()
??? End Sub
??? Private Sub AddDummyData(ByVal ds As DataSet)
??????? Dim dt As New DataTable
??????? Dim dr As DataRow
??????? '先取得DataTable后,才能new一个同格式的新行,否则dr为空
??????? dt = ds.Tables(0)
??????? dr = dt.NewRow()
??????? dt.Rows.Add(dr)
??? End Sub
??? Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
??????? GridView1.PageIndex = e.NewPageIndex
??????? GridViewDataBind()
??? End Sub
??? Protected Sub GridView1_Sorting(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
??? End Sub
End Class