综合

img rickjelly2004

翻页的WEB控件

发表于2004/8/26 12:53:00  2656人阅读

分类: asp.net

总共分三部分:。VB,Builder和Design

Imports System.ComponentModel

Imports System.web.UI.HtmlControls

Imports System.Web.UI

Imports System.Web.UI.WebControls

Imports System.Data

Imports System.Data.OracleClient

 

<DefaultProperty("ChangePage"), ControlBuilderAttribute(GetType(ChangePageBuilder)), _

ParseChildren(False), PersistChildren(False), _

Designer(GetType(ChangePageDesigner)), _

ToolboxData("<{0}:ChangePage runat=server></{0}:ChangePage>")> Public Class ChangePage

    Inherits System.Web.UI.WebControls.Panel

    Implements INamingContainer

 

    Private WithEvents txtPage As newTextBox

    Private WithEvents btnFirst As newButton

    Private WithEvents btnNext As newButton

    Private WithEvents btnPrevious As newButton

    Private WithEvents btnLast As newButton

    Private WithEvents btnGo As newButton

    Private WithEvents lblNowPage As newLabel

    Private WithEvents lblAllPage As newLabel

    Private WithEvents lblAllItems As newLabel

    Private WithEvents lblPage As newLabel

    Private htmlTabel As Table

    Private htmlRow As TableRow

    Private isCreated As Boolean

    Private mDataGrid As String

 

    Public Event PageIndexChange As EventHandler

 

    Public Sub New()

    End Sub

 

#Region "プロパティ"

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _

             NotifyParentProperty(True), PersistenceMode(PersistenceMode.InnerProperty), _

             Category("ChildControl"), Description("検索ボタン")> _

             Public ReadOnly Property PageTextBox() As newTextBox

        Get

            Me.EnsureChildControls()

            Return txtPage

        End Get

    End Property

 

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _

          NotifyParentProperty(True), PersistenceMode(PersistenceMode.InnerProperty), _

          Category("ChildControl"), Description("初頁ボタン")> _

          Public ReadOnly Property FirstPageButton() As newButton

        Get

            Me.EnsureChildControls()

            Return btnFirst

        End Get

    End Property

 

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _

             NotifyParentProperty(True), PersistenceMode(PersistenceMode.InnerProperty), _

             Category("ChildControl"), Description("次頁ボタン")> _

             Public ReadOnly Property NextPageButton() As newButton

        Get

            Me.EnsureChildControls()

            Return btnNext

        End Get

    End Property

 

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _

                     NotifyParentProperty(True), PersistenceMode(PersistenceMode.InnerProperty), _

                     Category("ChildControl"), Description("前頁ボタン")> _

                     Public ReadOnly Property PrePageButton() As newButton

        Get

            Me.EnsureChildControls()

            Return btnPrevious

        End Get

    End Property

 

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _

                    NotifyParentProperty(True), PersistenceMode(PersistenceMode.InnerProperty), _

                    Category("ChildControl"), Description("末頁ボタン")> _

                    Public ReadOnly Property LastPageButton() As newButton

        Get

            Me.EnsureChildControls()

            Return btnLast

        End Get

    End Property

 

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _

                 NotifyParentProperty(True), PersistenceMode(PersistenceMode.InnerProperty), _

                 Category("ChildControl"), Description("遷移ボタン")> _

                 Public ReadOnly Property ChangePageButton() As newButton

        Get

            Me.EnsureChildControls()

            Return btnGo

        End Get

    End Property

 

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _

                 NotifyParentProperty(True), PersistenceMode(PersistenceMode.InnerProperty), _

                 Category("ChildControl"), Description("現在の頁ラベル")> _

                 Public ReadOnly Property NowPageLabel() As newLabel

        Get

            Me.EnsureChildControls()

            Return lblNowPage

        End Get

    End Property

 

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _

                    NotifyParentProperty(True), PersistenceMode(PersistenceMode.InnerProperty), _

                    Category("ChildControl"), Description("合計頁ラベル")> _

                    Public ReadOnly Property AllPageLabel() As newLabel

        Get

            Me.EnsureChildControls()

            Return lblAllPage

        End Get

    End Property

 

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _

                        NotifyParentProperty(True), PersistenceMode(PersistenceMode.InnerProperty), _

                        Category("ChildControl"), Description("合計項目ラベル")> _

                        Public ReadOnly Property AllItemsLabel() As newLabel

        Get

            Me.EnsureChildControls()

            Return lblAllItems

        End Get

    End Property

 

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _

                        NotifyParentProperty(True), PersistenceMode(PersistenceMode.InnerProperty), _

                        Category("ChildControl"), Description("頁ラベル")> _

                        Public ReadOnly Property PageLabel() As newLabel

        Get

            Me.EnsureChildControls()

            Return lblPage

        End Get

    End Property

 

    <Category("Behavior"), Description("データグリッド項目のコントロール"), DefaultValue("")> _

        Public Property DataGrid() As String

        Get

            Return mDataGrid

        End Get

        Set(ByVal Value As String)

            mDataGrid = Value

        End Set

    End Property

 

