AspNetPager+GridView implements paged instance code

  • 2020-05-30 19:52:08
  • OfStack

The framework is.NET Framework 4.0
.1 consists of three parts: the design code of the front page, the program code of the front page, and the css style
Where the database connection operation USES DB class (connection statement), SQLHelper (Microsoft's database operation class)
Effect:


Foreground page design code


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestWebSite.Default" %>
<%@ Register assembly="AspNetPager" namespace="Wuqi.Webdiyer" tagprefix="webdiyer" %>
<!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>
    <link href="Styles/Paging.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:GridView ID="GridView1" runat="server" Height="261px" Width="737px" 
            CellPadding="4" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle HorizontalAlign="Left" BackColor="#507CD1" Font-Bold="True" 
                ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>

    </div>
    <webdiyer:AspNetPager ID="AspNetPager1" runat="server" 
        onpagechanged="AspNetPager1_PageChanged" CssClass="anpager" 
        CurrentPageButtonClass="cpb" FirstPageText=" Home page " LastPageText=" back " 
        NextPageText=" After the page " PrevPageText=" Previous page ">
    </webdiyer:AspNetPager>
    </form>
</body>
</html>

Foreground page program code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TestWebSite.Utilities;
using System.Data;
using System.Data.SqlClient;
using Wuqi.Webdiyer;
namespace TestWebSite
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Call binding paging and GridView
                BindGridView();
            }
        }
        //// Bind paging and GridView methods 
        private void BindGridView()
        {
            // The query 
            string sequal = "select StandardName as  The name of the standard , MakeUpItem as  Compensation program , Unit as  unit ," 
                + " cast(UnitPrice as decimal(18,2)) as  The unit price , cast(StandRate as decimal(18,2)) as "
                + " The new rate , Type as  classification  from Standard";
            // Get data table 
            DataTable dt = 
                SqlHelper.ExecuteDataset(DB.con, CommandType.Text, sequal).Tables[0];
            // Initializes the split page data source instance 
            PagedDataSource pds = new PagedDataSource();
            // Set the total number of rows 
            AspNetPager1.RecordCount = dt.Rows.Count;
            // Set the paging data source 
            pds.DataSource = dt.DefaultView;
            // Set the current page 
            pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            // Set the number of pages to display per page 
            pds.PageSize = AspNetPager1.PageSize;
            // Enable paging 
            pds.AllowPaging = true;
            // Set up the GridView Is the paging data source 
            GridView1.DataSource = pds;
            // The binding GridView
            GridView1.DataBind();
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            // Call binding paging and GridView
            BindGridView();
        }
    }
}

CSS style

.anpager 
{ 
    font: 11px Arial, Helvetica, sans-serif;
    padding:10px 20px 10px 0; 
    margin: 0px;
}
.anpager a 
{
    padding: 1px 6px; 
    border: solid 1px #ddd; 
    background: #fff; 
    text-decoration: none;
    margin-right:2px
}
.anpager a:visited 
{
    padding: 1px 6px; 
    border: solid 1px #ddd; 
    background: #fff; 
    text-decoration: none;
}
.anpager .cpb 
{
    padding: 1px 6px;
    font-weight: bold; 
    font-size: 13px;
    border:none
}
.anpager a:hover 
{
    color: #fff; 
    background: #ffa501;
    border-color:#ffa501;
    text-decoration: none;
}

/* AspNetPager1 property setting: CssClass="anpager" CurrentPageButtonClass="cpb"*/


Related articles: