Cs:
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;
Namespace Webedtor
{
/// <Summary>
/// Summary of TestFenye.
/// </Summary>
Public class TestFenye: System. Web. UI. Page
{
Protected System. Web. UI. WebControls. DataGrid DataGrid1;
Private SqlConnection con = new SqlConnection ("server = 192.168.1.188; uid = sa; pwd =; database = northwind ");
Protected System. Web. UI. WebControls. TextBox TextBox1;
Protected System. Web. UI. WebControls. Button Button1;
Protected System. Web. UI. WebControls. Button Button2;
Protected System. Web. UI. WebControls. Label lblcount;
Protected System. Web. UI. WebControls. PlaceHolder ph;
Protected System. Web. UI. WebControls. Label lblCur;
Protected System. Web. UI. WebControls. LinkButton LinkButton1;
Protected System. Web. UI. WebControls. LinkButton LinkButton2;
Protected System. Web. UI. WebControls. Label lblPagecount;
Private SqlCommand cmd = null;
Private const int PageCount = 20;
Private void Page_Load (object sender, System. EventArgs e)
{
If (! This. IsPostBack)
{
BindAutoPage (0 );
Lblcount. Text = GetAllCount (). ToString ();
Mode ();
}
Feye ();
}
Public void Feye ()
{
Int count = int. Parse (lblPagecount. Text );
LinkButton lb;
Ph. Controls. Clear ();
If (count <= 10)
{
For (int I = 0; I <count; I ++)
{
Lb = new LinkButton ();
Lb. Text = "[" + (I + 1). ToString () + "]";
Lb. ID = "lbl" + (I + 1). ToString ();
Lb. CommandName = I. ToString ();
Lb. Command + = new System. Web. UI. WebControls. CommandEventHandler (this. PageLbFenye );
Ph. Controls. Add (lb );
}
}
Else
{
Int cur = int. Parse (lblCur. Text );
If (cur> = count-6)
{
For (int I = count-10; I <count; I ++)
{
Lb = new LinkButton ();
Lb. Text = "[" + (I + 1). ToString () + "]";
Lb. ID = "lbl" + (I + 1). ToString ();
Lb. CommandName = I. ToString ();
Lb. Command + = new System. Web. UI. WebControls. CommandEventHandler (this. PageLbFenye );
Ph. Controls. Add (lb );
}
}
Else
{
If (cur <= 4)
{
For (int I = 0; I <10; I ++)
{
Lb = new LinkButton ();
Lb. Text = "[" + (I + 1). ToString () + "]";
Lb. ID = "lbl" + (I + 1). ToString ();
Lb. CommandName = I. ToString ();
Lb. Command + = new System. Web. UI. WebControls. CommandEventHandler (this. PageLbFenye );
Ph. Controls. Add (lb );
}
}
Else
{
For (int I = cur-5; I <cur + 5; I ++)
{
Lb = new LinkButton ();
Lb. Text = "[" + (I + 1). ToString () + "]";
Lb. ID = "lbl" + (I + 1). ToString ();
Lb. CommandName = I. ToString ();
Lb. Command + = new System. Web. UI. WebControls. CommandEventHandler (this. PageLbFenye );
Ph. Controls. Add (lb );
}
}
}
}
}
Public void PageLbFenye (Object sender, CommandEventArgs e)
{
Int pageindex = int. Parse (e. CommandName );
LblCur. Text = (int. Parse (e. CommandName) + 1). ToString ();
BindAutoPage (pageindex );
Feye ();
}
Public void Mode ()
{
Int count = int. Parse (lblcount. Text );
Int countPage = 0;
If (count % PageCount = 0)
{
CountPage = count/PageCount;
}
Else
{
CountPage = count/PageCount + 1;
}
LblPagecount. Text = countPage. ToString ();
}
Public int GetAllCount ()
{
Cmd = con. CreateCommand ();
Cmd. Connection = con;
Cmd. CommandText = "test_getCount ";
Cmd. CommandType = CommandType. StoredProcedure;
SqlParameter [] para = {
New SqlParameter ("@ tablename", SqlDbType. VarChar, 50 ),
New SqlParameter ("@ where", SqlDbType. VarChar, 50 ),
};
Para [0]. Value = "Orders ";
Para [1]. Value = "";
Foreach (SqlParameter paras in para)
{
Cmd. Parameters. Add (paras );
}
Cmd. Connection. Open ();
// Cmd. ExecuteNonQuery ();
DataSet ds = new DataSet ();
SqlDataAdapter sda = new SqlDataAdapter (cmd );
Sda. Fill (ds );
Cmd. Connection. Close ();
Return int. Parse (ds. Tables [0]. Rows [0] [0]. ToString ());
}
Public void bindAutoPage (int pageIndex)
{
If (pageIndex = 0)
{
Button1.Enabled = false;
LinkButton1.Enabled = false;
}
Else
{
Button1.Enabled = true;
LinkButton1.Enabled = true;
}
Try
{
If (pageIndex = int. Parse (lblPagecount. Text)-1)
{
Button2.Enabled = false;
LinkButton2.Enabled = false;
}
Else
{
Button2.Enabled = true;
LinkButton2.Enabled = true;
}
}
Catch {}
Cmd = con. CreateCommand ();
Cmd. Connection = con;
Cmd. CommandText = "Test_autoPage ";
Cmd. CommandType = CommandType. StoredProcedure;
LblCur. Text = (pageIndex + 1). ToString ();
SqlParameter [] para = {
New SqlParameter ("@ tablekey", SqlDbType. VarChar, 20 ),
New SqlParameter ("@ tablename", SqlDbType. VarChar, 50 ),
New SqlParameter ("@ pageindex", SqlDbType. Int, 4 ),
New SqlParameter ("@ pagesize", SqlDbType. Int, 4 ),
New SqlParameter ("@ where", SqlDbType. VarChar, 500)
};
Para [0]. Value = "OrderID ";
Para [1]. Value = "Orders ";
Para [2]. Value = pageIndex;
Para [3]. Value = PageCount;
Para [4]. Value = "";
Foreach (SqlParameter paras in para)
{
Cmd. Parameters. Add (paras );
}
Cmd. Connection. Open ();
// Cmd. ExecuteNonQuery ();
DataSet ds = new DataSet ();
SqlDataAdapter sda = new SqlDataAdapter (cmd );
Sda. Fill (ds );
DataGrid1.DataSource = ds. Tables [0]. DefaultView;
DataGrid1.DataBind ();
Cmd. Connection. Close ();
}
# Code generated by region Web Form Designer
Override protected void OnInit (EventArgs e)
{
//
// CODEGEN: This call is required by the ASP. NET Web form designer.
//
InitializeComponent ();
Base. OnInit (e );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. LinkButton1.Click + = new System. EventHandler (this. linkbutton#click );
This. Button1.Click + = new System. EventHandler (this. button#click );
This. Button2.Click + = new System. EventHandler (this. Button2_Click );
This. LinkButton2.Click + = new System. EventHandler (this. LinkButton2_Click );
This. TextBox1.TextChanged + = new System. EventHandler (this. textbox#textchanged );
This. Load + = new System. EventHandler (this. Page_Load );
}
# Endregion
Private void LinkButton3_Click (object sender, System. EventArgs e)
{
}
Private void linkbutton#click (object sender, System. EventArgs e)
{
BindAutoPage (0 );
Feye ();
}
Private void LinkButton2_Click (object sender, System. EventArgs e)
{
BindAutoPage (int. Parse (lblPagecount. Text)-1 );
Feye ();
}
Private void button#click (object sender, System. EventArgs e)
{
Int pagecur = int. Parse (lblCur. Text );
If (pagecur-1! = 0)
{
BindAutoPage (pagecur-2 );
}
Else
{
BindAutoPage (0 );
}
Feye ();
}
Private void Button2_Click (object sender, System. EventArgs e)
{
Int pagecur = int. Parse (lblCur. Text );
If (pagecur = int. Parse (lblPagecount. Text ))
{
BindAutoPage (int. Parse (lblPagecount. Text ));
}
Else
{
BindAutoPage (pagecur );
}
Feye ();
}
Private void TextBox1_TextChanged (object sender, System. EventArgs e)
{
Int pagecur = 0;
Try
{
Pagecur = int. Parse (TextBox1.Text );
BindAutoPage (pagecur-1 );
Feye ();
}
Catch
{
Response. Write ("<script> alert ('Enter the number'); </script> ");
}
}
}
}
HTML:
<% @ Page language = "c #" Codebehind = "TestFenye. aspx. cs" AutoEventWireup = "false" Inherits = "Webedtor. TestFenye" %>
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<Title> TestFenye </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">
<Script language = "javascript">
Var java = "";
Function aaa (va)
{
Java = va;
}
Function bb ()
{
Alert (java );
}
</Script>
</HEAD>
<Body MS_POSITIONING = "GridLayout">
<Form id = "Form1" method = "post" runat = "server">
<FONT face = "">
<Div>
<Div> total <asp: Label id = "lblcount" runat = "server"> </asp: Label> current record <asp: label id = "lblCur" runat = "server"> </asp: Label> page
Total
<Asp: Label id = "lblPagecount" runat = "server"> </asp: Label> page <INPUT type = "button" onclick = 'aaa (document. getElementById ("Ta "). value) 'value = "Button"> <INPUT type = "button" value = "Button" onclick = "bb () "> <input type = text id =" Ta "> </div>
<Div>
<Asp: LinkButton id = "LinkButton1" runat = "server"> homepage </asp: LinkButton> <asp: button id = "Button1" runat = "server" Width = "32px" Text = "<"> </asp: Button>
<Asp: PlaceHolder id = "ph" runat = "server"> </asp: PlaceHolder>
<Asp: Button id = "Button2" runat = "server" Width = "32px" Text = ">"> </asp: Button>
<Asp: LinkButton id = "LinkButton2" runat = "server"> last page </asp: LinkButton>
<Asp: TextBox id = "TextBox1" runat = "server" Width = "64px" AutoPostBack = "True"> </asp: TextBox>
</Div>
</Div>
<Asp: DataGrid id = "DataGrid1" EnableViewState = "False" style = "Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 80px"
Runat = "server" Height = "152px" Width = "704px" BorderColor = "# E7E7FF" BorderStyle = "None" BorderWidth = "1px"
BackColor = "White" CellPadding = "3" GridLines = "Horizontal" AutoGenerateColumns = "False">
<SelectedItemStyle Font-Bold = "True" ForeColor = "# F7F7F7" BackColor = "# 738A9C"> </SelectedItemStyle>
<AlternatingItemStyle BackColor = "# F7F7F7"> </AlternatingItemStyle>
<ItemStyle ForeColor = "# 4A3C8C" BackColor = "# E7E7FF"> </ItemStyle>
<HeaderStyle Font-Bold = "True" ForeColor = "# F7F7F7" BackColor = "# 4A3C8C"> </HeaderStyle>
<FooterStyle ForeColor = "# 4A3C8C" BackColor = "# B5C7DE"> </FooterStyle>
<Columns>
<Asp: BoundColumn DataField = "ORDERID" HeaderText = "ORDERID"> </asp: BoundColumn>
<Asp: BoundColumn DataField = "CustomerID" HeaderText = "CustomerID"> </asp: BoundColumn>
<Asp: BoundColumn DataField = "employeeID" HeaderText = "employeeID"> </asp: BoundColumn>
<Asp: BoundColumn DataField = "shipname" HeaderText = "shipname"> </asp: BoundColumn>
<Asp: BoundColumn DataField = "shipaddress" HeaderText = "shipaddress"> </asp: BoundColumn>
</Columns>
<PagerStyle HorizontalAlign = "Right" ForeColor = "# 4A3C8C" BackColor = "# E7E7FF" Mode = "NumericPages"> </PagerStyle>
</Asp: DataGrid> </FONT> </form>
</Body>
</HTML>
Stored Procedure:
CREATE proc Test_autoPage
@ Tablekey varchar (100 ),
@ Tablename varchar (100 ),
@ Pageindex int,
@ Pagesize int,
@ Where varchar (5000)
As
Declare @ SQL varchar (8000)
Set @ SQL = 'select top '+ str (@ pagesize) +' * from' + @ tablename + 'where 1 = 1' + @ where +
'And' + @ tablekey + 'not in (select top' + str (@ pageindex * @ pagesize) + ''+ @ tablekey + 'from' +
@ Tablename + 'where 1 = 1' + @ where + 'ORDER BY' + @ tablekey + ') order by' + @ tablekey
Set @ SQL = @ SQL + '; select count (*) from' + @ tablename + 'where 1 = 1' + @ where
Exec (@ SQL)
GO
Create proc test_getCount
@ Tablename varchar (100 ),
@ Where varchar (2000)
As
Declare @ SQL varchar (5000)
Set @ SQL = 'select count (*) from '+ @ tablename + 'where 1 = 1' + @ where
Exec (@ SQL)
I did not optimize the program I wrote. Hey, if you are interested, optimize it yourself ..