SqlDatasource+DataList的分頁範例

本文介绍了一种使用 ASP.NET 和 VB 实现数据分页的方法。通过 SqlDataSource 连接数据库并利用 PagedDataSource 控件进行分页显示,具体讲解了如何设置每页显示的记录数、生成页面导航链接及下拉菜单选择页数等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>
        &nbsp;<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值