Add custom navigation and paging information for the DataGrid customization page

Source: Internet
Author: User
datagrid| pagination in the last article I talked about customizing the DataGrid page, this avoids getting the entire data recordset to display a single page of data, thereby increasing paging efficiency, but using a digital connection or a simple previous page with a DataGrid, the next page, And you can't see the total number of pages, total records, and so on information. Here's what we need to add to the section.

First look at the modified pagination display, screenshots are as follows:



(Figure I)

The data source used is the same as the custom paging of the DataGrid control in ASP.net, which is access to the Northwind library, and for the sake of independence here's a list of stored procedures,

CREATE PROCEDURE [Getcustomersdatapage]

@PageIndex INT,

@PageSize INT,

@RecordCount INT out,

@PageCount INT out

As

SELECT @RecordCount = COUNT (*) from Customers

SET @PageCount = CEILING (@RecordCount * 1.0/@PageSize)

DECLARE @SQLSTR NVARCHAR (1000)

IF @PageIndex = 0 OR @PageCount <= 1

SET @SQLSTR =n ' SELECT top ' +str (@PageSize) +

' CustomerID, Companyname,address,phone from Customers order by CustomerID DESC

ELSE IF @PageIndex = @PageCount-1

SET @SQLSTR =n ' select * FROM (select top ' +str (@RecordCount-@PageSize * @PageIndex) +

' CustomerID, Companyname,address,phone from Customers to CustomerID ASC ' temptable ORDER by CustomerID '

ELSE

SET @SQLSTR =n ' select Top ' +str (@PageSize) + ' * FROM (select top ' +str (@RecordCount-@PageSize * @PageIndex) +

' CustomerID, Companyname,address,phone from Customers to CustomerID ASC ' temptable ORDER by CustomerID '



EXEC (@SQLSTR)

Go



The following is the code posted,

The ASPX file code is as follows:

<%@ Page language= "C #" codebehind= "DataGridCustomPaging.aspx.cs" autoeventwireup= "false" inherits= "ZZ. Aspnetpaging.datagridcustompaging "%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >

<HTML>

<HEAD>

<title>DataGridPaging</title>

<meta content= "Microsoft Visual Studio. NET 7.1" name= "generator" >

<meta content= "C #" Name= "Code_language" >

<meta content= "JavaScript" name= "vs_defaultClientScript" >

<meta content= "http://schemas.microsoft.com/intellisense/ie5" name= "Vs_targetschema" >

</HEAD>

<body>

<form id= "Form1" method= "POST" runat= "Server" >

<table id= "Table1" style= "font-size:9pt" cellspacing= "1" cellpadding= "1" width= "450" align= "center"

border= "1" >

<TR>

<td><asp:datagrid id= "DATAGRID1" runat= "Server" allowpaging= "true" allowcustompaging= "true" width= "100%" >

<footerstyle font-size= "9pt" ></FooterStyle>


<pagerstyle visible= "False" font-size= "9pt" mode= "NumericPages" ></PagerStyle>

</asp:datagrid></TD>

</TR>

<TR>

<TD>

<table id= "Table2" style= "font-size:9pt" cellspacing= "1" cellpadding= "1" width= "100%"

align= "Center" border= "1" >

<TR>

&LT;TD style= "width:150px" ><asp:linkbutton id= "Lbtnfirst" runat= "Server" commandname= "First" > Home page </asp: Linkbutton>

<asp:linkbutton id= "Lbtnprev" runat= "Server" commandname= "Prev" > Prev </asp:linkbutton>

<asp:linkbutton id= "Lbtnnext" runat= "Server" Commandname= "Next" > Next </asp:linkbutton>

<asp:linkbutton id= "Lbtnlast" runat= "Server" Commandname= "last" > End </asp:linkbutton></TD>

<TD> <asp:literal id= "Ltlpageindex" runat= "Server" ></asp:literal> page total <asp:literal id= " Ltlpagecount "runat=" Server ></asp:literal> page

Per page <asp:literal id= "ltlpagesize" runat= "server" ></asp:Literal> strip <asp:literal id= "Ltlrecordcount" runat= "Server" ></asp:Literal> Bar

</TD>

</TR>

</TABLE>

</TD>

</TR>

</TABLE>

</form>

</body>

</HTML>



The Aspx.cs file code is as follows:

Using System;

Using System.Collections;

Using System.ComponentModel;

Using System.Data;

Using System.Drawing;

Using System.Web;

Using System.Web.SessionState;

Using System.Web.UI;

Using System.Web.UI.WebControls;

Using System.Web.UI.HtmlControls;

Using System.Data.SqlClient;

Using System.Configuration;



Namespace ZZ. Aspnetpaging

