AspnetPager控件写的相当不错,不过作者没有提供一个标题排序的功能,我今天没事试用了一下,做了一个标题排序和大家一起分享
前台页面:
1 <div> 2 <asp:GridView ID="GridView1" runat="server" AllowSorting="True"> 3 </asp:GridView> 4 5 <webdiyer:AspNetPager ID="AspNetPager1" runat="server" horizontalalign="Center" width="100%" ShowPageIndexBox="Always" EnableUrlRewriting="true" UrlRewritePattern="./listpage_{0}.html" OnPageChanged="AspNetPager1_PageChanged" NumericButtonTextFormatString="-{0}-"> 6 </webdiyer:AspNetPager> 7 8 </div>
后台页面,(不喜欢VB.net的去在线转换一下)
Imports System.Data
Imports System.Data.SqlClient
Partial Public Class AspnetpagerTest
Inherits System.Web.UI.Page
Dim conn As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
conn = "data source =WANGBF\SQL2008EX;uid=sa;pwd=sa;database=AspNetPagerDB;pooling=true"
'Dim connection As SqlConnection = Nothing
'Dim ds As DataSet = SqlHelper.ExecuteDataset(conn, CommandType.Text, "SELECT * FROM userinfo")
'Me.GridView1.DataSource = ds.Tables(0).DefaultView
'Me.GridView1.DataBind()
If Not IsPostBack Then
'cache the number of total records to improve performance
Dim obj As Object = Cache(Convert.ToString([GetType]()) & "totalOrders")
If obj Is Nothing Then
Dim totalOrders As Integer = CInt(SqlHelper.ExecuteScalar(conn, "P_GetOrderNumber"))
Cache(Convert.ToString([GetType]()) & "totalOrders") = totalOrders
AspNetPager1.RecordCount = totalOrders
Else
AspNetPager1.RecordCount = CInt(obj)
End If
End If
End Sub
Protected Sub AspNetPager1_PageChanged(ByVal sender As Object, ByVal e As EventArgs) Handles AspNetPager1.PageChanged
BindGridView("CustomerID", "asc")
End Sub
Protected Sub GridView1_Sorted(ByVal sender As Object, ByVal e As EventArgs) Handles GridView1.Sorted
End Sub
Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
If String.IsNullOrEmpty(ViewState("sortdirection")) Then
ViewState("sortdirection") = SortDirection.Ascending.ToString()
Else
If ViewState("sortdirection") = SortDirection.Ascending.ToString() Then
ViewState("sortdirection") = SortDirection.Descending.ToString()
Else
ViewState("sortdirection") = SortDirection.Ascending.ToString()
End If
End If
Dim strOrderType As String
strOrderType = IIf(ViewState("sortdirection") = "Ascending", "desc", "asc")
BindGridView(e.SortExpression.ToString(), strOrderType)
End Sub
Public Sub BindGridView(ByVal strOrder As String, ByVal strOrderType As String)
Dim paras As SqlParameter() = New SqlParameter() { _
New SqlParameter("@tblName", "Customers"), _
New SqlParameter("@strFields", "*"), _
New SqlParameter("@strOrder", strOrder), _
New SqlParameter("@strOrderType", strOrderType), _
New SqlParameter("@PageSize", AspNetPager1.PageSize), _
New SqlParameter("@PageIndex", AspNetPager1.CurrentPageIndex), _
New SqlParameter("@strWhere", "")}
GridView1.DataSource = SqlHelper.ExecuteReader(conn, "proc_SplitPage", paras)
GridView1.DataBind()
End Sub
End Class
存储过程:
create procedure proc_SplitPage
(
@tblName varchar(255), -- 表名
@strFields varchar(1000) = '*', -- 需要返回的列,默认*
@strOrder varchar(255)='', -- 排序的字段名,必填
@strOrderType varchar(10)='ASC', -- 排序的方式,默认ASC
@PageSize int = 10, -- 页尺寸,默认10
@PageIndex int = 1, -- 页码,默认1
@strWhere varchar(1500) = '' -- 查询条件 (注意: 不要加 where
)
AS
declare @strSQL varchar(5000)
if @strWhere !=''
set @strWhere=' where '+@strWhere
set @strSQL= 'SELECT * FROM ('+ 'SELECT ROW_NUMBER() OVER (ORDER BY '+@strOrder+' '+@strOrderType+') AS pos,'+@strFields+' '+ 'FROM '+@tblName+' '+@strWhere+ ') AS sp WHERE pos BETWEEN '+str((@PageIndex-1)*@PageSize+1)+' AND '+str(@PageIndex*@PageSize)
exec (@strSQL)