Pagination | example <% @ Page language= "C #"%>
<% @ Import namespace= "System.Data"%>
<% @ Import namespace= "System.Data.SqlClient"%>
<script language= "C #" runat= "Server" >
SqlConnection myconn;
int pagesize,recordcount,pagecount,currentpage;
public void Page_Load (Object Src,eventargs E)
{
Set pagesize
PageSize = 5;
Connection statement
String myConnString = "server=127.0.0.1;database=example;uid=sa;pwd=1234567";
myconn = new SqlConnection (myConnString);
MyConn.Open ();
First request execution
if (! Page.IsPostBack)
{
Listbind ();
currentpage = 0;
viewstate["PageIndex"] = 0;
Calculate how many records are in total
RecordCount = Calculaterecord ();
Lblrecordcount.text = Recordcount.tostring ();
Calculate how many pages are in total
PageCount = recordcount/pagesize;
Lblpagecount.text = Pagecount.tostring ();
viewstate["PageCount"] = PageCount;
}
}
Calculate how many records are in total
public int Calculaterecord ()
{
Int intcount;
String strcount = "SELECT count (*) as CO from TABLE1";
SqlCommand Mycomm = new SqlCommand (strcount,myconn);
SqlDataReader dr = Mycomm.executereader ();
if (Dr. Read ())
{
intcount = Int32.Parse (dr["co"). ToString ());
intcount= (int) dr["CO"];
}
Else
{
intcount = 0;
}
Dr. Close ();
Return intcount;
}
ICollection Createsource ()
{
int StartIndex;
Set the start address of the import
StartIndex = currentpage*pagesize;
String Strsel = "SELECT * from TABLE1";
DataSet ds = new DataSet ();
SqlDataAdapter myadapter = new SqlDataAdapter (Strsel,myconn);
Myadapter.fill (Ds,startindex,pagesize, "Score");
Return DS. tables["Score"]. DefaultView;
}
public void Listbind ()
{
Score. DataSource = Createsource ();
Score. DataBind ();
Lbnnextpage.enabled = true;
Lbnprevpage.enabled = true;
if (currentpage== (PageCount-1)) lbnnextpage.enabled = false;
if (currentpage==0) lbnprevpage.enabled = false;
Lblcurrentpage.text = (currentpage+1). ToString ();
}
public void Page_onclick (Object Sender,commandeventargs e)
{
currentpage = (int) viewstate["PageIndex"];
PageCount = (int) viewstate["PageCount"];
string cmd = E.commandname;
Judge Cmd to determine the direction of page flipping
Switch (CMD)
{
Case "Next":
if (currentpage< (PageCount-1)) currentpage++;
Break
Case "Prev":
if (currentpage>0) currentpage--;
Break
}
viewstate["PageIndex"] = currentpage;
Listbind ();
}
</script>
<title></title>
<body>
<form runat= "Server" >
Total <asp:label id= "Lblrecordcount" forecolor= "Red" runat= "server"/> Record
Currently <asp:label id= "Lblcurrentpage" forecolor= "Red" runat= "server"/>/<asp:label id= "Lblpagecount" ForeColor= "Red" runat= "Server"/> Page
<asp:datalist id= "Score" runat= "Server"
Headerstyle-backcolor= "#aaaadd"
Alternatingitemstyle-backcolor= "Gainsboro"
Edititemstyle-backcolor= "Yellow"
>
<ItemTemplate>
Name: <%# DataBinder.Eval (Container.DataItem, "title")%>
<asp:linkbutton id= "Btnselect" text= "Editing" commandname= "edit" runat= "Server"/>
</ItemTemplate>
</asp:DataList>
Total <%=PageCount%> Page
<asp:linkbutton id= "Lbnprevpage" text= "previous page" Commandname= "prev" oncommand= "Page_onclick" runat= "Server"/>
<asp:linkbutton id= "Lbnnextpage" text= "next page" Commandname= "Next" oncommand= "Page_onclick" runat= "Server"/>
</form>
</body>