GridView.PagerTemplate 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置 GridView 控件中页导航行的自定义内容。
public:
virtual property System::Web::UI::ITemplate ^ PagerTemplate { System::Web::UI::ITemplate ^ get(); void set(System::Web::UI::ITemplate ^ value); };
[System.ComponentModel.Browsable(false)]
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
[System.Web.UI.TemplateContainer(typeof(System.Web.UI.WebControls.GridViewRow))]
public virtual System.Web.UI.ITemplate PagerTemplate { get; set; }
[<System.ComponentModel.Browsable(false)>]
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
[<System.Web.UI.TemplateContainer(typeof(System.Web.UI.WebControls.GridViewRow))>]
member this.PagerTemplate : System.Web.UI.ITemplate with get, set
Public Overridable Property PagerTemplate As ITemplate
属性值
一个 ITemplate,包含页导航行的自定义内容。 默认值为 null,表示未设置此属性。
- 属性
示例
以下示例演示如何创建自定义页导航模板,该模板允许用户使用 控件在控件中GridViewDropDownList导航。
<%@ 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">
protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
{
// Retrieve the pager row.
GridViewRow pagerRow = CustomersGridView.BottomPagerRow;
// Retrieve the PageDropDownList DropDownList from the bottom pager row.
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
// Set the PageIndex property to display that page selected by the user.
CustomersGridView.PageIndex = pageList.SelectedIndex;
}
protected void CustomersGridView_DataBound(Object sender, EventArgs e)
{
// Retrieve the pager row.
GridViewRow pagerRow = CustomersGridView.BottomPagerRow;
// Retrieve the DropDownList and Label controls from the row.
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
if(pageList != null)
{
// Create the values for the DropDownList control based on
// the total number of pages required to display the data
// source.
for(int i=0; i<CustomersGridView.PageCount; i++)
{
// Create a ListItem object to represent a page.
int pageNumber = i + 1;
ListItem item = new ListItem(pageNumber.ToString());
// If the ListItem object matches the currently selected
// page, flag the ListItem object as being selected. Because
// the DropDownList control is recreated each time the pager
// row gets created, this will persist the selected item in
// the DropDownList control.
if(i==CustomersGridView.PageIndex)
{
item.Selected = true;
}
// Add the ListItem object to the Items collection of the
// DropDownList.
pageList.Items.Add(item);
}
}
if(pageLabel != null)
{
// Calculate the current page number.
int currentPage = CustomersGridView.PageIndex + 1;
// Update the Label control with the current page information.
pageLabel.Text = "Page " + currentPage.ToString() +
" of " + CustomersGridView.PageCount.ToString();
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView PagerTemplate Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView PagerTemplate Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
allowpaging="true"
ondatabound="CustomersGridView_DataBound"
runat="server">
<pagerstyle forecolor="Blue"
backcolor="LightBlue"/>
<pagertemplate>
<table width="100%">
<tr>
<td style="width:70%">
<asp:label id="MessageLabel"
forecolor="Blue"
text="Select a page:"
runat="server"/>
<asp:dropdownlist id="PageDropDownList"
autopostback="true"
onselectedindexchanged="PageDropDownList_SelectedIndexChanged"
runat="server"/>
</td>
<td style="width:70%; text-align:right">
<asp:label id="CurrentPageLabel"
forecolor="Blue"
runat="server"/>
</td>
</tr>
</table>
</pagertemplate>
</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="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
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">
Protected Sub PageDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
' Retrieve the pager row.
Dim pagerRow As GridViewRow = CustomersGridView.BottomPagerRow
' Retrieve the PageDropDownList DropDownList from the bottom pager row.
Dim pageList As DropDownList = CType(pagerRow.Cells(0).FindControl("PageDropDownList"), DropDownList)
' Set the PageIndex property to display that page selected by the user.
CustomersGridView.PageIndex = pageList.SelectedIndex
End Sub
Protected Sub CustomersGridView_DataBound(ByVal sender As Object, ByVal e As EventArgs)
' Retrieve the pager row.
Dim pagerRow As GridViewRow = CustomersGridView.BottomPagerRow
' Retrieve the DropDownList and Label controls from the row.
Dim pageList As DropDownList = CType(pagerRow.Cells(0).FindControl("PageDropDownList"), DropDownList)
Dim pageLabel As Label = CType(pagerRow.Cells(0).FindControl("CurrentPageLabel"), Label)
If Not pageList Is Nothing Then
' Create the values for the DropDownList control based on
' the total number of pages required to display the data
' source.
Dim i As Integer
For i = 0 To CustomersGridView.PageCount - 1
' Create a ListItem object to represent a page.
Dim pageNumber As Integer = i + 1
Dim item As ListItem = New ListItem(pageNumber.ToString())
' If the ListItem object matches the currently selected
' page, flag the ListItem object as being selected. Because
' the DropDownList control is recreated each time the pager
' row gets created, this will persist the selected item in
' the DropDownList control.
If i = CustomersGridView.PageIndex Then
item.Selected = True
End If
' Add the ListItem object to the Items collection of the
' DropDownList.
pageList.Items.Add(item)
Next i
End If
If Not pageLabel Is Nothing Then
' Calculate the current page number.
Dim currentPage As Integer = CustomersGridView.PageIndex + 1
' Update the Label control with the current page information.
pageLabel.Text = "Page " & currentPage.ToString() & _
" of " & CustomersGridView.PageCount.ToString()
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView PagerTemplate Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView PagerTemplate Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
allowpaging="true"
ondatabound="CustomersGridView_DataBound"
runat="server">
<pagerstyle forecolor="Blue"
backcolor="LightBlue"/>
<pagertemplate>
<table width="100%">
<tr>
<td style="width:70%">
<asp:label id="MessageLabel"
forecolor="Blue"
text="Select a page:"
runat="server"/>
<asp:dropdownlist id="PageDropDownList"
autopostback="true"
onselectedindexchanged="PageDropDownList_SelectedIndexChanged"
runat="server"/>
</td>
<td style="width:70%; text-align:right">
<asp:label id="CurrentPageLabel"
forecolor="Blue"
runat="server"/>
</td>
</tr>
</table>
</pagertemplate>
</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="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
注解
当启用分页功能 (属性设置为 true) 时AllowPaging,控件中GridView将显示页导航行。 页导航行包含允许用户导航到控件中不同页面的控件。 可以使用 属性定义自己的 UI PagerTemplate ,而不是使用内置页导航行用户界面 (UI) 。
注意
PagerTemplate设置 属性后,它会替代内置的页导航行 UI。
若要为寻呼行指定自定义模板,请首先将标记放在 <PagerTemplate>
控件的 GridView 开始标记和结束标记之间。 然后,可以在开始标记和结束 <PagerTemplate>
标记之间列出模板的内容。 若要控制寻呼行的外观,请使用 PagerStyle 属性。
通常,按钮控件会添加到页导航模板以执行分页操作。 单击属性设置为“Page”的按钮控件CommandName
时,控件GridView将执行分页操作。 按钮的 CommandArgument
属性确定要执行的分页操作的类型。 下表列出了 控件支持的 GridView 命令参数值。
CommandArgument 值 |
说明 |
---|---|
“Next” | 导航到下一页。 |
“Prev” | 导航到上一页。 |
“First” | 导航到第一页。 |
“Last” | 导航到最后一页。 |
整数值 | 导航到指定的页码。 |