我发现,gridview中的页面定位的功能很不好用。
我想问下,有没有什么三方组件,可以直接替代gridview,同时,有“上一页”“下一页”首页,末页的功能?

解决方案 »

  1.   

    网上有很多分页控件,当然我是自己写的。----------------LhhPagination2.ascx--------------
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="LhhPagination2.ascx.cs" Inherits="UserControls_LhhPagination2" %>
    <table width="100%" border="0" align="center" cellspacing="0" class="pagination">
    <tr>
    <td align="left" width="50%">
    <asp:LinkButton ID="lbtnFirst" runat="server" ToolTip="首页" OnClick="lbtnFirst_Click">
    <asp:Literal runat="server" Text="首页" ID="litFirst"></asp:Literal>
    </asp:LinkButton>&nbsp;
    <asp:LinkButton ID="lbtnPrevious" runat="server" ToolTip="上一页" OnClick="lbtnPrevious_Click">
    <asp:Literal runat="server" Text="上一页" ID="litPrevious"></asp:Literal>
    </asp:LinkButton>&nbsp;
    <asp:LinkButton ID="lbtnNext" runat="server" ToolTip="下一页" OnClick="lbtnNext_Click">
    <asp:Literal runat="server" Text="下一页" ID="litNext"></asp:Literal>
    </asp:LinkButton>&nbsp;
    <asp:LinkButton ID="lbtnLast" runat="server" ToolTip="尾页" OnClick="lbtnLast_Click">
    <asp:Literal runat="server" Text="尾页" ID="litLast"></asp:Literal>
    </asp:LinkButton>
    <asp:RangeValidator id="rngPageIndex" runat="server" Display="None" ControlToValidate="txtPageIndex"
    Type="Integer" MinimumValue="1"></asp:RangeValidator>
    </td>
    <td align="right">
    共<asp:Label ID="lblRecordCount" runat="server" Font-Bold="True" ForeColor="Blue"></asp:Label>条记录 
    当前第<asp:Label ID="lblCurrentPage" runat="server" Font-Bold="True" ForeColor="Green"></asp:Label>页/ 
    共<asp:Label ID="lblPageCount" runat="server" Font-Bold="True" ForeColor="Blue"></asp:Label>页 
    &nbsp;&nbsp; 转到<asp:TextBox ID="txtPageIndex" style="TEXT-ALIGN:right" runat="server" Width="25px"></asp:TextBox><asp:LinkButton ID="lbtnGo" runat="server" ToolTip="跳转到该页" OnClick="lbtnGo_Click">GO</asp:LinkButton>
    </td>
    </tr>
    </table>
    <asp:ValidationSummary id="vsPagination" runat="server" ShowMessageBox="True" ShowSummary="False"></asp:ValidationSummary>----------------LhhPagination2.ascx.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;/// <summary>
    /// 不使用存储过程的分页技术
    /// </summary>
    public partial class UserControls_LhhPagination2 : System.Web.UI.UserControl
    {
    private int pageIndex = 1;
    private int pageCount;
    private int pageSize = 10;
    private int recordCount;
    private int showPaginationType;
    private string tableName;
    private string pkField="id"; //default:ID
    private string queryFields = "*"; //default:*
    private string sqlWhere = ""; //default:""
    private string sqlOrder = ""; //default:""
    private string controlName;
    private GridView gridView; //绑定的控件明
    public string ControlToValidate {
    set {
    controlName = value;
    }
    }
    //显示分页的类型
    public int ShowPaginationType {
    set {
    showPaginationType = value;
    }
    }
    //数据库表名或视图名
    public string TableName {
    set {
    tableName = value;
    }
    }
    //主键字段
    public string PkField {
    set {
    pkField = value;
    }
    }
    //需要显示的字段名列表
    public string QueryFields {
    set {
    queryFields = value;
    }
    }
    //需要过滤的字段名列表
    public string SqlWhere {
    set {
    sqlWhere = value;
    }
    }
    //需要排序的字段名列表
    public string SqlOrder {
    set {
    sqlOrder = value;
    }
    }
    //每页记录数
    public int PageSize {
    set {
    pageSize = value;
    }
    }
    //当前页码
    public int PageIndex {
    set {
    if (value < 1) {
    pageIndex = 1;
    } else if (value > pageCount) {
    pageIndex = pageCount;
    } else {
    pageIndex = value;
    }
    }
    }
    protected void Page_Load(object sender, EventArgs e) {
    //设置变量
    recordCount = GetRecordCound();
    if (recordCount % pageSize == 0) {
    pageCount = recordCount / pageSize;
    } else {
    pageCount = recordCount / pageSize + 1;
    }
    gridView = (GridView)Page.FindControl(controlName);
    //设置验证控件属性
    rngPageIndex.MaximumValue = pageCount.ToString();
    rngPageIndex.ErrorMessage = "有效的页码范围是1-" + pageCount.ToString();
    //设置初次打开页面
    if (!Page.IsPostBack) {
    pageIndex = 1;
    ViewState["pageIndex"] = pageIndex;
    BindGridView();
    }
    }
    //重写Render方法
    protected override void Render(System.Web.UI.HtmlTextWriter writer) { //设置页面显示的信息
    lblCurrentPage.Text = pageIndex.ToString();
    lblPageCount.Text = pageCount.ToString();
    lblRecordCount.Text = recordCount.ToString();
    txtPageIndex.Text = pageIndex.ToString();
    //设置显示分页的样式
    if (showPaginationType == 0) {
    litFirst.Text = "首页";
    litPrevious.Text = "上一页";
    litNext.Text = "下一页";
    litLast.Text = "尾页";
    } else if (showPaginationType == 1) {
    litFirst.Text = "<font face=webdings>9</font>";
    litPrevious.Text = "<font face=webdings>3</font>";
    litNext.Text = "<font face=webdings>4</font>";
    litLast.Text = "<font face=webdings>:</font>";
    }
    base.Render(writer);
    }
    //获取记录总数
    private int GetRecordCound() {
    string strQuery = null;
    if (sqlWhere != null && sqlWhere != string.Empty) {
    strQuery = "select count(*) from " + tableName + " where " + sqlWhere;
    } else {
    strQuery = "select count(*) from " + tableName;
    }
    Common.SqlHelp help = new Common.SqlHelp();
    return (int)help.ExecuteScalar(strQuery);
    } private void BindGridView() {
    Common.SqlHelp help = new Common.SqlHelp();
    DataSet ds = help.GetList(tableName, pkField, queryFields, sqlWhere, sqlOrder, pageSize, pageIndex);
    gridView.DataSource = ds.Tables[0].DefaultView;
    gridView.DataBind();
    }
    //首页
    protected void lbtnFirst_Click(object sender, System.EventArgs e) {
    pageIndex = 1;
    ViewState["pageIndex"] = pageIndex;
    BindGridView();
    }
    //上一页
    protected void lbtnPrevious_Click(object sender, System.EventArgs e) {
    pageIndex = (int)ViewState["pageIndex"];
    pageIndex--;
    if (pageIndex < 1) {
    pageIndex = 1;
    }
    ViewState["pageIndex"] = pageIndex;
    BindGridView();
    }
    //下一页
    protected void lbtnNext_Click(object sender, System.EventArgs e) {
    pageIndex = (int)ViewState["pageIndex"];
    pageIndex++;
    if (pageIndex > pageCount) {
    pageIndex = pageCount;
    }
    ViewState["pageIndex"] = pageIndex;
    BindGridView();
    }
    //尾页
    protected void lbtnLast_Click(object sender, System.EventArgs e) {
    pageIndex = pageCount;
    ViewState["pageIndex"] = pageIndex;
    BindGridView();
    }
    //GO
    protected void lbtnGo_Click(object sender, System.EventArgs e) {
    pageIndex = int.Parse(txtPageIndex.Text);
    ViewState["pageIndex"] = pageIndex;
    BindGridView();
    }
    }
      

  2.   

    有啊
    http://www.webdiyer.com/AspNetPager/default.aspx去这里