Datalist and datarepeater in ASP. NET provide a simple and fast way to display the data. In this way, we can use <itemtemplate> to control the data emission style as we like !. Unfortunately, unlike the DataGrid, they have built-in paging functions.
How can this problem be solved?
In fact, we can use the [pageddatasource] class to solve the paging problem.
Attributes of the pageddatasource class:
Datasource-Data Source
Whether allowpaging-true allows paging.
Pagesize-number of items per page
Pagecount-Total number of pages
Currentpageindex-current page index
The Code is as follows:
<% @ Page Language = "VB" %>
<% @ Import namespace = "system. Data" %>
<SCRIPT runat = "server">
Sub page_load (byval sender as object, byval e as eventargs)
Dim PGDS as pageddatasource = new pageddatasource
PGDS. datasource = createdatasource (). defaultview
PGDS. allowpaging = true
PGDS. pagesize = 6
Lbltotalpage. Text = PGDS. pagecount. tostring ()
Dim currentpage as integer
If not request. querystring ("page") is nothing then
Currentpage = convert. toint32 (request. querystring ("page "))
Else
Currentpage = 1
End if
PGDS. currentpageindex = currentpage-1
Lblcurrentpage. Text = currentpage. tostring ()
If not PGDS. isfirstpage then
Lnkprev. navigateurl = request. currentexecutionfilepath + "? Page = "+ convert. tostring (currentpage-1)
End if
If not PGDS. islastpage then
Lnknext. navigateurl = request. currentexecutionfilepath + "? Page = "+ convert. tostring (currentpage + 1)
End if
Repeater1.datasource = PGDS
Repeater1.databind ()
End sub
Function createdatasource () as datatable
Dim dT as datatable
Dim Dr as datarow
Dim I as integer
Dt = new datatable
DT. Columns. Add (New datacolumn ("integervalue", GetType (integer )))
DT. Columns. Add (New datacolumn ("stringvalue", GetType (string )))
DT. Columns. Add (New datacolumn ("datetimevalue", GetType (datetime )))
DT. Columns. Add (New datacolumn ("boolvalue", GetType (Boolean )))
For I = 0 to 50
Dr = DT. newrow ()
Dr (0) = I
Dr (1) = "item" + I. tostring ()
Dr (2) = datetime. Now. tow.timestring
If (I mod 2 <> 0) then
Dr (3) = true
Else
Dr (3) = false
End if
DT. Rows. Add (DR)
Next
Return dt
End Function
</SCRIPT>
<HTML> <Title> datarepeater </title>
<Style type = text/CSS>
Body {
Font: 10px verdana, Arial, Helvetica, "sans serif"; color: #000000;
}
. Txt {
Font-size: 12px
}
</Style>
</Head>
<Body>
<Form ID = "form1" name = form1 method = post runat = "server">
<Table class = TXT width = "100%" border = 0>
<Tbody> <tr> <TD>
<Asp: hyperlink id = lnkprev runat = "server"> previous page </ASP: hyperlink>
<Asp: hyperlink id = lnknext runat = "server"> next page </ASP: hyperlink>
<Asp: Label id = lblcurrentpage runat = "server"> </ASP: Label> total pages <asp: Label id = lbltotalpage runat = "server"> </ASP: label> page
</TD> </tr> </tbody> </table>
<Asp: repeater id = repeater1 runat = "server">
<Itemtemplate>
<HR Align = "Left" width = "60%" size = "1">
<Table class = TXT width = "100%" border = "0">
<Tr>
<TD>
Order Date: <% # databinder. eval (container. dataitem, "datetimevalue", "{0: d}") %>
</TD> <tr> <TD>
Quantity: <% # databinder. eval (container. dataitem, "integervalue", "{0: N2}") %>
</TD> <tr> <TD>
Item: <% # databinder. eval (container. dataitem, "stringvalue") %>
</TD> <tr> <TD>
Order Date: <asp: checkbox id = CHK1 checked = '<% # databinder. eval (container. dataitem, "boolvalue") %> 'runat = "server"/>
</TD> </tr> </table>
</Itemtemplate>
</ASP: repeater>
<HR hight = "1">
</Form>
</Body>
</Html>