Custom datalist Paging

Source: Internet
Author: User

Main front-end code:
<Table width = "100%" border = "0" cellspacing = "0" cellpadding = "0">
<Tr>
<TD>
<Asp: datalist id = "datalist1" runat = "server" width = "65%" onitemdatabound = "datalist1_itemdatabound">
<Itemtemplate>
<Table border = "0" cellpadding = "0" cellspacing = "0" width = "100%">
<Tr>
<TD style = "width: 10px;"> </TD>
<TD Height = "31">
<Asp: linkbutton id = "lkbtitle" runat = "server" commandargument = '<% # eval ("ArticleID") %>'
TEXT = '<% # eval ("title") %>' font-size = "medium" forecolor = "blue" onclick = "lkbtitle_click"> </ASP: linkbutton>
</TD>
</Tr>
<Tr>
<TD style = "width: 10px;"> </TD>
<TD>
<Span>
<Asp: Label id = "labcount" runat = "server" text = '<% # eval ("content") %>'> </ASP: Label>
</Span> </TD>
</Tr>
<Tr>
<TD style = "width: 10px;"> </TD>
<TD align = "right" Height = "31">
<SPAN class = "style19"> release date-<asp: Label id = "labdate" runat = "server" text = '<% # eval ("updatetime ") %> '> </ASP: Label>-</span> </TD>
</Tr>
</Table>
</Itemtemplate>
</ASP: datalist>
</TD>
</Tr>
</Table>
<Table width = "100%" border = "0" cellspacing = "0" cellpadding = "0">
<Tr>
<TD Height = "20" align = "center">
<Div id = "pageinfo" runat = "server"> </div>
</TD>
</Tr>
</Table>

Main background code:

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 fps_newsresult: system. Web. UI. Page
{
// The total number of records
Int totalcountrecord;
// Number of entries displayed per page
Int pageitem = 10;
// Current page number
Int currentpage = 1;

Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
If (request. querystring ["page"]! = NULL)
{
Currentpage = convert. toint32 (request. querystring ["page"]);
}
Buildgrid ();
}
}

Public void buildgrid ()
{
Tring sqlstr = "select ArticleID, title, content, updatetime from article order by updatetime DESC ";
Sqlconnection mycoon = new sqlconnection (configurationmanager. connectionstrings ["myconnectionstring"]. connectionstring );
Sqldataadapter da = new sqldataadapter (sqlstr, mycoon );
Dataset DS = new dataset ();
Int startrecord = (currentpage-1) * pageitem;
Da. Fill (DS, startrecord, pageitem, "article ");
Datalist1.datasource = Ds. Tables ["article"]. defaultview;
Datalist1.databind ();
Sqlcommand mycmd = new sqlcommand ();
Mycmd. commandtext = "select count (*) from article ";
Mycmd. Connection = mycoon;
Mycoon. open ();
// Obtain the total number of records
Totalcountrecord = (INT) mycmd. executescalar ();
Mycoon. Close ();
Buildpagers ();
}

Public void buildpagers ()
{
// Offset
Int step = 10;
Int leftnum = 0;
Int rightnum = 0;
String pageurl = "*. aspx"; // * set it to the current page
Int pagecount = (INT) math. Ceiling (double) (totalcountrecord)/pageitem );
If (currentpage-step <1)
{
Leftnum = 1;
}
Else
{
Leftnum = currentpage-step;
}

If (currentpage + Step> pagecount)
{
Rightnum = pagecount;
}
Else
{
Rightnum = currentpage + step;
}
String output = "";
For (INT I = leftnum; I <= rightnum; I ++)
{
If (I = currentpage)
{
Output + = "<font color = Red>" + I. tostring () + "</font> ";
}
Else
{
Output + = "<a href = '" + pageurl + "? Key = "+ key +" & page = "+ I. tostring () +" '> ["+ I. tostring () +"] </a> ";
}
}
If (currentpage> 1)
{
Output = "<a href = '" + pageurl + "? Key = "+ key +" & page = "+ (currentpage-1) +" '> previous page </a> "+ output;
}

If (currentpage <pagecount)
{
Output + = "<a href = '" + pageurl + "? Key = "+ key +" & page = "+ (currentpage + 1) +" '> next page </a> ";
}

Pageinfo. innerhtml = output;
}
}

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.