Add home, last page, and status features for the DataGrid's own pagination

Source: Internet
Author: User
The datagrid| paging DataGrid provides paging functionality, but it seems to be limited, but we can get the status through some of the DataGrid's properties and add the Home page, last feature button. There is no custom paging feature for the DataGrid, and it's good to manage paging by the DataGrid if it's not very efficient in the case of speed efficiency, and the cost is to get the whole data out and then delete the data from the specified page. The advantage is that the development speed is fast, do not need to write paging stored procedures. The example in this article uses the Northwind database in SQL Server. The running interface is as follows:



For the foreground display interface, I put a DataGrid, four LinkButton buttons, and four literal to display the record status.

The rest is to use the table to locate.

You need to set the AllowPaging property of the DataGrid to True and set the AllowCustomPaging property bit False (the default is False), and set the Visible property of PagerStyle to False so that the foreground does not appear.

The front desk code is as follows:

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

<! 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" pagesize= "5" width= "100%" allowpaging= "True" >


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

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

</asp:datagrid></TD>

</TR>

</TABLE>

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

border= "1" >

<TR>

&LT;TD style= "WIDTH:207PX" >

<asp:linkbutton id= "Lbtnfirst" runat= "Server" commandname= "First" > Home </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> section

<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 total

<asp:literal id= "Ltlrecordcount" runat= "Server" ></asp:literal> Bar

</TD>

</TR>

</TABLE>

</form>

</body>

</HTML>

Background CS file code, datagridpaging class from the System.Web.UI.Page inheritance, in the data binding time to pay attention to the situation without data (0 pages), and when the number of pages to avoid the foreground is reverse page caused by pages.

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 DataGridPaging:System.Web.UI.Page

{

private static string connstring = configurationsettings.appsettings["ConnString"];

private int recordCount;

private int PageCount;



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 ();

RecordCount = ds. Tables[0]. Rows.Count;

Get the current number of pages

PageCount = (int) math.ceiling (RecordCount * 1.0/pagesize);

Avoid CurrentPageIndex > PageCount errors when records are from there to none, and when an inverse page has been made

if (RecordCount ==0)

{

This. Datagrid1.currentpageindex = 0;

}

else if (this. Datagrid1.currentpageindex >= PageCount)

{

This. Datagrid1.currentpageindex = pageCount-1;

}

This. DataGrid1.DataSource = ds;

This. Datagrid1.databind ();

Navigationstatechange ();

}



Code generated #region the Web forms Designer

Override protected void OnInit (EventArgs e)

{

//

CodeGen: This call is required for the ASP.net Web forms Designer.

//

InitializeComponent ();

Base. OnInit (e);

}



<summary>

Designer supports required methods-do not use the Code editor to modify

The contents of this method.

</summary>

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 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 ();

}

Data binding

public static DataSet Getcustomersdata ()

{

SqlConnection conn = new SqlConnection (connstring);

String sqlstr = "Select CustomerID, Companyname,address,phone from Customers";

SqlCommand comm = new SqlCommand (SQLSTR, conn);

SqlDataAdapter dataAdapter = new SqlDataAdapter (comm);

DataSet ds = new DataSet ();

DataAdapter.Fill (DS);

return DS;

}



<summary>

Control the status of a navigation button or number

</summary>

public void Navigationstatechange ()

{

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;

}



}

if (RecordCount = 0)/////Datagrid.pagecount 1 pages when no record is found

This. Ltlpagecount.text = "0";

Else

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

if (RecordCount = 0)

This. Ltlpageindex.text = "0";

Else

This. Ltlpageindex.text = (PageIndex + 1). ToString ()///In the number of pages, foreground display page plus 1

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

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

}





Total pages

public int PageCount

{

Get{return this. Datagrid1.pagecount;}

}

Page size

public int PageSize

{

Get{return this. Datagrid1.pagesize;}

}

Page index, starting from scratch

public int PageIndex

{

Get{return this. Datagrid1.currentpageindex;}

Set{this. Datagrid1.currentpageindex = value;}

}

Total Records

public int RecordCount

{

Get{return RecordCount;}

Set{recordcount = value;}

}

}

}



It is recommended that you use the custom paging feature of the DataGrid if you need to pursue execution efficiencies and have a large amount of data. The stored procedure executes only one page. If you have any good suggestions or find problems, please leave a message on the blog.


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.