非常实用的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
    评论
ListView是ASP.NET中常用的数据绑定控件之一,它可以方便地将数据绑定到网页中,实现数据的展示和编辑。而DataPager是ASP.NET提供的数据分页控件,可以通过它来实现对数据的翻页操作。本文将介绍如何在ASP.NET中使用ListView和DataPager实现数据分页显示。 1.创建ListView控件 首先,在ASP.NET Web应用程序中创建一个新的Web窗体,然后从工具箱中拖拽ListView控件到窗体上。接着,右键单击ListView控件,选择“选择数据源”,在弹出的“数据源配置向导”中选择要绑定的数据源,这里以SQL Server数据库为例。 2.设置DataPager控件 接下来,在ListView控件下方添加一个DataPager控件,并将其与ListView控件关联。右键单击DataPager控件,选择“属性”,在“PagerTemplate属性中选择“NextPreviousPagerField”和“NumericPagerField”两个模板,这样就可以在DataPager中显示上一页、下一页和数字链接。 3.设置ListView控件分页属性 在ListView控件中启用分页功能,并设置每页显示的记录数。在ListView控件的属性中,将“EnablePaging”属性设置为True,然后将“PageSize”属性设置为每页显示的记录数,比如10条记录。 4.绑定数据 最后,在Page_Load事件中绑定数据,并将DataPager控件和ListView控件关联起来。代码如下: ``` protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindData(); } } private void BindData() { string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); string sql = "SELECT * FROM [Products]"; SqlCommand cmd = new SqlCommand(sql, conn); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adapter.Fill(ds, "Products"); PagedDataSource pds = new PagedDataSource(); pds.DataSource = ds.Tables["Products"].DefaultView; pds.AllowPaging = true; pds.PageSize = 10; int currentPageIndex = 0; if (Request.QueryString["page"] != null) { currentPageIndex = Convert.ToInt32(Request.QueryString["page"]) - 1; } pds.CurrentPageIndex = currentPageIndex; lblCurrentPage.Text = "当前页数:" + (currentPageIndex + 1).ToString() + " / " + pds.PageCount.ToString(); lnkbtnPrevious.Enabled = !pds.IsFirstPage; lnkbtnNext.Enabled = !pds.IsLastPage; lnkbtnFirst.Enabled = !pds.IsFirstPage; lnkbtnLast.Enabled = !pds.IsLastPage; lvProducts.DataSource = pds; lvProducts.DataBind(); } } ``` 在上述代码中,我们首先从数据库中查询数据,并将其填充到DataSet中。然后,我们使用PagedDataSource类来实现对数据的分页操作,并将其绑定到ListView控件中。在DataPager控件中,我们使用QueryStrings来实现分页链接的跳转操作。 至此,我们已经完成了使用ListView和DataPager实现数据分页显示的操作。运行程序,即可在网页上看到分页效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值