• 使用用户控件AspNetPager+Gridview实现分页功能


    1 前台代码:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestPager.aspx.cs" Inherits="TestPager" %>
    <%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %> 
    <!DOCTYPE html>
    <html >
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div >
            <asp:GridView ID="GridView1" runat="server" HorizontalAlign="Center">
                <RowStyle HorizontalAlign="Center" />
            </asp:GridView>
            <br/><br/><br/>
            <webdiyer:AspNetPager ID="AspNetPager1" runat="server" CustomInfoHTML="共%PageCount%页,
    当前为第%CurrentPageIndex%页,每页%PageSize%条" FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" PrevPageText="上一页" ShowBoxThreshold="1" ShowCustomInfoSection="right" Width="100%"
    OnPageChanging="AspNetPager1_PageChanging" PageSize="1" ButtonImageAlign="Middle"
    CustomInfoTextAlign="Center" Direction="LeftToRight" HorizontalAlign="Center" NumericButtonCount="5"
    CenterCurrentPageButton="True"> </webdiyer:AspNetPager> </div> </form> </body> </html>

     2后台代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Configuration;
    using System.Data.SqlClient;
    public partial class TestPager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridBindData();
              
            }
        }
    
        private void GridBindData()
        {
            string strConn = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strConn))
            {
                con.Open();
                string sql = "select t.* from Student t";
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        DataSet ds = new DataSet();
                        da.Fill(ds, "Student");
                        PagedDataSource pds = new PagedDataSource();
                        pds.DataSource = ds.Tables[0].DefaultView;
                        pds.AllowPaging = true;
                        AspNetPager1.RecordCount = pds.Count;
                        pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                        pds.PageSize = AspNetPager1.PageSize;
                        this.GridView1.DataSource = pds;
                        this.GridView1.DataBind();
                    
                    }
                
                }
            
            
            }
        }
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            GridBindData();
        }  
    }
    

     3 测试截图:

  • 相关阅读:
    【Selenium IDE】下载安装Chrome和Firefox插件IDE ide了解就行 不是重点 重点是写脚本
    调用接口时,生产环境,路径加斜杠“/”和不加的区别
    WPF 踩坑笔记12 DataGrid触发选中行事件
    WPF 踩坑笔记11 线程取消
    WPF 踩坑笔记10 ListBox异步动态加载
    WPF 踩坑笔记9 直接打印
    思维的体操
    【洛谷 P4213】 【模板】杜教筛(Sum)
    【洛谷 P2257】 YY的GCD(莫比乌斯反演)
    【洛谷 P4980】 【模板】Pólya 定理
  • 原文地址:https://www.cnblogs.com/thbbsky/p/3091635.html
Copyright © 2020-2023  润新知