您现在的位置是:网站首页> 编程资料编程资料

解析asp.net的分页控件_实用技巧_

2023-05-24 342人已围观

简介 解析asp.net的分页控件_实用技巧_

一、说明

  AspNetPager.dll这个分页控件主要用于asp.net webform网站,现将整理代码如下

二、代码

1、首先在测试页面Default.aspx页面添加引用

<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>

2、写一个Repeater列表控件用于显示数据

        
  •       <%# Eval("time") %>       "><%# Access.GetStringNum( Eval("name").ToString(),15) %>     
  •   

    3、添加

    PageSize属性是用于设置每页显示的数量

    4、后台代码绑定

     //测试数据源
    private void ShowNews() {   String strSql = String.Format("select * from News order by time asc");   DataTable dtbl = Access.ExecuteDataTable(strSql, null);   this.rptNews.DataSource = Access.GetPageDataSource(AspNetPager1, AspNetPager1.CurrentPageIndex - 1, dtbl);   this.rptNews.DataBind(); }

    Access是测试数据库访问类,在最后的Demo中提供给大家

    5、分页控件点击页码事件

     //分页 protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e) {   this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;   ShowNews(); } 
     

    最后奉上整页代码:

    Default.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %><%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>asp.net分页控件

    Default.aspx.cs

     using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ShowNews(); } } //测试数据源 private void ShowNews() { String strSql = String.Format("select * from News order by time asc"); DataTable dtbl = Access.ExecuteDataTable(strSql, null); this.rptNews.DataSource = Access.GetPageDataSource(AspNetPager1, AspNetPager1.CurrentPageIndex - 1, dtbl); this.rptNews.DataBind(); } //分页点击页码事件 protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e) { this.AspNetPager1.CurrentPageIndex = e.NewPageIndex; ShowNews(); } }

    三、Demo

      AspNetPager(dll)

      AspNetPage(Demo)

    以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,同时也希望多多支持!

    -六神源码网