datalist 分頁的用戶控件,我公司sunYun原創,如需轉載,請聯系作者
發表于:2007-06-30來源:作者:點擊數:
標簽:
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
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.De
buggerStepThrough()> 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.xue@163
.net聯系,多謝
一點說明:
我這里分頁是repeater ,但是如果要用datalist也很容易,這個我就不說明了。大家一看就知道
原文轉自:http://www.anti-gravitydesign.com