一、用a标签和HTML的input普通按钮

<asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate><ul></HeaderTemplate>
            <FooterTemplate></ul></FooterTemplate>
            <ItemTemplate>
                <li><%# Eval("Name") %></li>
            </ItemTemplate>
        </asp:Repeater>
    </div>
        <a href="Default.aspx" runat="server" id="lnkPrev">
            <input id="Button1" type="button" value="上一页" /></a>
        <a href="Default.aspx" runat="server" id="lnkNext">
            <input id="Button2" type="button" value="下一页" /></a>
public partial class _Default : System.Web.UI.Page
{
    private MyDBDataContext _Context = new MyDBDataContext();
    private const int PAGESIZE = 3;
    private int _PageNo = 1;  //当前的页号
    //获取总页数
    public int GetPageCount()
    {
        //取总行数
        int rowsCount = _Context.Car.Count();
        //算出总页数
        int pageCount = (int)Math.Ceiling( 1.0*rowsCount / PAGESIZE);

        return pageCount;
    }
    public List<Car> GetPagedCar()
    {
        var query = _Context.Car.Skip(PAGESIZE*(_PageNo-1)).Take(PAGESIZE);
        return query.ToList();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request["pn"] != null)
            {
                _PageNo = Convert.ToInt32(Request["pn"]);
            }
            Repeater1.DataSource = GetPagedCar();
            Repeater1.DataBind();

            if (_PageNo == 1)
            {
                lnkPrev.HRef = "Default.aspx?pn=1";
            }
            else
            {
                lnkPrev.HRef = "Default.aspx?pn=" + (_PageNo - 1).ToString();
            }
            if (_PageNo == GetPageCount())
            {
                lnkNext.HRef = "Default.aspx?pn=" + GetPageCount().ToString();
            }
            else
            {
                lnkNext.HRef = "Default.aspx?pn=" + (_PageNo + 1).ToString();
            }

        }
    }
}

二、用DropDownList和普通按钮

<form id="form1" runat="server">
    <div>
     <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate><ul></HeaderTemplate>
            <FooterTemplate></ul></FooterTemplate>
            <ItemTemplate>
                <li><%# Eval("Name") %></li>
            </ItemTemplate>
        </asp:Repeater>
    </div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上一页" />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="下一页" />
        <asp:DropDownList ID="PageList" runat="server" AutoPostBack="True" OnSelectedIndexChanged="PageList_SelectedIndexChanged">
        </asp:DropDownList>
    &nbsp;一共<asp:Label ID="lblAll" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label>
        页,当前是第<asp:Label ID="lblNow" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label></form>

 

public partial class Default2 : System.Web.UI.Page
{
    private MyDBDataContext _Context = new MyDBDataContext();
    private const int PAGESIZE = 3;
    private int _PageNo = 1;  //当前的页号
    //获取总页数
    public int GetPageCount()
    {
        //取总行数
        int rowsCount = _Context.Car.Count();
        //算出总页数
        int pageCount = (int)Math.Ceiling(1.0 * rowsCount / PAGESIZE);

        return pageCount;
    }
    //获取指定页面记录
    public List<Car> GetPagedCar()
    {
        _PageNo = Convert.ToInt32( PageList.SelectedValue);
        var query = _Context.Car.Skip(PAGESIZE * (_PageNo - 1)).Take(PAGESIZE);
        return query.ToList();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillPageList();
            ShowCars();
        }
    }

    private void ShowCars()
    {
        Repeater1.DataSource = GetPagedCar();
        Repeater1.DataBind();

        //给当前页和一共几页赋值
        lblNow.Text = PageList.SelectedValue;
        lblAll.Text = PageList.Items.Count.ToString();
    }
    //在下拉列表里面获取总页数
    private void FillPageList()
    {
        PageList.Items.Clear();

        int pageCount = GetPageCount();
        for (int i = 1; i <= pageCount; i++)
        {
            ListItem li = new ListItem(i.ToString(), i.ToString());
            PageList.Items.Add(li);
        }
    }
    //点击下拉列表里的数字
    protected void PageList_SelectedIndexChanged(object sender, EventArgs e)
    {
        ShowCars();
    }
    //上一页
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (PageList.SelectedIndex == 0)
        {
            return;
        }
        PageList.SelectedIndex--;
        ShowCars();
    }
    //下一页
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (PageList.SelectedIndex == GetPageCount() - 1)
        {
            return;
        }

        PageList.SelectedIndex++;
        ShowCars();
    }
}

 

posted on 2015-08-18 10:42  浅笑瑾年  阅读(109)  评论(0编辑  收藏  举报