DataGrid quick paging ~~!
Cs:
Using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Data. SqlClient;
Public partial class user_shiyan2: System. Web. UI. Page
{
Private static int intRecordCount; // total number of records
Private static int intPageCount; // total number of pages
Private static int intCurrentPageIndex; // current page number
Private DataView dv;
Protected void Page_Load (object sender, EventArgs e)
{
If (! Page. IsPostBack)
{
// Set the current page to 1, that is, display the record on the first page
IntCurrentPageIndex = 1;
// Set attributes
// DataGrid1.AllowPaging = "True ";
DataGrid1.AllowPaging = true;
// DataGrid1AllowCustomPaging = "True ";
DataGrid1.AllowCustomPaging = true;
// Bind data (Bind () is a custom method)
This. Bind (intCurrentPageIndex );
}
}
Private void Bind (int intCurrentPageIndex)
{
String strConn = ConfigurationSettings. receivettings ["Str_ SQL"]. ToString ();
// SQL statement that extracts only one page (10 records) of data at a time
String strSql = "select top 10 * from attorn where attorn_id not in (select top" + (intCurrentPageIndex-1) * 10 + "attorn_id from attorn )";
If (TextBox1.Text! = Null & TextBox1.Text! = "")
{
StrSql = "select top 10 * from attorn where attorn_id not in (select top" + (intCurrentPageIndex-1) * 10 + "attorn_id from attorn where [User_name] like '%" + TextBox1.Text + "%') and [User_name] like '%" + TextBox1.Text + "% '";
}
// Obtain the SQL statement for the total number of records
String strSqlCount = "select count (*) from attorn ";
If (TextBox1.Text! = Null & TextBox1.Text! = "")
{
StrSqlCount = "select count (*) from attorn where [User_name] like '%" + TextBox1.Text + "% '";
}
// Establish a connection with the data
SqlConnection dbConnection = new SqlConnection (strConn );
DbConnection. Open ();
// Create an instance for the adapter and fill in the data
SqlDataAdapter dsAdapter = new SqlDataAdapter (strSql, dbConnection );
DataSet ds = new DataSet ();
DsAdapter. Fill (ds );
Dv = ds. Tables [0]. DefaultView;
// Obtain the total number of records
SqlCommand cmd = new SqlCommand (strSqlCount, dbConnection );
IntRecordCount = (Int32) cmd. ExecuteScalar ();
// Close the connection
DbConnection. Close ();
// Calculate the total number of pages
Double dblRecordCount;
DblRecordCount = System. Convert. ToDouble (intRecordCount );
IntPageCount = (Int32) Math. Ceiling (dblRecordCount/10 );
Label1.Text = intCurrentPageIndex. ToString ();
Label2.Text = intPageCount. ToString ();
// Bind data
DataGrid1.DataSource = dv;
DataGrid1.DataBind ();
}
Protected void linkbutton#click (object sender, EventArgs e)
{
IntCurrentPageIndex = 1;
// Rebind data
This. Bind (intCurrentPageIndex );
}
Protected void LinkButton2_Click (object sender, EventArgs e)
{
IntCurrentPageIndex = intCurrentPageIndex-1;
If (intCurrentPageIndex <= 0)
{
IntCurrentPageIndex = 1;
}
// Rebind data
This. Bind (intCurrentPageIndex );
}
Protected void LinkButton3_Click (object sender, EventArgs e)
{
IntCurrentPageIndex = intCurrentPageIndex + 1;
If (intCurrentPageIndex> intPageCount)
{
IntCurrentPageIndex = intPageCount;
}
// Rebind data
This. Bind (intCurrentPageIndex );
}
Protected void LinkButton4_Click (object sender, EventArgs e)
{
IntCurrentPageIndex = intPageCount;
// Rebind data
This. Bind (intCurrentPageIndex );
}
Protected void button#click (object sender, EventArgs e)
{
IntCurrentPageIndex = 1;
// Rebind data
This. Bind (intCurrentPageIndex );
}
}
Aspx:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "shiyan2.aspx. cs" Inherits = "user_shiyan2" %>
No title page