ASP.NET 2.0 的 GridView內建分頁功能,預設是以「數字+符號」呈現,例如「
<< ... 1 2 3 4 5 6 7 8 9 10
... >>」,碰到客戶說看不懂符號的意思,只好想辦法把它改成中文字,變成「
第一頁 上十頁 1 2 3 4 5 6 7 8 9 10
下十頁 最後頁」。
貼上程式碼如下,請參閱。使用Northwind的Customers資料表。
=======================
GridView_Pager_Number.aspx
=======================
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridView_Pager_Number.aspx.cs" Inherits="GridView_Pager_Number" %>
<!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">
<p>
<br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None" onrowdatabound="GridView1_RowDataBound" PageSize="4">
<PagerSettings Mode="NumericFirstLast" />
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True"
SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName"
SortExpression="ContactName" />
<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle"
SortExpression="ContactTitle" />
<asp:BoundField DataField="Address" HeaderText="Address"
SortExpression="Address" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Region" HeaderText="Region"
SortExpression="Region" />
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode"
SortExpression="PostalCode" />
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
<asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT * FROM [Customers]"></asp:SqlDataSource>
</p>
<div>
</div>
</form>
</body>
</html>
=======================
GridView_Pager_Number.aspx.cs
=======================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class GridView_Pager_Number : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//取得Pager Row
if (e.Row.RowType == DataControlRowType.Pager)
{
//取得內崁的TableRow
TableRow trPager = (TableRow)e.Row.Controls[0].Controls[0].Controls[0];
for (int i = 0; i < trPager.Controls.Count; i++)
{
TableCell tcPager = (TableCell)trPager.Controls[i];
for (int j = 0; j < tcPager.Controls.Count; j++)
{
//...格式必為LinkButton
if (tcPager.Controls[j].ToString() == "System.Web.UI.WebControls.DataControlPagerLinkButton")
{
//原Number顯示方式為"... 11 12 13 14 15 16 17 18 19 20 ..."
//故 (i=0 && LinkButton.Text="..."), 即目前頁數>10
LinkButton lbtn = (LinkButton)tcPager.Controls[j];
if ((lbtn.Text == "<<") && (i == 0))
{
lbtn.Text = "第一頁 ";
}
else if ((lbtn.Text == "...") && (i == 0))
{
lbtn.Text = "上十頁 ";
}
else if (lbtn.Text == "...")
{
lbtn.Text = " 下十頁";
}
else if (lbtn.Text == ">>")
{
lbtn.Text = " 最後頁";
}
}
}
}
}
}
}
=======================
---
Welly