获取或设置 GridView 控件中页导航行的自定义内容。
命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)
语法:C#
public virtual ITemplate PagerTemplate { get ; set ; }
属性值
一个 System.Web.UI.ITemplate ,包含页导航行的自定义内容。默认值为 null,表示未设置此属性。
备注下面的代码示例演示如何创建一个允许用户使用 DropDownList 控件在 GridView 控件中进行导航的自定义页导航模板。
启用分页功能时(即 AllowPaging 属性设置为 true 时),GridView 控件中显示一个页导航行。该页导航行包含允许用户导航到该控件的不同页面的控件。可以不使用内置页导航行用户界面 (UI),而使用 PagerTemplate 属性定义您自己的用户界面。
注意 |
---|
设置 PagerTemplate 属性时,该属性重写内置页导航行用户界面。 |
若要为页导航行指定自定义模板,先要在 GridView 控件的开始标记和结束标记之间放置 <PagerTemplate> 标记。然后可以在开始和结束 <PagerTemplate> 标记之间列出模板的内容。若要控制页导航行的外观,请使用 PagerStyle 属性。
通常,将按钮控件添加到页导航模板以执行分页操作。单击 CommandName 属性设置为“Page”的按钮控件时,GridView 控件会执行分页操作。按钮的 CommandArgument 属性确定要执行的分页操作的类型。下表列出了 GridView 控件支持的命令参数值。
CommandArgument 值 | 说明 |
---|---|
“Next” | 导航至下一页。 |
“Prev” | 导航至上一页。 |
“First” | 导航至第一页。 |
“Last” | 导航至最后一页。 |
整数值 | 导航至指定页码。 |
示例
说明:演示如何创建一个允许用户使用 DropDownList 控件在 GridView 控件中进行导航的自定义页导航模板。
< script runat = " server " >
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;
}
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 >
< body >
< form 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 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 width = " 70% " 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 >