非常实用的GridView.PagerTemplate 属性

获取或设置 GridView 控件中页导航行的自定义内容。

命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)

语法:C#
[TemplateContainerAttribute( typeof (GridViewRow))] 
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 控件中进行导航的自定义页导航模板。

<% @ Page language = " C# "   %>

< 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 >
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
GridView 是一个常用的控件,用于在网页上展示数据。当数据量较大时,我们通常需要对 GridView 进行分页,以便用户能够方便地浏览数据。下面是 GridView 分页的使用方法: 1. 在 .aspx 页面上添加 GridView 控件,并设置 AllowPaging 属性为 True。 ```html <asp:GridView ID="GridView1" runat="server" AllowPaging="True"></asp:GridView> ``` 2. 在 .aspx.cs 页面中编写代码,设置 GridView 的数据源并绑定数据。同时,设置 GridViewPageSize 属性为每页显示的数据量。最后,调用 DataBind() 方法绑定数据。 ```c# protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // 设置数据源 DataTable dt = GetData(); // 设置每页显示的数据量 GridView1.PageSize = 10; // 绑定数据 GridView1.DataSource = dt; GridView1.DataBind(); } } private DataTable GetData() { // 从数据库中获取数据 // ... // 返回数据表 return dt; } ``` 3. 在 .aspx 页面上添加 PagerTemplate,用于显示分页按钮。 ```html <asp:GridView ID="GridView1" runat="server" AllowPaging="True"> <PagerTemplate> <asp:LinkButton ID="lnkPrev" runat="server" CommandName="Page" CommandArgument="Prev">上一页</asp:LinkButton> <% for (int i = 0; i < GridView1.PageCount; i++) { %> <asp:LinkButton ID="lnkPage" runat="server" CommandName="Page" CommandArgument="<%# i %>" Text="<%# i + 1 %>"></asp:LinkButton> <% } %> <asp:LinkButton ID="lnkNext" runat="server" CommandName="Page" CommandArgument="Next">下一页</asp:LinkButton> </PagerTemplate> </asp:GridView> ``` 4. 在 .aspx.cs 页面中编写代码,处理分页按钮的事件。 ```c# protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Page") { // 获取当前页码 int pageIndex = GridView1.PageIndex; if (e.CommandArgument.ToString() == "Prev") { // 上一页 if (pageIndex > 0) { pageIndex--; } } else if (e.CommandArgument.ToString() == "Next") { // 下一页 if (pageIndex < GridView1.PageCount - 1) { pageIndex++; } } else { // 其他页码 pageIndex = Convert.ToInt32(e.CommandArgument); } // 设置当前页码 GridView1.PageIndex = pageIndex; // 重新绑定数据 GridView1.DataSource = GetData(); GridView1.DataBind(); } } ``` 通过以上步骤,就可以实现 GridView 的分页功能了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值