{

public class DataGridCustomPaging:System.Web.UI.Page

{

private int PageCount;

private int recordCount;



protected System.Web.UI.WebControls.LinkButton Lbtnfirst;

protected System.Web.UI.WebControls.LinkButton Lbtnprev;

protected System.Web.UI.WebControls.LinkButton Lbtnnext;

protected System.Web.UI.WebControls.LinkButton lbtnlast;

protected System.Web.UI.WebControls.Literal Ltlpageindex;

protected System.Web.UI.WebControls.Literal Ltlpagecount;

protected System.Web.UI.WebControls.Literal ltlpagesize;

protected System.Web.UI.WebControls.Literal Ltlrecordcount;

protected System.Web.UI.WebControls.DataGrid DataGrid1;



private void Page_Load (object sender, System.EventArgs e)

{

if (! Page.IsPostBack)

{

Datagriddatabind ();

}

}



Binding Data

private void Datagriddatabind ()

{

DataSet ds = Getcustomersdata (pageindex,pagesize,ref recordcount,ref PageCount);

This. Datagrid1.virtualitemcount = RecordCount;

This. DataGrid1.DataSource = ds;

This. Datagrid1.databind ();

Setpagingstate ();

}



Code generated #region the Web forms Designer

Override protected void OnInit (EventArgs e)

{

InitializeComponent ();

Base. OnInit (e);

}



private void InitializeComponent ()

{

This. Lbtnfirst.click + = new System.EventHandler (this. Lbtnnavigation_click);

This. Lbtnprev.click + = new System.EventHandler (this. Lbtnnavigation_click);

This. Lbtnnext.click + = new System.EventHandler (this. Lbtnnavigation_click);

This. Lbtnlast.click + = new System.EventHandler (this. Lbtnnavigation_click);

This. Load + = new System.EventHandler (this. Page_Load);

}

#endregion



private static DataSet getcustomersdata (int pageindex,int pagesize,ref int recordcount,ref int pagecount)

{

String connstring = configurationsettings.appsettings["ConnString"];

SqlConnection conn = new SqlConnection (connstring);

SqlCommand comm = new SqlCommand ("Getcustomersdatapage", conn);

Comm. Parameters.Add (New SqlParameter ("@PageIndex", SqlDbType.Int));

Comm. Parameters[0]. Value = PageIndex;

Comm. Parameters.Add (New SqlParameter ("@PageSize", SqlDbType.Int));

Comm. PARAMETERS[1]. Value = pageSize;

Comm. Parameters.Add (New SqlParameter ("@RecordCount", SqlDbType.Int));

Comm. PARAMETERS[2]. Direction = ParameterDirection.Output;

Comm. Parameters.Add (New SqlParameter ("@PageCount", SqlDbType.Int));

Comm. PARAMETERS[3]. Direction = ParameterDirection.Output;

Comm.commandtype = CommandType.StoredProcedure;

SqlDataAdapter dataAdapter = new SqlDataAdapter (comm);

DataSet ds = new DataSet ();

DataAdapter.Fill (DS);

RecordCount = (int) Comm. PARAMETERS[2]. Value;

PageCount = (int) Comm. PARAMETERS[3]. Value;

return DS;

}



private void Lbtnnavigation_click (object sender, System.EventArgs e)

{

LinkButton btn = (LinkButton) sender;

Switch (btn.commandname)

{

Case "a":

PageIndex = 0;

Break

Case "Prev"://if (PageIndex > 0)

PageIndex = PageIndex-1;

Break

Case "Next"://if (PageIndex < PageCount-1)

PageIndex = PageIndex + 1;

Break

Case "Last":

PageIndex = PageCount-1;

Break

}

Datagriddatabind ();

}



<summary>

Control the status of a navigation button or number

</summary>

public void Setpagingstate ()

{

if (PageCount <= 1)//(RecordCount <= PageSize)//less than or equal to one page

{

This. lbtnfirst.enabled = false;

This. lbtnprev.enabled = false;

This. lbtnnext.enabled = false;

This. lbtnlast.enabled = false;

}

else//More than one page

{

if (PageIndex = 0)//current is the first page

{

This. lbtnfirst.enabled = false;

This. lbtnprev.enabled = false;

This. Lbtnnext.enabled = true;

This. Lbtnlast.enabled = true;

}

else if (PageIndex = = PageCount-1)//current is last page

{

This. Lbtnfirst.enabled = true;

This. Lbtnprev.enabled = true;

This. lbtnnext.enabled = false;

This. lbtnlast.enabled = false;

}

else//Middle page

{

This. Lbtnfirst.enabled = true;

This. Lbtnprev.enabled = true;

This. Lbtnnext.enabled = true;

This. Lbtnlast.enabled = true;

}

}



This. Ltlpagesize.text = Pagesize.tostring ();

This. Ltlrecordcount.text = Recordcount.tostring ();

if (RecordCount = 0)

{

This. Ltlpagecount.text = "0";

This. Ltlpageindex.text = "0";

}

Else

{

This. Ltlpagecount.text = Pagecount.tostring ();

This. Ltlpageindex.text = (PageIndex + 1). ToString ();

}

}





public int PageCount

{

Get

{

return this. Datagrid1.pagecount;

}

}



public int PageSize

{

Get

{

return this. Datagrid1.pagesize;

}

}



public int PageIndex

{

Get

{

return this. Datagrid1.currentpageindex;

}

Set

{

This. Datagrid1.currentpageindex = value;

}

}



public int RecordCount

{

Get

{

return recordCount;

}

}

}

}



The above code is relatively simple, also do not need to analyze, if there is any good suggestions or problems can be in the blog message, very happy to communicate with you.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.