Implementing pagination for a DataList control

Source: Internet
Author: User
Tags eval php and prev tostring visual studio
Pagination | Control DataList controls are powerful, he supports selection, editing, and implementation is simple, but the most frustrating thing is that it does not have the ability to page pagination like the DataGrid control, so a good control cannot be paged!!! It's a really frustrating thing to do.
However, just DataList does not provide the built-in paging functionality, but does not mean that we can not use the DataList control to implement pagination, since it does not give me paging functionality, it had to do it myself.
The following is all the original code, in fact, the method used in PHP and the page is similar, but here is the combination of DataAdapter and dataset, rather than PHP in the SQL statement directly to fix.



Default.aspx file:
<%@ Page language= "C #" codebehind= "Default.aspx.cs" autoeventwireup= "false" inherits= "Guestbook._default"%>
<HTML>
<HEAD>
<title> China BS NET Guestbook </title>
<HTML>
<HEAD>
<title>datalist Paging </title>
<meta name= "generator" content= "Microsoft Visual Studio 7.0" >
<meta name= "Code_language" content= "C #" >
<meta name= "vs_defaultClientScript" content= "JavaScript" >
</HEAD>
<body ms_positioning= "GridLayout" >
<form method= "POST" action= "default.aspx" runat= "Server" id= "Form1" >
<table width= "100%" border= "0" cellpadding= "2" cellspacing= "0" align= "center" >
<tr>
&LT;TD align= "Middle" >
<asp:datalist id= "Myguest" repeatcolumns= "1" runat= "Server" >
<ItemTemplate>
<table class= "Gueststyleline" border= "0" cellpadding= "0" cellspacing= "1" align= "center" width= "610" >
<tr>
<td>
<table class= "Gueststyleline" border= "0" cellpadding= "0" cellspacing= "1" align= "center" width= "610" >
<tr>
<td>
<table width= "100%" border= "0" cellpadding= "6" cellspacing= "0" class= "GUESTSTYLEBG" align= "Center" >
<tr>
&LT;TD width= "112" align= "center" height= "153" valign= "Top" >
<table width= "100%" border= "0" cellspacing= "0" cellpadding= "0" height= "100%" class= "GUESTSTYLEBG" >
<tr>
<td>
<table border= "0" cellspacing= "0" cellpadding= "0" align= "center" height= ">"
<tr>
&LT;TD class= "FILTER" >
<div align= "center" ><font style= "left:0px; width:100%; Word-wrap:break-word; 130; 0PT "><%# DataBinder.Eval (Container.DataItem," name ")%></font></div>
</td>
</tr>
</table>
<div align= "center" ><font style= "left:0px; width:100%; Word-wrap:break-word; 130; 0PT "> "width=" height= "" align= "Middle" ></font><br>
<br>

<%# DataBinder.Eval (Container.DataItem, "sex")%>
★</div>
</td>
</tr>
</table>
</td>
&LT;TD align= "Center" height= "153" width= "2" >
<table class= "Gueststyleline" width= "1" height= "100%" border= "0" cellpadding= "0" cellspacing= "0" >
<tr>
&LT;TD width= "1" ></td>
</tr>
</table>
</td>
&LT;TD height= "valign=" "Top" width= "463" >
<table width= "100%" border= "0" cellspacing= "0" cellpadding= "0" class= "GUESTSTYLEBG" style= "table-layout:fixed" Height= ">"
<tr>
&LT;TD height= "width=" 310 >:<%# DataBinder.Eval (Container.DataItem," caption ")%><br>
</td>
&LT;TD width= "151" height= "><font style=" 0pt "> :<%# DataBinder.Eval (Container.DataItem," postdate ")% ></font></td>
</tr>
</table>
<table width= "100%" border= "0" cellpadding= "5" cellspacing= "0" class= "GUESTSTYLEBG" height= "45%" style= " Table-layout:fixed ">
<tr>
&LT;TD valign= "Top" width= "align=" "Center" >
</td>
&LT;TD valign= "Top" width= "94%" style= "left:0px"; width:100%; Word-wrap:break-word; 130; 0PT ">
<%# DataBinder.Eval (Container.DataItem, "content")%>
</td>
</tr>
</table>
<table width= "100%" border= "0" cellpadding= "4" cellspacing= "0" >
<tr>
&LT;TD valign= "Top" >
<a href= "<%# DataBinder.Eval (Container.DataItem," homepage ")%>" target= "_blank" ></a>
<a href= "http://www.chinagz.net/ip/ipsearch.cgi?ip=<%# DataBinder.Eval (Container.DataItem," IP ")%>" target= "_blank" ></a>
<a href= "http://search.tencent.com/cgi-bin/friend/user_show_info?ln=<%# DataBinder.Eval (Container.DataItem , "QQ")%> "target=" _blank ">
</a>
<a href= "mailto:<%# DataBinder.Eval (Container.DataItem," email ")%>" ></a>
<a href= "javascript:;" admin "," "," Hide "," postcontent "," "," Show ")" >
</a >
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<BR>
</td>
</tr>
</table>
<table width= "100%" border= "0" cellspacing= "0" cellpadding= "2" align= "Center" height= "9" >
<TBODY>
<tr>
&LT;TD align= "Middle" >
<table class= "Gueststyleline" width= "610" border= "0" cellpadding= "1" cellspacing= "0" height= ">"
<tr>
&LT;TD height= ">"
<table width= "100%" border= "0" cellspacing= "0" cellpadding= "0" class= "GUESTSTYLEBG" height= ">"
<tr>
&LT;TD width= "35%" align= "Middle" height= ">"
Total <asp:label id= "Lblrecordcount" forecolor= "Red" runat= "server"/> Record is currently <asp:label id= "Lblcurrentpage" Forecolor= "Red" runat= "server"/>/<asp:label id= "Lblpagecount" forecolor= "Red" runat= "Server"/> Page
</td>
&LT;TD align= "Middle" height= ">"
<asp:linkbutton id= "Lbnprevpage" text= "previous page" Commandname= "prev" runat= "Server"/>
<asp:linkbutton id= "Lbnnextpage" text= "next page" Commandname= "Next" runat= "Server"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</TBODY>
</table>
</form>
</body>
</HTML>



Note: The command event for the two LinkButton control must be changed to Page_onclick:



Codebehind file:
private void Page_Load (object sender, System.EventArgs e)
{
pagesize=1;
Conn= New SqlConnection (application["Guest_conn"). ToString ());
Conn.Open ();
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 guest";
SqlCommand Mycomm = new SqlCommand (strcount,conn);
SqlDataReader dr = Mycomm.executereader ();
if (Dr. Read ())
{
intcount = Int32.Parse (dr["co"). ToString ());
}
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 guest ORDER by postdate Desc";
DataSet ds = new DataSet ();
SqlDataAdapter myadapter = new SqlDataAdapter (Strsel,conn);
Myadapter.fill (Ds,startindex,pagesize, "Guest");
Return DS. tables["Guest". DefaultView;
}



public void Listbind ()
{
Myguest.datasource = Createsource ();
Myguest.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 ();
}

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.