. NET Paging Source Example

Source: Internet
Author: User
Tags prev tostring
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>



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.