In asp.net, how to do the paging I have been asked more than once, how to implement paging in the ASP.net function. I really do not want to answer this question. Because in the asp.net to achieve pagination, it is too simple, simple to see the program will go to the gas to jump, oh to publish sigh, why this dongdong not early out.
In the live web technology, we want to do pagination, is often a long string of code to get it, and every page, we need to rewrite once, annoying. But with the DataGrid control in ASP.net, our paging program can be easily done with just a few settings for the DataGrid control. We are still using a program to see:
<% @ Page language= "C #"%>
<% @ Import namespace= "System.Data"%>
<% @ Import namespace= "System.Data.ADO"%>
<script language= "C #" runat= "Server" >
public void Page_Load (Objectsrc,eventargs e)
{
file://Joint statement
String myConnString = "Driver={microsoft Access Driver (*.mdb)};D bq=c:/test/test.mdb;";
String Strcomm = "SELECT * from userlist order by id";
file:// Open a connection.
Adoconnectionmyconnection = new ADOConnection (myConnString);
file://two dozen DataSetCommand.
Adodatasetcommandmycomm = new Adodatasetcommand (strcomm,myconnection);
Datasetmydataset = new DataSet ();
file://put Userlist,booklist table to DataSet
Mycomm.filldataset (myDataSet, "userlist");
DataGrid1.DataSource = mydataset.tables["UserList"]. DefaultView;
Datagrid1.databind ();
}
</script>
<title></title>
<body>
<form runat= "Server" >
<asp:datagrid id= "DATAGRID1" runat= "Server"
Bordercolor= "BLACK"
Borderwidth= "1"
Gridlines= "Both"
Cellpadding= "3"
cellspacing= "0"
Font-name= "Verdana"
Font-size= "8pt"
Headerstyle-backcolor= "#aaaadd"
Alternatingitemstyle-backcolor= "#eeeeee"
/>
</form>
</body>
Its display results are:
Figure 11-1
You can see that 11 of the data in this userlist table are all out, no paging.
Now let's change the properties of the DataGrid control. Plus
Allowpaging= "True"
Pagesize= "5"
Pagerstyle-horizontalalign= "Right"
Look again:
Figure 11-2
Look at the bottom of the picture, is not more, is not, this means pagination, we go to the label can see the next page:
Figure 11-4
It's all too easy. Oh. Their source is just that I added the three attributes. In fact, as long as a allowpaging on the line.
AllowPaging refers to allowing paging, which is the most important. With it, we can be divided into pages. PAGESIZE specifies the number of records to display per page, and if not, the default is 10.
Pagerstyle-horizontalalign is the location that specifies the faceted display, and the default is left.
All code is:
<asp:datagrid id= "DATAGRID1" runat= "Server"
Allowpaging= "True"
Pagesize= "5"
Pagerstyle-horizontalalign= "Right"
Bordercolor= "BLACK"
Borderwidth= "1"
Gridlines= "Both"
Cellpadding= "3"
cellspacing= "0"
Font-name= "Verdana"
Font-size= "8pt"
Headerstyle-backcolor= "#aaaadd"
Alternatingitemstyle-backcolor= "#eeeeee"
/>
is not very simple. Oh.
Note that when writing this do not forget <form>, otherwise your page is able to show, but can not turn, hehe. Because this is required to be submitted:)