引用分页控件页面前台
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="control/AspnetPager.ascx" TagName="AspnetPager" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "> <html xmlns="> <head runat="server"> <title>无标题页</title> <link href="css/default.css" rel="stylesheet" type="text/css" /> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server"></asp:GridView> <br /> <uc1:AspnetPager ID="AspnetPager1" runat="server" OnPageIndex_Chaged="AspNetPager1_PageIndexChanged" CurrentPageIndex="1" PageSize="10" /> </div> </form> </body> </html>后台
using System;
using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetData(); } }
protected void AspNetPager1_PageIndexChanged(object sender, EventArgs e)
{ GetData(); }protected DataTable GetData()
{ DataTable table = null; using (SqlConnection conn = new SqlConnection("Data Source=(local); database=database;uid=***;pwd=****")) { if (conn.State == ConnectionState.Closed) { conn.Open(); } SqlCommand cmd = new SqlCommand("jjp_CategoryList_Get", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@pageIndex", AspnetPager1.CurrentPageIndex); cmd.Parameters.AddWithValue("@PageSize", AspnetPager1.PageSize); cmd.Parameters.Add("@Records", SqlDbType.Int); cmd.Parameters["@Records"].Direction = ParameterDirection.Output; SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); AspnetPager1.TotalRecordsCount = int.Parse(cmd.Parameters["@Records"].Value.ToString()); } return table; } }