datalist 分页的用户控件,我公司sunYun原创,如需转载,请联系

80酷酷网    80kuku.com

  分页|控件|原创listControl.aspx
<% Control Language="vb" AutoEventWireup="false" Codebehind="listControl.ascx.vb" Inherits="govoa.listControl" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录  当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页
<asp:LinkButton id="lblPrevious" runat="server" text="上一页"></asp:LinkButton>
<asp:LinkButton id="lblNext" runat="server" text="下一页"></asp:LinkButton> 
<asp:DropDownList id="NumPerPage" runat="server" AutoPostBack="True">
    <asp:ListItem Value="5">
        5行/页</asp:ListItem>
    <asp:ListItem Value="8" Selected="True">
        8行/页</asp:ListItem>
    <asp:ListItem Value="10">
        10行/页</asp:ListItem>
    <asp:ListItem Value="15">
        15行/页</asp:ListItem>
    <asp:ListItem Value="20">
        20行/页</asp:ListItem>
    <asp:ListItem Value="25">
        25行/页</asp:ListItem>
    <asp:ListItem Value="30">
        30行/页</asp:ListItem>
    <asp:ListItem Value="35">
        35行/页</asp:ListItem>
    <asp:ListItem Value="40">
        40行/页</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtPage" Runat="server" Width="30"></asp:TextBox><asp:Button ID="btnGo" Runat="server" Text="转到"></asp:Button>



listControl.aspx.vb

Imports System.Data.SqlClient
Public MustInherit Class listControl
    Inherits System.Web.UI.UserControl
    Protected WithEvents lblRecordCount As System.Web.UI.WebControls.Label
    Protected WithEvents lblCurrentPage As System.Web.UI.WebControls.Label
    Protected WithEvents lblPageCount As System.Web.UI.WebControls.Label
    Protected WithEvents lblPrevious As System.Web.UI.WebControls.LinkButton
    Protected WithEvents lblNext As System.Web.UI.WebControls.LinkButton
    Protected WithEvents NumPerPage As System.Web.UI.WebControls.DropDownList
    Protected WithEvents txtPage As System.Web.UI.WebControls.TextBox
    Protected WithEvents btnGo As System.Web.UI.WebControls.Button
    Private m_DataContainer As Repeater
    Private m_datasource As String
    Private m_toRefresh As Boolean = False

    Private MyConn As SqlConnection
    Private RecordCount, PageCount, CurrentPage As Integer


    '/ <summary>
    '/ 取得需要绑定的控件
    '/ </summary>

    Public Property GetRelatedControl() As Repeater
        Get
            Return m_DataContainer
        End Get
        Set(ByVal Value As Repeater)
            m_DataContainer = Value
        End Set
    End Property

    Public Property ToRefresh() As Boolean
        Get
            Return m_toRefresh
        End Get
        Set(ByVal Value As Boolean)
            m_toRefresh = Value
        End Set
    End Property

    Public Property GetRelatedSqlStr() As String
        Get
            Return m_datasource
        End Get
        Set(ByVal Value As String)
            m_datasource = Value
            If m_toRefresh Then
                refreshData()
            End If
        End Set
    End Property


