locked
Custom PagerTemplate not firing pageIndexChanging event. RRS feed

  • Question

  • User1482735129 posted

    I have a custom pagerTemplate class . Everything works with it but it doesnt call my gridviews pageIndexchanging or the pageIndexchanged event.  I need to do some additional coding in the changed event so Ia m needing it to fire.  Anybody know why when you use a custom class it wouldnt fire this event.

     

    here is my class

    Imports System.Drawing
    Imports System.Web.UI
    Imports System.Web.UI.WebControls

    'ITemplate - Defines the behavior for populating a templated ASP.NET server control with child controls.
    'The child controls represent the inline templates defined on the page.
    Public Class PagerTemplate
        Inherits TemplateControl
        Implements ITemplate, INamingContainer
    #Region "Constructors"
        Public Sub New()
     
            MyBase.New()
            'Setting the default colors
            ForeColor = Color.White
            DisabledForeColor = Color.Gray
            BackColor = ColorTranslator.FromHtml("#284775")
            FontName = "Arial"
            FontSize = 12
            FontStyle = Drawing.FontStyle.Regular
        End Sub

     

        Public Sub New(ByVal ForeColorValue As Color, ByVal DisabledForeColorValue As Color, ByVal BackColorValue As Color, ByVal FontNameValue As String, ByVal FontSizeValue As Integer, ByVal FontStyleValue As FontStyle)


            ForeColor = ForeColorValue
            DisabledForeColor = DisabledForeColorValue
            BackColor = BackColorValue
            FontName = FontNameValue
            FontSize = FontSizeValue
            FontStyle = FontStyleValue

        End Sub
    #End Region

    #Region "Data"
        Private WithEvents NextLinkBtn As New LinkButton
        Private WithEvents PrevLinkBtn As New LinkButton
        Private WithEvents FirstLinkBtn As New LinkButton
        Private WithEvents LastLinkBtn As New LinkButton
        Private LblTotalRecs As New Label
        Private LblPages As New Label
        Private WithEvents _container As Control
        Private CurrentPage As Integer
        Private TotalPages As Integer
        Private mTotRecCount As Integer
        Private mForeColor As Color
        Private mBackColor As Color
        Private mDisabledForeColor As Color
        Private mFontName As String
        Private mFontSize As Integer
        Private mFontStyle As FontStyle
        Private gridView As GridView

     

     

        Public Property ForeColor() As Color
            Get
                Return mForeColor
            End Get
            Set(ByVal value As Color)
                mForeColor = value
            End Set
        End Property

        Public Property BackColor() As Color
            Get
                Return mBackColor
            End Get
            Set(ByVal value As Color)
                mBackColor = value
            End Set
        End Property

        Public Property DisabledForeColor() As Color
            Get
                Return mDisabledForeColor
            End Get
            Set(ByVal value As Color)
                mDisabledForeColor = value
            End Set
        End Property

        Public Property FontName() As String
            Get
                Return mFontName
            End Get
            Set(ByVal value As String)
                mFontName = value
            End Set
        End Property

        Public Property FontSize() As Integer
            Get
                Return mFontSize
            End Get
            Set(ByVal value As Integer)
                mFontSize = value
            End Set
        End Property

        Public Property FontStyle() As FontStyle
            Get
                Return mFontStyle
            End Get
            Set(ByVal value As FontStyle)
                mFontStyle = value
            End Set
        End Property

    #End Region

    #Region "Local Sub/Functions"

        Public Sub InstantiateIn(ByVal container As Control) Implements System.Web.UI.ITemplate.InstantiateIn
      Dim PagerTable As New Table
            Dim PagerTableRow As New TableRow

            'Creating the cells in the table.
            Dim cellLeft As New TableCell
            Dim cellMiddle As New TableCell
            Dim cellRight As New TableCell
       
            Try
                _container = container

                LblTotalRecs.Text = String.Empty
                LblTotalRecs.ForeColor = ForeColor 'White

                cellLeft.Controls.Add(LblTotalRecs)
                cellLeft.Controls.Add(New LiteralControl("    "))
                PagerTableRow.Cells.Add(cellLeft)

                'Create and defining the navigation buttons for the pager.
                FirstLinkBtn.Text = "First"
                FirstLinkBtn.ForeColor = ForeColor 'White
                cellMiddle.Controls.Add(FirstLinkBtn)
                cellMiddle.Controls.Add(New LiteralControl("    "))

                NextLinkBtn.Text = "Next"
                NextLinkBtn.ForeColor = ForeColor 'White
                cellMiddle.Controls.Add(NextLinkBtn)
                cellMiddle.Controls.Add(New LiteralControl("    "))

                PrevLinkBtn.Text = "Prev"
                PrevLinkBtn.ForeColor = ForeColor 'White
                cellMiddle.Controls.Add(PrevLinkBtn)
                cellMiddle.Controls.Add(New LiteralControl("    "))

                LastLinkBtn.Text = "Last"
                LastLinkBtn.ForeColor = ForeColor 'White
                cellMiddle.Controls.Add(LastLinkBtn)
                cellMiddle.Controls.Add(New LiteralControl("    "))
                PagerTableRow.Cells.Add(cellMiddle)


                LblPages.Text = String.Empty
                LblPages.ForeColor = ForeColor 'White
                cellRight.Controls.Add(LblPages)
                PagerTableRow.Cells.Add(cellRight)

                'Adding the style to the cells
                cellLeft.Style.Add(HtmlTextWriterStyle.WhiteSpace, "nowrap")
                cellLeft.Style.Add(HtmlTextWriterStyle.TextAlign, "left")

                cellMiddle.Style.Add(HtmlTextWriterStyle.WhiteSpace, "nowrap")
                cellMiddle.Style.Add(HtmlTextWriterStyle.TextAlign, "center")

                cellRight.Style.Add(HtmlTextWriterStyle.WhiteSpace, "nowrap")
                cellRight.Style.Add(HtmlTextWriterStyle.TextAlign, "right")
                PagerTable.CellPadding = 0
                PagerTable.CellSpacing = 0
                PagerTable.Width = Unit.Percentage(100)
                PagerTableRow.HorizontalAlign = HorizontalAlign.Left
                PagerTableRow.VerticalAlign = VerticalAlign.Middle
                ' PagerTableRow.ForeColor = ForeColor
                PagerTableRow.BackColor = BackColor
                PagerTableRow.Font.Bold = FontStyle
                PagerTableRow.Font.Name = FontName
                PagerTableRow.Font.Size = FontSize

                PagerTable.Rows.Add(PagerTableRow)

                _container.Controls.Add(PagerTable)

     

           

            Catch ex As Exception
                Throw
            Finally
                cellLeft.Dispose()
                cellMiddle.Dispose()
                cellRight.Dispose()
                PagerTable.Dispose()
                PagerTableRow.Dispose()
            End Try


        End Sub


        Private Sub FormatPager(ByVal gridView As System.Web.UI.WebControls.GridView)

            Try


                Dim PageIndex As Integer


                'Move first and previous
                If (gridView.PageIndex = 0) Then
                    FirstLinkBtn.ForeColor = DisabledForeColor
                    PrevLinkBtn.ForeColor = DisabledForeColor
                    FirstLinkBtn.Enabled = False
                    PrevLinkBtn.Enabled = False

                Else
                    FirstLinkBtn.ForeColor = ForeColor
                    PrevLinkBtn.ForeColor = ForeColor
                    FirstLinkBtn.Enabled = True
                    PrevLinkBtn.Enabled = True

                End If

                'Move next and last
                If (gridView.PageIndex < gridView.PageCount - 1) Then
                    NextLinkBtn.ForeColor = ForeColor
                    LastLinkBtn.ForeColor = ForeColor
                    NextLinkBtn.Enabled = True
                    LastLinkBtn.Enabled = True

                Else
                    NextLinkBtn.ForeColor = DisabledForeColor
                    NextLinkBtn.ForeColor = DisabledForeColor
                    NextLinkBtn.Enabled = False
                    LastLinkBtn.Enabled = False

                End If

                LblTotalRecs.Text = "Total Records: " & TotRecCount
                TotalPages = gridView.PageCount
                PageIndex = gridView.PageIndex
                CurrentPage = PageIndex + 1
                LblPages.Text = "Page " & CurrentPage & " of " & TotalPages

            Catch ex As Exception
                Throw
            End Try
        End Sub

        Private Sub FirstLinkBtn_Click(ByVal Sender As Object, ByVal e As EventArgs) Handles FirstLinkBtn.Click
     
            Try
                gridView = _container.NamingContainer.NamingContainer
                gridView.PageIndex = 0
                gridView.SelectedIndex = -1
            
                FormatPager(gridView)
            Catch ex As Exception
                Throw
            End Try

        End Sub

        Private Sub NextLinkBtn_Click(ByVal Sender As Object, ByVal e As EventArgs) Handles NextLinkBtn.Click
     
            Try
                gridView = _container.NamingContainer.NamingContainer

                If (gridView.PageIndex < gridView.PageCount - 1) Then
                    gridView.PageIndex = gridView.PageIndex + 1
                    gridView.SelectedIndex = -1
                End If

         

                FormatPager(gridView)

            Catch ex As Exception
                Throw
            End Try

        End Sub

        Private Sub PrevLinkBtn_Click(ByVal Sender As Object, ByVal e As EventArgs) Handles PrevLinkBtn.Click

            Try
                gridView = _container.NamingContainer.NamingContainer

                If (gridView.PageIndex > 0) Then
                    gridView.PageIndex = gridView.PageIndex - 1
                    gridView.SelectedIndex = -1
                End If

                FormatPager(gridView)
            Catch ex As Exception
                Throw
            End Try

     

        End Sub

        Private Sub LastLinkBtn_Click(ByVal Sender As Object, ByVal e As EventArgs) Handles LastLinkBtn.Click

            Try
                gridView = _container.NamingContainer.NamingContainer

                gridView.PageIndex = gridView.PageCount - 1
                gridView.SelectedIndex = -1

                FormatPager(gridView)
            Catch ex As Exception
                Throw
            End Try


        End Sub

        Private Sub Container_DataBinding(ByVal sender As Object, ByVal e As EventArgs) Handles _container.DataBinding

            Try
                gridView = _container.NamingContainer.NamingContainer
                FormatPager(gridView)
            Catch ex As Exception
                Throw
            End Try


        End Sub

     

    #End Region

    #Region "IDisposable Support"

    #End Region


    End Class

    Wednesday, October 22, 2008 10:07 AM

Answers

All replies