Asp. NET tip: DataGrid Traditional Paging mode

Source: Internet
Author: User
Tags count sql tostring visual studio

This paging is similar to the traditional ASP paging approach.

datagridpage.aspx

The following are the referenced contents:
<%@ Page language= "C #" codebehind= "DataGridPage.aspx.cs" autoeventwireup= "false" inherits= "Netcrm.datagridpage"% >
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title>DataGridPage</title>
<meta name= "generator" content= "Microsoft Visual Studio. NET 7.1" >
<meta name= "Code_language" content= "C #" >
<meta name= "vs_defaultClientScript" content= "JavaScript" >
<meta name= "vs_targetschema" content= "http://schemas.microsoft.com/intellisense/ie5" >
</HEAD>
<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
<asp:datagrid id= "DATAGRID1" runat= "Server" alternatingitemstyle-backcolor= "#eeeeee" headerstyle-backcolor= "# Aaaadd "
Font-size= "8pt" font-name= "Verdana" cellpadding= "3" borderwidth= "1px" bordercolor= "BLACK"
Pagerstyle-horizontalalign= "Right" pagerstyle-mode= "NumericPages"
Pagesize= "5" font-names= "Verdana" width= "100%" >
<alternatingitemstyle backcolor= "#EEEEEE" ></AlternatingItemStyle>
<pagerstyle horizontalalign= "Right" mode= "NumericPages" ></PagerStyle>
</asp:datagrid>
</form>
<table cellspacing= "0" cellpadding= "1" width= "100%" bgcolor= "#aaaadd" border= "0" >
<TBODY>
<TR>
<TD>
<table cellspacing= "0" cellpadding= "4" width= "100%" bgcolor= "#fef8e2" border= "0" >
<TBODY>
<TR>
&LT;TD class= "M" noWrap align= "center" ><asp:literal id= "Literal1" runat= "Server" ></asp:literal></ Td>
</TR>
<TR>
&LT;TD class= "C" noWrap align= "center" ><asp:literal id= "Literal2" runat= "Server" ></asp:literal></ Td>
</TR>
</TBODY>
</TABLE>
</TD>
</TR>
</TBODY>
</TABLE>
</body>
</HTML>

DataGridPage.aspx.cs

The following are the referenced contents:
Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Data.SqlClient;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Namespace Netcrm
{
<summary>
Summary description of the datagridpage.
</summary>
public class DataGridPage:System.Web.UI.Page
{
protected System.Web.UI.WebControls.Literal Literal1;
protected System.Web.UI.WebControls.Literal Literal2;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
if (! IsPostBack)
{
Bindgrid ();
}
}
private void Bindgrid ()
{
String connstring = "server=.;D Atabase=northwind; User Id=sa; password=; ";
String Sql= "Select * from Orders";
SqlConnection conn = new SqlConnection (connstring);
Conn. Open ();
DataSet ds = new DataSet ();
SqlDataAdapter sqladapter = new SqlDataAdapter (Sql,conn);
Sqladapter.fill (ds, "users");
DataView DataView = new DataView ();
DataView = ds. Tables[0]. DefaultView;
DataGrid1.DataSource = ds. Tables[0]. DefaultView;
Datagrid1.databind ();
String Cpage;
int pageSize = 10;
int currentpage;
int PageCount;
int numresults = 0;
if (request.querystring["page"]==null)
{
Cpage= "1";
}
Else
{
cpage=request.querystring["Page"]. ToString ();
}
Try
{
CurrentPage = Int32.Parse (cpage);
}
Catch
{
CurrentPage = 1;
}
numresults = 0;
int start = (int) ((currentPage-1) * pageSize);
int to = (int) (currentpage * pageSize);
If (start <= 0) start = 0;
Numresults = DataView. Count;
int a1=0;
PageCount = Math.divrem (numresults,pagesize,out a1);
if (a1>0)
{
pagecount++;
}
if (Currentpage>pagecount | | currentpage<=0)
{
CurrentPage = 1;
}
if (Currentpage==pagecount)
{
to = DataView. Count;
}
Create one DataTable with one column.
DataTable myTable = new DataTable ("MyTable");
MyTable = DataView. Table.clone ();
DataColumn colItem1 = new DataColumn ("name", Type.GetType ("System.String"));
DataColumn colItem2 = new DataColumn ("Types", Type.GetType ("System.String"));
DataColumn colItem3 = new DataColumn ("Vendor", Type.GetType ("System.String"));
MYTABLE.COLUMNS.ADD (COLITEM1);
MYTABLE.COLUMNS.ADD (COLITEM2);
MYTABLE.COLUMNS.ADD (COLITEM3);
Add row
DataRow NewRow;
for (int i=start;i<numresults;i++)
{
if (i<to)
{
NewRow = Mytable.newrow ();
for (int k=0;k<dataview. table.columns.count;k++)
{
Newrow[k] = DataView. TABLE.ROWS[I][K];
}
MYTABLE.ROWS.ADD (NewRow);
}
}
Mytable.acceptchanges ();
DataView Resultdataview = new DataView (myTable);
DataGrid1.DataSource = Resultdataview;
Datagrid1.databind ();
<summary>
Generates a pager bar.
</summary>
String strnav = "";
int endpage;
if (currentpage>1)
{
Strnav + = "<a href= '? page=" + (currentPage-1). ToString () + "' > Prev </a>";
}
if (currentpage>11)
{
Strnav + = "<a href= '" page=1 ' >1</a> ... ";
}
if (pagecount>currentpage+10)
{
EndPage = currentpage+10;
}
Else
{
EndPage = PageCount;
}
for (int i=currentpage-10;i<endpage+1;i++)
{
if (i>=1)
{
if (i==currentpage)
{
Strnav + = "<font color= #990000 ><strong>" + i.tostring () + "</strong></font>";
}
Else
{
Strnav + = "<a href= ' page=" + i.tostring () + "' >" + i.tostring () + "</a>";
}
}
}
if ((currentpage+10) <pagecount)
{
Strnav + = "... <a href= '? page=" + pagecount.tostring () + "' >" + pagecount.tostring () + "</a>";
}
if (Currentpage<pagecount)
{
Strnav + = "<a href= '? page=" + (currentpage+1). ToString () + "' > next page </a>";
}
Literal1.text = Strnav;
Literal2.text = "Total" + numresults.tostring () + "article supply information, currently showing the first" +
(start+1). ToString () + "-" + to. ToString () + "bar, total" + pagecount.tostring () + "page";
}
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. Load + = new System.EventHandler (this. Page_Load);
}
#endregion
}
}



Related Article

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.