#Region " Web 窗体设计器生成的代码 "

    '该调用是 Web 窗体设计器所必需的。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
        '不要使用代码编辑器修改它。
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '在此处放置初始化页的用户代码
        MyConn = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("ConnectionString"))
        MyConn.Open()
        ViewState("PageSize") = NumPerPage.SelectedItem.Value.ToString()
        If Not Page.IsPostBack Then
            BindControl()
            CurrentPage = 0
            ViewState("PageIndex") = 0

            '计算总共有多少记录
            RecordCount = CalculateRecord()
            lblRecordCount.Text = RecordCount.ToString()

            '计算总共有多少页
            PageCount = Fix((RecordCount + Int32.Parse(NumPerPage.SelectedItem.Value)) / Convert.ToInt32(ViewState("PageSize").ToString())) - (IIf(RecordCount Mod Convert.ToInt32(ViewState("PageSize").ToString()) <> 0, 0, 1))  'ToDo: Unsupported feature: conditional (?) operator.
            lblPageCount.Text = PageCount.ToString()
            If PageCount <= 1 Then
                lblNext.Enabled = False
            End If
            ViewState("PageCount") = PageCount
        End If
    End Sub

    Private Sub refreshData()
        MyConn = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("ConnectionString"))
        MyConn.Open()
        ViewState("PageSize") = NumPerPage.SelectedItem.Value.ToString()

        BindControl()
        CurrentPage = 0
        ViewState("PageIndex") = 0

        '计算总共有多少记录
        RecordCount = CalculateRecord()
        lblRecordCount.Text = RecordCount.ToString()

        '计算总共有多少页

        PageCount = Fix((RecordCount + Int32.Parse(NumPerPage.SelectedItem.Value)) / Convert.ToInt32(ViewState("PageSize").ToString())) - (IIf(RecordCount Mod Convert.ToInt32(ViewState("PageSize").ToString()) <> 0, 0, 1)) 'ToDo: Unsupported feature: conditional (?) operator.
        lblPageCount.Text = PageCount.ToString()
        If PageCount <= 1 Then
            lblNext.Enabled = False
        End If
        ViewState("PageCount") = PageCount

    End Sub
    '/ <summary>
    '/ 绑定控件
    '/ </summary>
    Private Sub BindControl()

        m_DataContainer.DataSource = CreateSource()
        m_DataContainer.DataBind()
        '
        lblNext.Enabled = True
        lblPrevious.Enabled = True
        If CurrentPage = PageCount - 1 Then
            lblNext.Enabled = False
        End If
        If CurrentPage = 0 Then
            lblPrevious.Enabled = False
        End If
        lblCurrentPage.Text = (CurrentPage + 1).ToString()
        txtPage.Text = (CurrentPage + 1).ToString()
    End Sub 'BindControl

    '/ <summary>
    '/ 产生DataList的DataView
    '/ </summary>
    '/ <returns></returns>
    Private Function CreateSource() As DataView

        Dim StartIndex As Integer

        '设定导入的起终地址
        StartIndex = CurrentPage * Convert.ToInt32(ViewState("PageSize").ToString()) '计算起始索引
        Dim ds As New DataSet()

        Dim MyAdapter As New SqlDataAdapter(m_datasource, MyConn)
        MyAdapter.Fill(ds, StartIndex, Convert.ToInt32(ViewState("PageSize").ToString()), "Score")

        Return ds.Tables("Score").DefaultView
    End Function 'CreateSource

    '/ <summary>
    '/ 计算有多少记录
    '/ </summary>
    '/ <returns></returns>
    Public Function CalculateRecord() As Integer


        Dim ds As New DataSet()

        Dim MyAdapter As New SqlDataAdapter(m_datasource, MyConn)
        MyAdapter.Fill(ds, "tempTable")

        Return ds.Tables("tempTable").DefaultView.Count
    End Function 'CalculateRecord





    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
        Try
            Dim InputValue As Integer = Convert.ToInt32(txtPage.Text)
            PageCount = CInt(ViewState("PageCount"))
            If InputValue <= PageCount And InputValue > 0 Then
                CurrentPage = Convert.ToInt32(txtPage.Text) - 1
            Else
                CurrentPage = CInt(ViewState("PageIndex"))
            End If
            ViewState("PageIndex") = CurrentPage
        Catch
        End Try
        BindControl()
    End Sub



    Private Sub NumPerPage_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumPerPage.SelectedIndexChanged
        Try
            ViewState("PageSize") = NumPerPage.SelectedItem.Value

            CurrentPage = 0
            ViewState("PageIndex") = 0

            '计算总共有多少记录
            RecordCount = CalculateRecord()
            lblRecordCount.Text = RecordCount.ToString()

            '计算总共有多少页
            PageCount = RecordCount / Convert.ToInt32(ViewState("PageSize").ToString()) + (IIf(RecordCount Mod Convert.ToInt32(ViewState("PageSize").ToString()) = 0, 0, 1)) 'ToDo: Unsupported feature: conditional (?) operator.
            lblPageCount.Text = PageCount.ToString()
            ViewState("PageCount") = PageCount

            BindControl()
        Catch
        End Try
    End Sub

    Private Sub lblPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblPrevious.Click
        CurrentPage = CInt(ViewState("PageIndex"))
        PageCount = CInt(ViewState("PageCount"))



        If CurrentPage > 0 Then
            CurrentPage -= 1
        End If

        ViewState("PageIndex") = CurrentPage

        BindControl()
    End Sub

    Private Sub lblNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblNext.Click
        CurrentPage = CInt(ViewState("PageIndex"))
        PageCount = CInt(ViewState("PageCount"))



        If CurrentPage < PageCount - 1 Then
            CurrentPage += 1
        End If

        ViewState("PageIndex") = CurrentPage

        BindControl()
    End Sub
End Class

调用
自己看着办
几点说明:
这个是测试版本

掉用如下
        ListControl1.GetRelatedControl = rptContent
        ListControl1.ToRefresh = True  
        ListControl1.GetRelatedSqlStr = New bizlogic.DBFilm().selectFilmStr(ddl_Type.SelectedItem.Value, tbx_keyWord.Text)

如果有bug
请和 H.xue163.net联系,多谢
一点说明:
我这里分页是repeater ,但是如果要用datalist也很容易,这个我就不说明了。大家一看就知道

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: