Anyone who has used ASP to develop web programs knows that paging is a headache for ASP programmers. Here I am talking about the paging technology easily created by Asp.net.
The first step is to create a project in vs.net. I won't talk about it anymore. You can do it yourself. Here I will mainly post important code.
1. webform1.aspx
<% @ Page Language = "C #" codebehind = "webform1.aspx. cs" autoeventwireup = "false" inherits = "Page. webform1" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> webform1 </title>
<Meta content = "Microsoft Visual maxcompute 7.0" 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 ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Table align = "center">
<Tr>
<TD style = "height: pixel PX" colspan = 2> <asp: dataGrid id = "datagrid1" runat = "server" pagesize = "5" font-size = "8pt" font-names = "verdana" borderwidth = "1px" backcolor = "# ffe0c0 "bordercolor =" # ffc080 "horizontalalign =" center "cellpadding =" 3 "allowpaging =" true "Height =" 152px "width =" 700px ">
<Alternatingitemstyle backcolor = "linen">
</Alternatingitemstyle>
<Itemstyle verticalalign = "bottom">
</Itemstyle>
<Headerstyle horizontalalign = "center" verticalalign = "bottom" backcolor = "# ff8000">
</Headerstyle>
<Footerstyle horizontalalign = "Justify" backcolor = "white">
</Footerstyle>
<Pagerstyle horizontalalign = "right" forecolor = "red" mode = "numericpages">
</Pagerstyle>
</ASP: DataGrid> </TD>
</Tr>
<Tr align = "center">
<TD align = left> <font size = 2> total </font> & nbsp; <asp: label id = "lblallcount" runat = server font-size = "8pt" forecolor = "# 0000ff" font-names = "verdana"> </ASP: Label> & nbsp; <font size = 2> record </font>
<Font size = "2"> total </font> & nbsp; <asp: label id = "lblpagecount" runat = "server" font-size = "8pt" forecolor = "# 0000ff" font-names = "verdana"> </ASP: label> & nbsp; <font size = "2"> page </font>
<Font size = "2"> NO. </font> & nbsp; <asp: label id = "lblcurrentindex" runat = "server" font-size = "8pt" forecolor = "# 0000ff" font-names = "verdana"> </ASP: label> & nbsp; <font size = "2"> page </font> </TD>
<TD align = right> <asp: linkbutton id = "first" runat = "server" font-size = "8pt" forecolor = "# 3300ff" commandargument = "first" font-names = "verdana"> </asp: linkbutton>
<Asp: linkbutton id = "Prev" font-size = "8pt" forecolor = "# 3300ff" commandargument = "Prev" runat = "server" font-names = "verdana"> </asp: linkbutton>
<Asp: linkbutton id = "Next" font-size = "8pt" forecolor = "# 3300ff" commandargument = "Next" runat = "server" font-names = "verdana"> </asp: linkbutton>
<Asp: linkbutton id = "last" font-size = "8pt" forecolor = "# 3300ff" commandargument = "last" runat = "server" font-names = "verdana"> </asp: linkbutton>
<Font size = "2"> jump to the </font> <asp: textbox id = "to" runat = "server" columns = "1"/> <font size = "2"> page </font> <asp: button id = "go" text = "go" runat = "server" backcolor = "whitesmoke" font-names = "verdana"/>
</TD>
</Tr>
</Table>
</Form>
</Body>
</Html>
2. webform1.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 page
{
/// <Summary>
/// Summary of webform1.
/// </Summary>
Public class webform1: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. Label lblpagecount;
Protected system. Web. UI. webcontrols. Label lblcurrentindex;
Protected system. Web. UI. webcontrols. linkbutton first;
Protected system. Web. UI. webcontrols. linkbutton Prev;
Protected system. Web. UI. webcontrols. linkbutton next;
Protected system. Web. UI. webcontrols. DataGrid datagrid1;
Protected system. Web. UI. webcontrols. textbox;
Protected system. Web. UI. webcontrols. Button go;
Protected system. Web. UI. webcontrols. Label lblallcount;
Protected system. Web. UI. webcontrols. linkbutton last;
Void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
First. Text = "Homepage ";
Prev. Text = "Previous Page ";
Next. Text = "next page ";
Last. Text = "last page ";
If (! Ispostback)
{
Bindgrid ();
}
}
# Region web form designer generated code
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. Maid + = new system. Web. UI. webcontrols. Maid (this. page_changed );
This. First. Click + = new system. eventhandler (this. btnclick );
This. Prev. Click + = new system. eventhandler (this. btnclick );
This. Next. Click + = new system. eventhandler (this. btnclick );
This. Last. Click + = new system. eventhandler (this. btnclick );
This. Go. Click + = new system. eventhandler (this. goclick );
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
Void page_changed (Object source, system. Web. UI. webcontrols. datagridpagechangedeventargs E)
{
Datagrid1.currentpageindex = E. newpageindex;
Bindgrid ();
}
Void bindgrid ()
{
Sqlconnection Cn = new sqlconnection ("Server = localhost; database = pubs; uid = sa; Pwd = ");
CN. open ();
Sqldataadapter da = new sqldataadapter ("select * from authors", CN );
Dataset DS = new dataset ();
Da. Fill (DS, "Authors ");
Datagrid1.datasource = Ds. Tables ["Authors"]. defaultview;
Datagrid1.databind ();
CN. Close ();
Showstate ();
}
Void showstate ()
{
Sqlconnection Cn = new sqlconnection ("Server = localhost; database = pubs; uid = sa; Pwd = ");
CN. open ();
Sqldataadapter da = new sqldataadapter ("select * from authors", CN );
Dataset DS = new dataset ();
Da. Fill (DS, "Authors ");
Datatable dt = Ds. Tables ["Authors"];
Int COUNT = DT. Rows. count;
Lblallcount. Text = count. tostring ();
Lblpagecount. Text = maid. tostring ();
Lblcurrentindex. Text = (maid + 1). tostring ();
}
Void btnclick (Object sender, system. eventargs E)
{
String Arg = (linkbutton) sender). commandargument;
Switch (ARG)
{
Case ("first "):
Datagrid1.currentpageindex = 0;
Break;
Case ("Prev "):
If (maid> 0)
Datagrid1.currentpageindex --;
Break;
Case ("Next "):
If (maid-1 ))
Datagrid1.currentpageindex ++;
Break;
Case ("last "):
Maid-1;
Break;
Default:
Datagrid1.currentpageindex = convert. toint32 (ARG );
Break;
}
Bindgrid ();
}
Private void goclick (Object sender, system. eventargs E)
{
If (to. Text. Trim ()! = "")
{
Int Index = int32.parse (to. Text. Trim ()-1;
If (index> = 0 & index <datagrid1.pagecount)
{
Datagrid1.currentpageindex = index;
}
}
Bindgrid ();
}
}
}
Now that the paging web program is complete, try it.