#End Region

 

    Protected Overrides Sub CreateChildControls()

        htmlTabel = New Table

        htmlRow = New TableRow

 

        If txtPage Is Nothing Then

            isCreated = False

            txtPage = New newTextBox

            txtPage.ID = MyBase.ID & "_PageTextBox"

        Else

            isCreated = True

        End If

 

        If btnFirst Is Nothing Then

            isCreated = False

            btnFirst = New newButton

            btnFirst.ID = MyBase.ID & "_FirstPageButton"

        Else

            isCreated = True

        End If

 

        If btnNext Is Nothing Then

            isCreated = False

            btnNext = New newButton

            btnNext.ID = MyBase.ID & "_NextPageButton"

        Else

            isCreated = True

        End If

 

        If btnPrevious Is Nothing Then

            isCreated = False

            btnPrevious = New newButton

            btnPrevious.ID = MyBase.ID & "_PrePageButton"

        Else

            isCreated = True

        End If

 

        If btnLast Is Nothing Then

            isCreated = False

            btnLast = New newButton

            btnLast.ID = MyBase.ID & "_LastPageButton"

        Else

            isCreated = True

        End If

 

        If btnGo Is Nothing Then

            isCreated = False

            btnGo = New newButton

            btnGo.ID = MyBase.ID & "_ChangePageButton"

        Else

            isCreated = True

        End If

 

        If lblNowPage Is Nothing Then

            isCreated = False

            lblNowPage = New newLabel

            lblNowPage.ID = MyBase.ID & "_NowPageLabel"

        Else

            isCreated = True

        End If

 

        If lblAllPage Is Nothing Then

            isCreated = False

            lblAllPage = New newLabel

            lblAllPage.ID = MyBase.ID & "_AllPageLabel"

        Else

            isCreated = True

        End If

 

        If lblAllItems Is Nothing Then

            isCreated = False

            lblAllItems = New newLabel

            lblAllItems.ID = MyBase.ID & "_AllItemsLabel"

        Else

            isCreated = True

        End If

 

        If lblPage Is Nothing Then

            isCreated = False

            lblPage = New newLabel

            lblPage.ID = MyBase.ID & "_PageLabel"

        Else

            isCreated = True

        End If

 

        MyBase.CreateChildControls()

 

        If isCreated = False Then

            lblPage.Text = "頁へ"

            btnFirst.Text = "初頁"

            btnPrevious.Text = "前頁"

            btnNext.Text = "次頁"

            btnLast.Text = "末頁"

            btnGo.Text = "遷移"

            lblNowPage.Text = "0"

            lblAllPage.Text = "/0頁"

            lblAllItems.Text = "  全て:0件"

            txtPage.Width = Unit.Pixel(40)

        End If

 

        htmlTabel.CellPadding = 0

        htmlTabel.CellSpacing = 0

        htmlTabel.Rows.Add(htmlRow)

        htmlRow.Cells.Add(New TableCell)

        htmlRow.Cells(0).HorizontalAlign = HorizontalAlign.Left

        htmlRow.Cells(0).Controls.Add(btnFirst)

        htmlRow.Cells(0).Controls.Add(btnPrevious)

        htmlRow.Cells(0).Controls.Add(btnNext)

        htmlRow.Cells(0).Controls.Add(btnLast)

        htmlRow.Cells.Add(New TableCell)

        htmlRow.Cells(1).HorizontalAlign = HorizontalAlign.Left

        htmlRow.Cells(1).Controls.Add(txtPage)

        htmlRow.Cells.Add(New TableCell)

        htmlRow.Cells(2).HorizontalAlign = HorizontalAlign.Left

        htmlRow.Cells(2).Controls.Add(lblPage)

        htmlRow.Cells.Add(New TableCell)

        htmlRow.Cells(3).HorizontalAlign = HorizontalAlign.Left

        htmlRow.Cells(3).Controls.Add(btnGo)

        htmlRow.Cells.Add(New TableCell)

        htmlRow.Cells(4).HorizontalAlign = HorizontalAlign.Left

        htmlRow.Cells(4).Controls.Add(lblNowPage)

        htmlRow.Cells.Add(New TableCell)

        htmlRow.Cells(5).HorizontalAlign = HorizontalAlign.Left

        htmlRow.Cells(5).Controls.Add(lblAllPage)

        htmlRow.Cells.Add(New TableCell)

        htmlRow.Cells(6).HorizontalAlign = HorizontalAlign.Left

        htmlRow.Cells(6).Controls.Add(lblAllItems)

 

        MyBase.Controls.Add(htmlTabel)

 

    End Sub

 

    Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)

        MyBase.EnsureChildControls()

        RenderChildren(output)

    End Sub

 

    Protected Overrides Sub AddParsedSubObject(ByVal obj As Object)

        Dim btnIdName As Array

        Dim lblIdName As Array

 

        If TypeOf obj Is newTextBox Then

            txtPage = obj

        End If

 

        If TypeOf obj Is newButton Then

            btnIdName = Split(CType(obj, newButton).ID.ToString, "_")

            Select Case btnIdName(1).ToString

                Case "FirstPageButton"

                    Me.btnFirst = obj

                Case "NextPageButton"

                    Me.btnNext = obj

                Case "PrePageButton"

                    Me.btnPrevious = obj

                Case "LastPageButton"

                    Me.btnLast = obj

                Case "ChangePageButton"

                    Me.btnGo = obj

            End Select

        End If

 

        If TypeOf obj Is newLabel Then

            lblIdName = Split(CType(obj, newLabel).ID.ToString, "_")

            Select Case lblIdName(1).ToString

                Case "NowPageLabel"

                    Me.lblNowPage = obj

                Case "AllPageLabel"

                    Me.lblAllPage = obj

                Case "AllItemsLabel"

                    Me.lblAllItems = obj

                Case "PageLabel"

                    Me.lblPage = obj

            End Select

        End If

 

    End Sub

 

    Private Sub btnFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFirst.Click

        MyBase.EnsureChildControls()

        If Not Page.FindControl(DataGrid) Is Nothing Then

            CType(Page.FindControl(DataGrid), DataGrid).CurrentPageIndex = 0

            txtPage.Text = ""

            RaiseEvent PageIndexChange(Me, e)

            Call NewPageDataBind()

        Else

            Throw New Exception(DataGrid & "を探し当てていない")

        End If

    End Sub

 

    Private Sub btnLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLast.Click

        Dim pageCount As Integer

        MyBase.EnsureChildControls()

 

        If Not Page.FindControl(DataGrid) Is Nothing Then

            pageCount = CType(Page.FindControl(DataGrid), DataGrid).PageCount

            CType(Page.FindControl(DataGrid), DataGrid).CurrentPageIndex = _

                Math.Max(pageCount - 1, 0)

            txtPage.Text = ""

            RaiseEvent PageIndexChange(Me, e)

            Call NewPageDataBind()

        Else

            Throw New Exception(DataGrid & "を探し当てていない")

        End If

    End Sub

 

    Private Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click

        Dim currentPageIndex As Integer

        Dim pageCount As Integer

        MyBase.EnsureChildControls()

 

        If Not Page.FindControl(DataGrid) Is Nothing Then

            pageCount = CType(Page.FindControl(DataGrid), DataGrid).PageCount

            currentPageIndex = CType(Page.FindControl(DataGrid), DataGrid).CurrentPageIndex

            CType(Page.FindControl(DataGrid), DataGrid).CurrentPageIndex = _

                               Math.Min(currentPageIndex + 1, pageCount - 1)

            txtPage.Text = ""

            RaiseEvent PageIndexChange(Me, e)

            Call NewPageDataBind()

        Else

            Throw New Exception(DataGrid & "を探し当てていない")

        End If

    End Sub

 

    Private Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click

        Dim currentPageIndex As Integer

        MyBase.EnsureChildControls()

 

        If Not Page.FindControl(DataGrid) Is Nothing Then

            currentPageIndex = CType(Page.FindControl(DataGrid), DataGrid).CurrentPageIndex

            CType(Page.FindControl(DataGrid), DataGrid).CurrentPageIndex = _

            Math.Max(currentPageIndex - 1, 0)

            txtPage.Text = ""

            RaiseEvent PageIndexChange(Me, e)

            Call NewPageDataBind()

        Else

            Throw New Exception(DataGrid & "を探し当てていない")

        End If

    End Sub

 

    Public Sub NewPageDataBind()

        Dim recordCount As Integer

        Dim pageCount As Integer

        Dim pageDgdSize As Integer

        Dim CurrentPageIndex As Integer

 

        CurrentPageIndex = CType(Page.FindControl(DataGrid), DataGrid).CurrentPageIndex

        pageDgdSize = CType(Page.FindControl(DataGrid), DataGrid).PageSize

        pageCount = CType(Page.FindControl(DataGrid), DataGrid).PageCount

 

        If (CType(Page.FindControl(DataGrid), DataGrid).DataSource Is Nothing) Then

            SetButtonState(0, CurrentPageIndex)

            Exit Sub

        End If

 

        If (CType(Page.FindControl(DataGrid), DataGrid).DataSource.GetType().ToString(). _

            ToLower() = "system.data.datatable") Then

            recordCount = CType(Page.FindControl(DataGrid), DataGrid).DataSource.Rows.Count

        ElseIf (CType(Page.FindControl(DataGrid), DataGrid).DataSource.GetType().ToString(). _

            ToLower() = "system.data.dataview") Then

            recordCount = CType(Page.FindControl(DataGrid), DataGrid).DataSource.Count

        ElseIf (CType(Page.FindControl(DataGrid), DataGrid).DataSource.GetType().ToString(). _

            ToLower() = "system.data.dataset") Then

            recordCount = CType(Page.FindControl(DataGrid), DataGrid).DataSource.Tables(0). _

                Rows.Count

        End If

 

        If pageCount.ToString = 0 Then

            lblNowPage.Text = 0

        Else

            lblNowPage.Text = _

                (CType(Page.FindControl(DataGrid), DataGrid).CurrentPageIndex + 1).ToString()

        End If

 

        lblAllPage.Text = "/" & pageCount.ToString() & "頁"

        lblAllItems.Text = "  全て:" & recordCount.ToString() & "件"

        SetButtonState(pageCount, CurrentPageIndex)

    End Sub

 

    Public Sub SetButtonState(ByVal pageCount As Integer, ByVal CurrentPageIndex As Integer)

        btnFirst.Enabled = (0 < CurrentPageIndex)

        btnPrevious.Enabled = (0 < CurrentPageIndex)

        btnNext.Enabled = (CurrentPageIndex < pageCount - 1)

        btnLast.Enabled = (CurrentPageIndex < pageCount - 1)

        btnGo.Enabled = (1 < pageCount)

        txtPage.Enabled = (1 < pageCount)

    End Sub

 

    Private Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click

        Dim PageCount As Integer

 

        If Not Page.FindControl(DataGrid) Is Nothing Then

            PageCount = CType(Page.FindControl(DataGrid), DataGrid).PageCount

        Else

            Throw New Exception(DataGrid & "を探し当てていない")

            Exit Sub

        End If

 

        If txtPage.Text.ToString.Equals("") Then

            Exit Sub

        Else

            If PageCount < CInt(txtPage.Text) Then

                txtPage.Text = ""

                Exit Sub

            Else

                If Math.Min(PageCount - 1, CInt(txtPage.Text) - 1) < 0 Then

                    txtPage.Text = ""

                    Exit Sub

                End If

                CType(Page.FindControl(DataGrid), DataGrid).CurrentPageIndex = _

                    Math.Min(PageCount - 1, CInt(txtPage.Text) - 1)

                txtPage.Text = ""

                RaiseEvent PageIndexChange(Me, e)

                Call NewPageDataBind()

            End If

        End If

 

    End Sub

 

    Private Sub ChangePage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

        MyBase.EnsureChildControls()

        If DataGrid Is Nothing Then

            Throw New Exception("必要なDataGridプロパティが設定されていない")

            Exit Sub

        End If

        If Not Page.FindControl(DataGrid) Is Nothing Then

            Call NewPageDataBind()

        Else

            Throw New Exception(DataGrid & "を探し当てていない")

        End If

    End Sub

