• [转]Repeate分页


     
    Repeate分页 (2008-07-20 16:01:24)

    有上一页下一页首页尾页
    主要代码参考天轰穿教程
    数据库直接用的sql2000中的pubs

    以下为源码:
    //repeate.aspx.cs

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class repeate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Repeater1.DataSource = pds();
                Repeater1.DataBind();           
            }
        }
        private PagedDataSource pds()
        {
            string connstring = ConfigurationManager.ConnectionStrings["pconn"].ConnectionString;
            SqlConnection con = new SqlConnection(connstring);
          
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter("select * from authors",con);
            sda.Fill(ds,"name");
            SqlDataAdapter sda2 = new SqlDataAdapter("select * from titleauthor",con);
            sda2.Fill(ds,"title");
            ds.Relations.Add("myrela",ds.Tables["name"].Columns["au_id"],ds.Tables["title"].Columns["au_id"]);

            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = ds.Tables["name"].DefaultView;
            pds.AllowPaging = true;//允许分页
            pds.PageSize = 5;//单页显示项数
            pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
            return pds;
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Footer)
            {
                DropDownList ddlp = (DropDownList)e.Item.FindControl("ddlp");

                HyperLink lpfirst = (HyperLink)e.Item.FindControl("hlfir");
                HyperLink lpprev = (HyperLink)e.Item.FindControl("hlp");
                HyperLink lpnext = (HyperLink)e.Item.FindControl("hln");
                HyperLink lplast = (HyperLink)e.Item.FindControl("hlla");

                pds().CurrentPageIndex = ddlp.SelectedIndex;

                int n = Convert.ToInt32(pds().PageCount);//n为分页数
                int i = Convert.ToInt32(pds().CurrentPageIndex);//i为当前页

                Label lblpc = (Label)e.Item.FindControl("lblpc");
                lblpc.Text = n.ToString();
                Label lblp = (Label)e.Item.FindControl("lblp");
                lblp.Text = Convert.ToString(pds().CurrentPageIndex + 1);

                if (!IsPostBack)
                {
                    for (int j = 0; j < n; j++)
                    {
                        ddlp.Items.Add(Convert.ToString(j + 1));
                    }
                }

                if (i <= 0)
                {
                    lpfirst.Enabled = false;
                    lpprev.Enabled = false;
                    lplast.Enabled = true;
                    lpnext.Enabled = true;
                }
                else
                {
                    lpprev.NavigateUrl = "?page=" + (i - 1);
                }
                if (i >= n - 1)
                {
                    lpfirst.Enabled = true;
                    lplast.Enabled = false;
                    lpnext.Enabled = false;
                    lpprev.Enabled = true;
                }
                else
                {
                    lpnext.NavigateUrl = "?page=" + (i + 1);
                }

                lpfirst.NavigateUrl = "?page=0";//向本页传递参数page
                lplast.NavigateUrl = "?page=" + (n - 1);

                ddlp.SelectedIndex = Convert.ToInt32(pds().CurrentPageIndex);//更新下拉列表框中的当前选中页序号
            }

        }
        protected void ddlp_SelectedIndexChanged(object sender, EventArgs e)
        {//脚模板中的下拉列表框更改时激发
            string pg=Convert.ToString((Convert.ToInt32(((DropDownList)sender).SelectedValue)-1));//获取列表框当前选中项
            Response.Redirect("repeate.aspx?page="+pg);//页面转向
        }
    }


    //repeate.aspx
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="repeate.aspx.cs" Inherits="repeate" %>
    <%@ Import Namespace="System.Data" %>

    <!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:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound" >
            <HeaderTemplate><%-- 我是头模板--%>
            <table width="500">
            <tr style="background-color: #ccffcc;">
            <td>作者</td>
            <td>书籍</td>
            </tr>
            </HeaderTemplate>
            <ItemTemplate><%--我是项模板--%>
            <tr>
            <td><a href='repeate.aspx?id=<%# Eval("au_id")%>'><%# Eval("au_lname") %></a></td>
            <td><asp:Repeater ID="Repeater2" runat="server" DataSource='<%# Eval("myrela") %>'>
            <ItemTemplate>
            <%# Eval("[\"title_id\"]") %>
            </ItemTemplate>
            </asp:Repeater>
            </td>
            </tr>       
            </ItemTemplate>
            <SeparatorTemplate><%--这是分隔线模板--%>
            <tr>
            <td colspan="2">
            <hr style="border-top:1pt;"/>
            </td>
            </tr>
            </SeparatorTemplate>
            <FooterTemplate><%--这是脚模板--%>
            <tr>
            <td colspan="2" style="font-size:12pt;color:#0099ff; background-color:#e6feda;">
            共<asp:Label ID="lblpc" runat="server" Text="Label"></asp:Label>页当前为第
            <asp:Label ID="lblp" runat="server" Text="Label"></asp:Label>页
            <asp:HyperLink ID="hlfir" runat="server" Text="首页"></asp:HyperLink>
            <asp:HyperLink ID="hlp" runat="server" Text="上一页"></asp:HyperLink>
            <asp:HyperLink ID="hln" runat="server" Text="下一页"></asp:HyperLink>
            <asp:HyperLink ID="hlla" runat="server" Text="尾页"></asp:HyperLink>
             跳至第
             <asp:DropDownList ID="ddlp" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlp_SelectedIndexChanged" >
             </asp:DropDownList>页
            </td>
            </tr>
            </table>
            </FooterTemplate>
            </asp:Repeater>
            &nbsp;</div>
        </form>
    </body>
    </html>

  • 相关阅读:
    RK3399之时钟
    C之{}注意点
    ARM之不用段寄存猜想
    linux驱动之入口
    android之HAL
    git
    消息中间之ActiveMQ
    Maven之阿里云镜像仓库配置
    清理Oracle临时表空间
    Tomcat控制台日志输出到本地文件
  • 原文地址:https://www.cnblogs.com/xjyggd/p/1342337.html
Copyright © 2020-2023  润新知