0

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

4

1 回答 1

2

输出 DataList 缺少像 in 那样的GridView分页功能,但可以为DataList. 在这里,我已经完成 PagedDataSource了分页,其中包含数据绑定控件的分页相关属性,允许它执行分页。有关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 的不同布局随意摆弄它。 希望它能帮助您解决问题。

于 2012-06-30T01:51:38.483 回答