End Class

  

Imports System.ComponentModel

Imports System.Web.UI

 

Public Class ChangePageBuilder

    Inherits ControlBuilder

 

    Public Overrides Function GetChildControlType(ByVal tagName As String, ByVal attributes As IDictionary) As Type

       

        If tagName.ToLower().EndsWith("newtextbox") Then

            Return GetType(newTextBox)

        End If

 

        If tagName.ToLower().EndsWith("newbutton") Then

            Return GetType(newButton)

        End If

 

        If tagName.ToLower().EndsWith("newlabel") Then

            Return GetType(newLabel)

        End If

        Return Nothing

    End Function

End Class

 

Imports System.IO

Imports System.Web.UI

Imports System.Web.UI.WebControls

Imports System.Web.UI.Design

Imports System.ComponentModel

Imports System.ComponentModel.Design

 

Public Class ChangePageDesigner

    Inherits ControlDesigner

 

    Public Overrides Function GetDesignTimeHtml() As String

        Dim strWriter As StringWriter       

        Dim htmlWriter As HtmlTextWriter   

        Dim control As ChangePage          

 

        strWriter = New StringWriter

        htmlWriter = New HtmlTextWriter(strWriter)

        control = CType(Me.Component, ChangePage)

        MyBase.GetDesignTimeHtml()

 

        If Not control.HasControls Then

            Return GetEmptyDesignTimeHtml()

        End If

 

        control.RenderControl(htmlWriter)

        Return strWriter.ToString

    End Function

 

    Public Overrides Function GetPersistInnerHtml() As String

        Dim strWriter As StringWriter       

        Dim htmlWriter As HtmlTextWriter   

        Dim control As ChangePage         

 

        strWriter = New StringWriter

        htmlWriter = New HtmlTextWriter(strWriter)

        control = CType(Me.Component, ChangePage)

        MyBase.GetPersistInnerHtml()

        htmlWriter.Write(ControlPersister.PersistControl(control.FirstPageButton))

        htmlWriter.Write(ControlPersister.PersistControl(control.PrePageButton))

        htmlWriter.Write(ControlPersister.PersistControl(control.NextPageButton))

        htmlWriter.Write(ControlPersister.PersistControl(control.LastPageButton))

        htmlWriter.Write(ControlPersister.PersistControl(control.PageTextBox))

        htmlWriter.Write(ControlPersister.PersistControl(control.PageLabel))

        htmlWriter.Write(ControlPersister.PersistControl(control.ChangePageButton))

        htmlWriter.Write(ControlPersister.PersistControl(control.NowPageLabel))

        htmlWriter.Write(ControlPersister.PersistControl(control.AllPageLabel))

        htmlWriter.Write(ControlPersister.PersistControl(control.AllItemsLabel))

        Return strWriter.ToString

    End Function

 

End Class

阅读全文
0 0

相关文章推荐

img
取 消
img