最近在學寫ASP.net,感覺不錯!
不過既然datagrid有支援分頁怎麼datalist不順便支援?
流程是Sqldatasource控制項→PagedDataSource控制項→DataList控制項
參考”廖瑞奇站長的文章-用Repeater分頁”撰寫以下程式
範例:http://vip.blueshop.com.tw/joehwang/flower/default.aspx
下載:http://joehwang.myweb.hinet.net/xuite/page_datalist.zip
default.aspx
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim db1 As New SqlDataSource
db1.ProviderName = "System.Data.OleDb"
db1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|flower.mdb;"
db1.SelectCommand = "select * from [flowers_plants]"
Dim dv As Data.DataView = db1.Select(New DataSourceSelectArguments)
Dim Pgds As PagedDataSource = New PagedDataSource '//宣告PagedDataSource分頁物件
Pgds.DataSource = dv '//將DataView寫入PagedDataSource
Pgds.AllowPaging = True '//是否分頁屬性
Pgds.PageSize = 8 '//一頁顯示幾筆紀錄
Label1.Text = Pgds.PageCount.ToString() '//總頁數
'//如果Request("page")沒有頁數等於第一頁
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
Label1.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
Me.DataList1.DataSource = Pgds '把分頁物件的資料丟給datalist1顯示
DataList1.RepeatColumns = 4'設定行數
DataList1.RepeatDirection = RepeatDirection.Horizontal
'Horizontal 清單的項目會由左而右,然後由上而下,以水平成列顯示,直到所有項目皆呈現。
' GridView1.AutoGenerateColumns = True
Me.DataList1.DataBind()
Dim i As Integer
Dim oItem As New ListItem()
For i = 1 To (Pgds.PageCount)
oItem.Text = "第" & i & "頁"
oItem.Value = "default.aspx?page=" & i
ddlist1.Items.Insert(i, New ListItem(oItem.Text, oItem.Value))
Next i
End Sub
Protected Sub ddlist1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Label1.Text = ddlist1.SelectedItem.Value
Response.Redirect(ddlist1.SelectedItem.Value)
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>未命名頁面</title>
</head>
<body>
<form id="form1" runat="server">
<div align=center>
<asp:DropDownList ID="ddlist1" runat="server" OnSelectedIndexChanged="ddlist1_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Value="新增">請選擇</asp:ListItem>
</asp:DropDownList>
<asp:DataList ID="DataList1" runat="server" CellPadding="3" Width="152px" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellSpacing="2" GridLines="Both">
<ItemTemplate>
<a href="detail.aspx?id=<%# DataBinder.Eval(Container.DataItem, "fp_id") %>"><img src=small/<%# DataBinder.Eval(Container.DataItem, "s_file") %> /></a>
</ItemTemplate>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<ItemStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
</asp:DataList></div>
<div align=center> 目前是第<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>頁</div>
<div align=center>
<asp:HyperLink ID="lnkPrev" runat="server">上一頁</asp:HyperLink>
<asp:HyperLink ID="lnkNext" runat="server">下一頁</asp:HyperLink>
</div>
</form>
</body>
</html>
detail.aspx
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim id As Integer
id = Request.QueryString("id")
Dim db1 As New SqlDataSource
db1.ProviderName = "System.Data.OleDb"
db1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|flower.mdb;"
db1.SelectCommand = "select * from [flowers_plants] where fp_id= ? "
db1.SelectParameters.Add("fp_id", id)
Dim dv As Data.DataView = db1.Select(New DataSourceSelectArguments)
Me.FormView1.DataSource = dv
Me.FormView1.DataBind()
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>未命名頁面</title>
</head>
<body>
<form id="form1" runat="server">
<div align=center>
<asp:FormView ID="FormView1" runat="server" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical">
<ItemTemplate>
<img src=small/<%# DataBinder.Eval(Container.DataItem, "s_file") %> /><br />
ID:
<asp:Label ID="fp_idLabel" runat="server" Text='<%# Bind("fp_id") %>'></asp:Label><br />
名稱:
<asp:Label ID="c_nameLabel" runat="server" Text='<%# Bind("c_name") %>'></asp:Label><br />
學名:
<asp:Label ID="e_nameLabel" runat="server" Text='<%# Bind("e_name") %>'></asp:Label><br />
家族:
<asp:Label ID="familyLabel" runat="server" Text='<%# Bind("family") %>'></asp:Label><br />
別名:
<asp:Label ID="aliasLabel" runat="server" Text='<%# Bind("alias") %>'></asp:Label><br />
科名:
<asp:Label ID="scientific_nameLabel" runat="server" Text='<%# Bind("scientific_name") %>'>
</asp:Label><br />
類別
<asp:Label ID="classLabel" runat="server" Text='<%# Bind("class") %>'></asp:Label><br />
</ItemTemplate>
<FooterStyle BackColor="#CCCC99" />
<EditRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F7DE" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
</asp:FormView>
</div>
</form>
</body>
</html>