GRIDVIEW分页用户控件

本文介绍了一个自定义的ASP.NET分页控件实现方式,该控件能够与GridView结合使用,提供前后页切换、跳转等功能,并通过DropDownList实现了页面选择。此控件依赖于C#编程语言。
前台:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Pager.ascx.cs" Inherits="e_booking_English_UserControls_Pager" %>
<div style="text-align: right; padding-right: 30px">
    每页<asp:Label ID="pagesize" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageSize %>'
        Style="padding-right: 3px; padding-left: 3px"></asp:Label>项 共<asp:Label ID="rowscount"
            runat="server" Text='<%# ((GridView)Container.Parent.Parent).Rows.Count %>' Style="padding-right: 3px;
            padding-left: 3px"></asp:Label>项
    <asp:LinkButton ID="cmdFirstPage" runat="server" CommandName="Page" CommandArgument="First"
        Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=0 %>">首页</asp:LinkButton>
    <asp:LinkButton ID="cmdPreview" runat="server" CommandArgument="Prev" CommandName="Page"
        Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=0 %>">前页</asp:LinkButton>
    第<asp:Label ID="lblcurPage" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex+1%>'
        Style="padding-right: 3px; padding-left: 3px"></asp:Label>页/共<asp:Label ID="lblPageCount"
            runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageCount %>' Style="padding-right: 3px;
            padding-left: 3px"></asp:Label>页
    <asp:LinkButton ID="cmdNext" runat="server" CommandName="Page" CommandArgument="Next"
        Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=((GridView)Container.Parent.Parent).PageCount-1 %>">后页</asp:LinkButton>
    <asp:LinkButton ID="cmdLastPage" runat="server" CommandArgument="Last" CommandName="Page"
        Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=((GridView)Container.Parent.Parent).PageCount-1 %>">尾页</asp:LinkButton>
    转到<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" CausesValidation="false"
        OnSelectedIndexChanged="DropPageChanged" OnDataBinding="DropDownList1_DataBinding">
    </asp:DropDownList>
    页
</div>

后台:

using System;
using System.Data;
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 e_booking_English_UserControls_Pager : System.Web.UI.UserControl
{
    public GridView grd
    {
        get
        {
            GridView grd = Parent.Parent.NamingContainer as GridView;
            return grd;
        }
    }
    private void Page_Load(object sender, System.EventArgs e)
    {

    }
    protected void DropPageChanged(object sender, EventArgs e)
    {
        DropDownList DropDownList1 = sender as DropDownList;
        grd.PageIndex = int.Parse(DropDownList1.SelectedValue);
    }
    protected void DropDownList1_DataBinding(object sender, EventArgs e)
    {
        for (int i = 1; i <= grd.PageCount; i++)
        {
            DropDownList1.Items.Add(new ListItem(i + "/" + grd.PageCount, (i - 1).ToString()));
        }
        DropDownList1.SelectedIndex = grd.PageIndex;
    }
}

转载于:https://www.cnblogs.com/cosiray/archive/2009/08/22/1551968.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值