如何在Datalist控件中进行分页?

时间:2012-06-29 20:10:31

标签: asp.net vb.net paging datalist

我在ASP.NET 3.5 Access 2003 DataSource中有我的DataList。我想使用VB为DataList控件创建分页。

1 个答案:

答案 0 :(得分:2)

Output DataList缺少GridView中的pagging功能,但可以实现DataList的pagging功能。 这里我使用PagedDataSource进行了pagging,其中包含数据绑定控件的分页相关属性,允许它执行pagging。有关PagedDataSource

的更多详情

内嵌代码

   <div>
        <asp:DataList ID="dataListStudent" runat="server">
            <ItemTemplate>
                <table cellpadding="10">
                    <tr>
                        <td nowrap="nowrap">
                            <b>Student id</b>
                        </td>
                        <td nowrap="nowrap">
                            <b>First name</b>
                        </td>
                        <td nowrap="nowrap">
                            <b>Last name</b>
                        </td>
                        <td>
                            <b>City</b>
                        </td>
                    </tr>
                    <hr />
                    <tr>
                        <td nowrap="nowrap">
                            <%# Eval("StudentID") %>
                        </td>
                        <td nowrap="nowrap">
                            <%# Eval("FirstName") %>
                        </td>
                        <td nowrap="nowrap">
                            <%# Eval("LastName") %>
                        </td>
                        <td nowrap="nowrap">
                            <%# Eval("City") %>
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:DataList>
        <br />
        <table border="0" width="410">
            <tr>
                <td align="left">
                    <asp:LinkButton ID="lbPrev" runat="server">
Prev
                    </asp:LinkButton>
                </td>
                <td align="right">
                    <asp:LinkButton ID="lbNext" runat="server">
Next
                    </asp:LinkButton>
                </td>
            </tr>
        </table>
    </div>

<强>代码隐藏

Imports System.Data
Imports System.Data.OleDb

Partial Class _Default
    Inherits System.Web.UI.Page
    Dim pageds As New PagedDataSource()

    Public Property CurrentPage() As Integer

        Get
            If Me.ViewState("CurrentPage") Is Nothing Then
                Return 0
            Else
                Return Convert.ToInt16(Me.ViewState("CurrentPage").ToString())
            End If
        End Get

        Set(ByVal value As Integer)
            Me.ViewState("CurrentPage") = value
        End Set
    End Property
    Sub bindDataList()
        Dim sql As String
        sql = "SELECT * FROM STUDENTS"
        Dim da As New OleDbDataAdapter(sql, "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Course.mdb")
        Dim dt = New DataTable()
        da.Fill(dt)
        Try
            pageds.DataSource = dt.DefaultView
            pageds.AllowPaging = True
            pageds.PageSize = 3
            pageds.CurrentPageIndex = CurrentPage
            lbNext.Enabled = Not pageds.IsLastPage
            lbPrev.Enabled = Not pageds.IsFirstPage
            dataListStudent.DataSource = pageds
            dataListStudent.DataBind()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Protected Sub lbPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbPrev.Click
        currentPage -= 1
        bindDataList()
    End Sub
    Protected Sub lbNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbNext.Click
        currentPage += 1
        bindDataList()

    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            bindDataList()

        End If
    End Sub
End Class

您的连接字符串,列名可能不同您可能希望DataList的不同布局随意摆弄它。 希望它能帮助您解决问题。