簡單的repeater分頁程式碼
昨天研究了一天才隱約明白了一點這種利用ds給賦新值的分頁方法。
practice_1
--------------------------------------------
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="vb" runat="server">
'定義bindlist子程式,呼叫pageno(頁面位置)引數
sub BindList(PageNo As Integer)
'使用connection物件開啟資料連線
Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("db/music.mdb")
'定義sql
Dim strSQL As String = "Select * From 唱片,風格 where 唱片.風格號碼 = 風格.風格號碼 Order By '新增時間' Desc"
'建立dataadapter物件
Dim objCmd As New OleDbDataAdapter(strSQL, ConnString)
'建立dataset物件
Dim DS As New DataSet()
'呼叫dataadapter物件的fill()方法講選取的資料置入dataset物件。
objCmd.Fill(DS, "唱片")
'宣告mytable 變數為 datatable物件,並將ds.tables("歌曲")賦值給 mytable變數。
Dim myTable As DataTable = DS.Tables("唱片")
'定義每頁面顯示2條記錄。
dim pagesize as integer = 2
'顯示留言的總條數
dim totalrecord as integer = mytable.rows.count
showtotalrecord.text = totalrecord
'顯示流言總頁數totalrecord為流言總條數
dim totalpage as integer
if totalrecord < pagesize then
totalpage = 1
else
if totalrecord mod pagesize <> 0 then
totalpage = totalrecord / pagesize + 1
else
totalpage = totalrecord / pagesize
end if
end if
ShowTotalPage.Text = TotalPage
'將當前頁碼賦給Showpagenumber控制元件,以顯示當前頁數
Showpagenumber.text = pageno
'建立 tempTable 資料表,其結構與mytable相同,tempTable 資料表
'儲存的資料會連線到 Repeater 控制元件,以顯示留言內容。
Dim tempTable As DataTable = myTable.Clone()
Dim I, J As Integer
For I = (PageNo -1) * PageSize To PageNo * PageSize - 1
If I > myTable.Rows.Count -1 Then Exit For
Dim newRow As DataRow = tempTable.NewRow()
For J = 0 To myTable.Columns.Count -1
newRow(myTable.Columns(J).ColumnName) = myTable.Rows(I).Item(J)
Next
tempTable.Rows.Add(newRow)
Next
myRepeater.DataSource = tempTable
myRepeater.DataBind()
End Sub
'此過程用來判斷是否應該顯示,首頁等按鈕。
sub showwhatbutton()
if Showpagenumber.text = 1 then
firstpage.visible = false
prepage.visible = false
else
firstpage.visible = true
prepage.visible = true
end if
if Showpagenumber.text = showtotalpage.text then
lastpage.visible = false
nextpage.visible = false
else
lastpage.visible = true
nextpage.visible = true
end if
end sub
'此程式用來建立快速換頁選單
Sub PageListItem()
Dim I As Integer
PageList.Items.Clear()
For I = 0 To ShowTotalPage.Text -1
PageList.Items.Add("第 " & I + 1 & " 頁")
Next
End Sub
'頁面第一次載入時執行此程式,呼叫bindlist和pagelistitem子程式並賦值
Sub Page_Init(sender As Object, e As Eventargs)
BindList(1)
PageListItem()
showwhatbutton()
End Sub
'響應下拉選單 PageList 命令。
Sub PageList_Change(sender As Object, e As EventArgs)
BindList(PageList.SelectedIndex + 1)
showwhatbutton()
End Sub
'此過程會根據使用者點取的按鈕來決定應該顯示哪個分頁的資料
sub pagechange(sender as object, e as commandeventargs)
select case e.commandargument
case "nextpage"
BindList( Showpagenumber.text + 1)
case "prepage"
BindList( Showpagenumber.text - 1)
case "firstpage"
BindList( 1 )
case "lastpage"
BindList( showtotalpage.text - 1)
'當用戶在文字框裡輸入 數字的時候執行下面程式碼。
'如果使用者輸入的數字大於總頁數時,顯示最後一頁。如果小於首頁時,顯示首頁。
case "pagenumber"
if pagenumber.text > showtotalpage.text then
pagenumber.text = showtotalpage.text
bindlist(pagenumber.text)
else if pagenumber.text < 1 then
pagenumber.text = 1
bindlist(pagenumber.text)
else
bindlist(pagenumber.text)
end if
end select
showwhatbutton()
end sub
</script>
<Html>
<Body>
<Form Runat="Server">
收錄專輯總數:<Asp:Label Runat="Server" Id="ShowTotalrecord" /><br>
目前位於第<Asp:Label Runat="Server" Id="Showpagenumber" />頁<br>
<asp:linkbutton runat="server" ID="firstpage" OnCommand="pagechange" Text="首頁" CommandArgument="firstpage"/>
<asp:linkbutton runat="server" ID="lastpage" OnCommand="pagechange" Text="尾頁" CommandArgument="lastpage"/>
<asp:linkbutton runat="server" ID="prepage" OnCommand="pagechange" Text="上一頁" CommandArgument="prepage"/>
<asp:linkbutton runat="server" ID="nextpage" OnCommand="pagechange" Text="下一頁" CommandArgument="nextpage"/>
<br>
總頁數:<Asp:Label Runat="Server" Id="showtotalpage" /><br>
轉到:
<asp:dropdownlist runat="Server" ID="PageList" AutoPostBack="True" OnSelectedIndexChanged="PageList_Change" />
<asp:textbox runat="server" id="pagenumber" ontextchange="change"/>
<asp:button runat="server" ID="numberbutton" OnCommand="pagechange" Text="GO" CommandArgument="pagenumber"/>
<Asp:Repeater Runat="Server" Id="myRepeater">
<ItemTemplate> <Br>
<Br>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="147" rowspan="6"> <div align="center"><Img Src="<%# Container.DataItem("唱片圖片") %>"></div></td>
<td width="362"> </td>
</tr>
<tr>
<td>樂隊:<%# Container.DataItem("樂隊名") %></td>
</tr>
<tr>
<td>唱片:<%# Container.DataItem("唱片名") %></td>
</tr>
<tr>
<td>加入時間:<%# Container.DataItem("新增時間")%> </td>
</tr>
<tr>
<td>推薦指數:<%# Container.DataItem("欣賞指數")%></td>
</tr>
<tr>
<td height="41">唱片風格:<%# Container.DataItem("風格名")%></td>
</tr>
</table>
<Br>
</ItemTemplate>
</Asp:Repeater>
</Form>
</Body>
</Html>
---------------------------------------------------------------------------------------------------
practice_2
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="vb" runat="server">
'定義bindlist子程式,呼叫pageno(頁面位置)引數
sub BindList(PageNo As Integer)
'使用connection物件開啟資料連線
Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("db/music.mdb")
'定義sql
Dim strSQL As String = "Select * From 唱片,風格 where 唱片.風格號碼 = 風格.風格號碼 Order By '新增時間' Desc"
'建立dataadapter物件
Dim objCmd As New OleDbDataAdapter(strSQL, ConnString)
'建立dataset物件
Dim DS As New DataSet()
'呼叫dataadapter物件的fill()方法講選取的資料置入dataset物件。
objCmd.Fill(DS, "唱片")
'宣告mytable 變數為 datatable物件,並將ds.tables("歌曲")賦值給 mytable變數。
Dim myTable As DataTable = DS.Tables("唱片")
dim pagesize as integer = 2
'顯示留言的總條數
dim totalrecord as integer = mytable.rows.count
showtotalrecord.text = totalrecord
'顯示流言總頁數totalrecord為流言總條數
dim totalpage as integer
if totalrecord < pagesize then
totalpage = 1
else
if totalrecord mod pagesize <> 0 then
totalpage = totalrecord / pagesize + 1
else
totalpage = totalrecord / pagesize
end if
end if
ShowTotalPage.Text = TotalPage
'建立 tempTable 資料表,其結構與mytable相同,tempTable 資料表
'儲存的資料會連線到 Repeater 控制元件,以顯示留言內容。
Dim tempTable As DataTable = myTable.Clone()
Dim I, J As Integer
For I = (PageNo -1) * PageSize To PageNo * PageSize - 1
If I > myTable.Rows.Count -1 Then Exit For
Dim newRow As DataRow = tempTable.NewRow()
For J = 0 To myTable.Columns.Count -1
newRow(myTable.Columns(J).ColumnName) = myTable.Rows(I).Item(J)
Next
tempTable.Rows.Add(newRow)
Next
myRepeater.DataSource = tempTable
myRepeater.DataBind()
End Sub
'此過程用來判斷是否應該顯示,首頁等按鈕。
sub showwhatbutton()
if pagelist.selectedindex= 0 then
firstpage.visible = false
prepage.visible = false
else
firstpage.visible = true
prepage.visible = true
end if
if pagelist.selectedindex = pagelist.items.count - 1 then
lastpage.visible = false
nextpage.visible = false
else
lastpage.visible = true
nextpage.visible = true
end if
end sub
'此程式用來建立快速換頁選單
Sub PageListItem()
Dim I As Integer
PageList.Items.Clear()
For I = 0 To ShowTotalPage.Text -1
PageList.Items.Add("第 " & I + 1 & " 頁")
Next
End Sub
'頁面第一次載入時執行此程式,呼叫bindlist和pagelistitem子程式並賦值
Sub Page_Init(sender As Object, e As Eventargs)
BindList(1)
PageListItem()
showwhatbutton()
End Sub
'響應下拉選單 PageList 命令。
Sub PageList_Change(sender As Object, e As EventArgs)
BindList(PageList.SelectedIndex + 1)
showwhatbutton()
End Sub
sub pagechange(sender as object, e as commandeventargs)
select case e.commandargument
case "firstpage"
pagelist.selectedindex = 0
case "nextpage"
pagelist.selectedindex += 1
case "prepage"
pagelist.selectedindex -= 1
case "lastpage"
pagelist.selectedindex = showtotalpage.text - 1
end select
bindlist(PageList.SelectedIndex + 1)
showwhatbutton()
end sub
</script>
<Html>
<Body>
<Form Runat="Server">
收錄專輯總數:<Asp:Label Runat="Server" Id="ShowTotalrecord" />
<asp:linkbutton runat="server" ID="firstpage" OnCommand="pagechange" Text="首頁" CommandArgument="firstpage"/>
<asp:linkbutton runat="server" ID="lastpage" OnCommand="pagechange" Text="尾頁" CommandArgument="lastpage"/>
<asp:linkbutton runat="server" ID="prepage" OnCommand="pagechange" Text="上一頁" CommandArgument="prepage"/>
<asp:linkbutton runat="server" ID="nextpage" OnCommand="pagechange" Text="下一頁" CommandArgument="nextpage"/>
<br>
總頁數:<Asp:Label Runat="Server" Id="showtotalpage" />
目前位於:<asp:dropdownlist runat="Server" ID="PageList" AutoPostBack="True" OnSelectedIndexChanged="PageList_Change" />
<Asp:Repeater Runat="Server" Id="myRepeater">
<ItemTemplate> <Br>
<Br>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="147" rowspan="6"> <div align="center"><Img Src="<%# Container.DataItem("唱片圖片") %>"></div></td>
<td width="362"> </td>
</tr>
<tr>
<td>樂隊:<%# Container.DataItem("樂隊名") %></td>
</tr>
<tr>
<td>唱片:<%# Container.DataItem("唱片名") %></td>
</tr>
<tr>
<td>加入時間:<%# Container.DataItem("新增時間")%> </td>
</tr>
<tr>
<td>推薦指數:<%# Container.DataItem("欣賞指數")%></td>
</tr>
<tr>
<td height="41">唱片風格:<%# Container.DataItem("風格名")%></td>
</tr>
</table>
<Br>
</ItemTemplate>
</Asp:Repeater>
</Form>
</Body>
</Html>
---------------------------------------------------------------
資料庫可以按照程式碼裡自行建立