利用Repeater控件来分页显示

web.config 文件

<?xml version="1.0" encoding="utf-8"?>
<!--
    注意: 除了手动编辑此文件以外,您还可以使用
    Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
     “网站”->“Asp.Net 配置”选项。
    设置和注释的完整列表在
    machine.config.comments 中,该文件通常位于
    /Windows/Microsoft.Net/Framework/v2.x/Config 中
-->
<configuration>
    <appSettings/>
    <connectionStrings>
        <add name="connStr" connectionString="Data Source=L;Initial Catalog=database;User ID=sa" providerName="System.Data.SqlClient" />
    </connectionStrings>
    <system.web>
        <!--
            设置 compilation debug="true" 将调试符号插入
            已编译的页面中。但由于这会
            影响性能,因此只在开发过程中将此值
            设置为 true。
        -->
        <compilation debug="false" />
        <!--
            通过 <authentication> 节可以配置 ASP.NET 使用的
            安全身份验证模式,
            以标识传入的用户。
        -->
        <authentication mode="Windows" />
        <!--
            如果在执行请求的过程中出现未处理的错误,
            则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
            开发人员通过该节可以配置
            要显示的 html 错误页
            以代替错误堆栈跟踪。

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
    </system.web>
</configuration>
 

default.aspx  文件

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>共<asp:Literal ID="ltl_RecordCount" runat=server></asp:Literal>条记录 共<asp:Literal ID="ltl_PageCount" runat=server></asp:Literal>页
        当前第<asp:Literal ID="ltl_Pageindex" runat="server"></asp:Literal>页
        <asp:HyperLink ID="lbn_First" runat=server>首页</asp:HyperLink><asp:HyperLink ID="lbn_Prev" runat=server>上一页</asp:HyperLink>
        <asp:HyperLink ID="lbn_Next" runat=server>下一页</asp:HyperLink><asp:HyperLink ID="lbn_Last" runat=server>尾页</asp:HyperLink>
        转到<asp:Literal ID="ltl_Jump" runat=server></asp:Literal>
    </div>
    <div>aaa bbbb</div>
    <div>
        <asp:Repeater ID="ad1" runat=server>
            <ItemTemplate><!--队列模板-->
               <div> <%# Eval("ADInfoID") %>
                <%# Eval("ADInfoTitle") %></div>

            </ItemTemplate>
                       
        </asp:Repeater>

    </div>
    </form>
</body>
</html>

default.aspx.cs  文件

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Text;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;//SQL数据库的必须类库

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //**************
        //直接连接数据库
        //string connStr="user id=sa;password=;initial catalog=epabc;data source=localhost";
        //SqlConnection MyConn = new SqlConnection(connStr);

        //********************
        //通过配置文件的连接字符串来连接
        SqlConnection conn=new SqlConnection(ConfigurationManager.ConnectionStrings["connStr"].ConnectionString);

        string sql = "select * from adinfo";
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        //this.ad1.DataSource = ds.Tables["adinfos"];
        //this.ad1.DataBind();
       // conn.Close();

        //DataTable dt=new DataTable();
   

        int Pageindex;
        if (Request.QueryString["Page"] != null)
            Pageindex = Convert.ToInt32(Request.QueryString["Page"]);
        else
            Pageindex = 1;

        int L_Manage = 0;
    //对用于分页的类的引用
PagedDataSource pds=new PagedDataSource();
pds.DataSource=ds.Tables[0].DefaultView;//设置数据源(DataTable类型)
pds.AllowPaging=true;
//第页显示的行数
pds.PageSize=18;

//设置当前页
if(Pageindex<1) Pageindex=1;
pds.CurrentPageIndex=Pageindex-1;

this.ad1.DataSource=pds;
this.ad1.DataBind();

//显示页码
ltl_RecordCount.Text = pds.DataSourceCount.ToString();
ltl_PageCount.Text = pds.PageCount.ToString();
ltl_Pageindex.Text = Pageindex.ToString();
ltl_Jump.Text = Jump_List (pds.PageCount , Pageindex , L_Manage);

//显示上下翻页(URL后面跟的参数自已跟据需要定义)
lbn_First.ToolTip = "跳转到首页";
lbn_First.NavigateUrl=Request.CurrentExecutionFilePath+"?Org_ID="+ L_Manage +"&page=1";
lbn_Prev.ToolTip = "跳转到上一页";
lbn_Prev.NavigateUrl=Request.CurrentExecutionFilePath+"?Org_ID="+ L_Manage +"&page="+(Pageindex-1);
lbn_Next.ToolTip = "跳转到下一页";
lbn_Next.NavigateUrl=Request.CurrentExecutionFilePath+"?Org_ID="+ L_Manage +"&page="+(Pageindex+1);
lbn_Last.ToolTip = "跳转到最后一页";
lbn_Last.NavigateUrl=Request.CurrentExecutionFilePath+"?Org_ID="+ L_Manage +"&page="+pds.PageCount.ToString();

//确定链接的显示方式
if(Pageindex<=1 && pds.PageCount<=1)
{
    lbn_First.NavigateUrl = "";
    lbn_Prev.NavigateUrl = "";
    lbn_Next.NavigateUrl = "";
    lbn_Last.NavigateUrl = "";
}
if(Pageindex<=1 && pds.PageCount>1)
{
    lbn_First.NavigateUrl = "";
    lbn_Prev.NavigateUrl = "";
}
if(Pageindex >= pds.PageCount)
{
    lbn_Next.NavigateUrl = "";
    lbn_Last.NavigateUrl = "";
}

/// <summary>
/// 计算分页跳转
/// </summary>
/// <param name="Pagecount">页面数</param>
/// <returns>string</returns>

 

    }

    private string Jump_List(int Pagecount, int Pageindex, long L_Manage)
    {
        StringBuilder sb = new StringBuilder(); //继承 using System.Text;
        sb.Append("<select id=/"Page_Jump/" name=/"Page_Jump/" οnchange=/"window.location='" + Request.CurrentExecutionFilePath + "?page='+ this.options[this.selectedIndex].value + '&Org_ID=" + L_Manage + "';/">");
        for (int i = 1; i <= Pagecount; i++)
        {
            if (Pageindex == i)
                sb.Append("<option value='" + i + "' selected>" + i + "</option>");
            else
                sb.Append("<option value='" + i + "'>" + i + "</option>");
        }
        sb.Append("</select>");

        return sb.ToString();
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值