DataList controls also play paging (go from aspcn.com)

Source: Internet
Author: User
Pagination | Controls are well known, ASP. NET provides us with three data control--datagrid,repeater,datalist. In these three controls, the DataGrid control is the most powerful, repeater control is most faithful to the template, and the DataList control is both.

The DataGrid control is too famous, so it used to speak a lot, repeater function too little, nothing good to say. This is mainly about DataList controls.

DataList control is actually very powerful, he supports the selection, editing, implementation of the method is also very simple, but the most troubling is that it does not like the DataGrid control built-in paging functionality, such a good control should not 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.

(This procedure is tested through the. Net Framework Beta 2)


<% @ Page language= "C #"%>
<% @ Import namespace= "System.Data"%>
<% @ Import namespace= "System.Data.OleDb"%>
<script language= "C #" runat= "Server" >
/*
Create by Flying Knife
Http://www.aspcn.com
2001-7-25 01:44

Support. Net Framework Beta 2
*/
OleDbConnection myconn;
int pagesize,recordcount,pagecount,currentpage;
public void Page_Load (Object Src,eventargs E)
{
Set pagesize
PageSize = 10;

Connection statement
String myConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" Data source= "+server.mappath" (".") +".. \\DataBase\\db1.mdb; ";
myconn = new OleDbConnection (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 Score";
OleDbCommand Mycomm = new OleDbCommand (strcount,myconn);
OleDbDataReader 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 Score";
DataSet ds = new DataSet ();

OleDbDataAdapter myadapter = new OleDbDataAdapter (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, "name")%>
<asp:linkbutton id= "Btnselect" text= "Editing" commandname= "edit" runat= "Server"/>
</ItemTemplate>
</asp:DataList>
<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>


 


Run the results as shown above:)

When you write a program, the most important thing is to think of their own brain, and definitely not a problem to where to ask. The problem is so simple that no one wants to answer it.

A lot of thinking, a lot of information, is the real harvest.



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.