欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

解析asp.net的分页控件

程序员文章站 2023-09-03 23:56:05
一、说明   aspnetpager.dll这个分页控件主要用于asp.net webform网站,现将整理代码如下 二、代码 1、首先在测试页面default.as...

一、说明

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

二、代码

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

<%@ register assembly="aspnetpager" namespace="wuqi.webdiyer" tagprefix="webdiyer" %>

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

<asp:repeater id="rptnews" runat="server">
  <itemtemplate>
    <li>
      <span><%# eval("time") %></span>
      <a href="newsshow.aspx?id=<%# eval("id") %>"><%# access.getstringnum( eval("name").tostring(),15) %></a>
    </li>
  </itemtemplate>
</asp:repeater>

3、添加

<webdiyer:aspnetpager id="aspnetpager1" runat="server" alwaysshow="true" custominfostyle="font-size: 12px"
    firstpagetext="首页" horizontalalign="center" inputboxstyle="width:19px" lastpagetext="尾页"
    meta:resourcekey="aspnetpager1" nextpagetext="下一页" pagesize="10" prevpagetext="上一页"
    style="font-size: 14px" width="95%" cssclass="anpager" currentpagebuttonclass="cpb"
    onpagechanging="aspnetpager1_pagechanging" showboxthreshold="10">
</webdiyer:aspnetpager>

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

解析asp.net的分页控件

解析asp.net的分页控件

4、后台代码绑定

//测试数据源<br>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" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
 <title>asp.net分页控件</title>
 <link href="css/css.css" rel="stylesheet" type="text/css" />
 <style type="text/css">
 .aboutcontentnr{width:100%; height:auto; }
 </style>
</head>
<body>
 <form id="form1" runat="server">
 <div>
  <ul class="news">
   <asp:repeater id="rptnews" runat="server">
    <itemtemplate>
     <li><span>
      <%# eval("time") %></span><a href="newsshow.aspx?id=<%# eval("id") %>"><%# access.getstringnum( eval("name").tostring(),15) %></a></li>
    </itemtemplate>
   </asp:repeater>
  </ul>
  <div class="paginator">
   <webdiyer:aspnetpager id="aspnetpager1" runat="server" alwaysshow="true" custominfostyle="font-size: 12px"
    firstpagetext="首页" horizontalalign="center" inputboxstyle="width:19px" lastpagetext="尾页"
    meta:resourcekey="aspnetpager1" nextpagetext="下一页" pagesize="10" prevpagetext="上一页"
    style="font-size: 14px" width="95%" cssclass="anpager" currentpagebuttonclass="cpb"
    onpagechanging="aspnetpager1_pagechanging" showboxthreshold="10">
   </webdiyer:aspnetpager>
  </div>
 </div>
 </form>
</body>
</html>

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)

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