1

I am building a custom ASP.Net gridview server control (not user control) with a custom pager. The custom pager contains a dynamically added dropdownlist which changes the gridview's pagesize (items per page). The dropdownlist is added in the RenderChildren method which I am overriding in the server control's code.

I am basically cloning the default pager row and adding the dropdownlist into the row, then adding the cloned pager row to the gridview and destroying the original.

The problem is that even though the dropdownlist's autopostback property is set to true and I am adding an event handler for it, I cannot get the event to fire when changing the selectedindex. I have tried several different approaches with no luck.

I believe the problem is that the dropdownlist has not yet been rendered by the time the selectedindex is changed in the calling page. But I don't know enough about server controls and the page lifecycle to figure out how to resolve it.

Here is an abbreviated version of the code:

Public Class CustomGridView
    Inherits System.Web.UI.WebControls.GridView
    Implements IPostBackEventHandler

    Protected WithEvents mctlDropdownListPageSize As New DropDownList
    Public Event PageSizeChanged(ByVal sender As Object, ByVal e As EventArgs)

    Protected Sub dropdownListPageSize_Click(ByVal sender As Object, ByVal e As EventArgs)
        RaiseEvent PageSizeChanged(Me.mctlDropdownListPageSize, e)
    End Sub

    'This sets up paging using the pageddatasource
    Protected Overrides Sub InitializePager(row As GridViewRow, columnSpan As Integer, pagedDataSource As PagedDataSource)
        pagedDataSource.AllowPaging = True
        pagedDataSource.AllowCustomPaging = True
        pagedDataSource.VirtualCount = TotalRows
        pagedDataSource.CurrentPageIndex = CurrentPageIndex
        Me.PageIndex = CurrentPageIndex
        MyBase.InitializePager(row, columnSpan, pagedDataSource)
    End Sub

    Protected Overrides Sub RenderChildren(writer As HtmlTextWriter)
        Try
            If Me.Controls.Count = 0 Then
                ' nothing to render, use default
                MyBase.RenderChildren(writer)
                Return
            End If

            ' select the header row in the grid
            Dim gridViewTable As WebControl = DirectCast(Me.Controls(0), WebControl) ' the table

            ' the table->row(0) if no top paging, otherwise row(1)
            Dim pagerRow As GridViewRow = gridViewTable.Controls(0)

            'Get the pager cell -- we want only one cell containing all the elements
            Dim pagerContainerCell As TableCell = pagerRow.Cells(0)

            'Create the new table which will contain the title, results summary and pager
            Dim newPagerTable As New Table
            newPagerTable.CellPadding = 0
            newPagerTable.CellSpacing = 0
            newPagerTable.CssClass = "section_headers gridpagertable"

            'Create a table row to contain the new cells
            Dim newPagerRow As New TableRow
            newPagerRow.CssClass = "pagerrow"

            'Create 2 cells to hold the new controls + pager
            Dim resultsCell As New TableCell
            resultsCell.CssClass = "results"
            Dim pagerCell As New TableCell
            pagerCell.CssClass = "pager"

            If Me.AllowPaging Then
                'Add the pagesize dropdown and results summary text
                'Create the results label 
                Dim mctlResultsLabelText As New LiteralControl
                mctlResultsLabelText.Text = "<span>Results per page </span>"

                'Create the pagesize dropdown container div
                Dim mctlResultsDropdownListContainerDiv As New HtmlControls.HtmlGenericControl("div")
                mctlResultsDropdownListContainerDiv.Attributes("class") = "dropdown_select"

                Dim mctlResultsDropdownListLabel As New HtmlControls.HtmlGenericControl("label")

                'Create the pagesize dropdownlist
                mctlDropdownListPageSize.ID = "dropDownListPageSize"
                mctlDropdownListPageSize.Items.Add(New ListItem("100", "100"))
                mctlDropdownListPageSize.Items.Add(New ListItem("200", "200"))
                mctlDropdownListPageSize.Items.Add(New ListItem("300", "300"))
                mctlDropdownListPageSize.AutoPostBack = True
                mctlDropdownListPageSize.EnableViewState = True
                AddHandler mctlDropdownListPageSize.SelectedIndexChanged, AddressOf Me.dropdownListPageSize_Click

                'Add the dropdown tot he dropdown container div
                mctlResultsDropdownListLabel.Controls.Add(mctlDropdownListPageSize)
                mctlResultsDropdownListContainerDiv.Controls.Add(mctlResultsDropdownListLabel)

                'Add the results drpdown label
                resultsCell.Controls.Add(mctlResultsLabelText)
                'Add the pagesize dropdown
                resultsCell.Controls.Add(mctlResultsDropdownListContainerDiv)
                'Add the cell to the row
                newPagerRow.Controls.Add(resultsCell)
            End If

            'Add the pager control and action icons
            Dim mctlPagerContainerDiv As New HtmlControls.HtmlGenericControl("div")
            mctlPagerContainerDiv.Attributes.Add("class", "pagination")


            'Add the div to the pager cell
            pagerCell.Controls.Add(mctlPagerContainerDiv)

            If Me.AllowPaging Then
                'Get the existing pager container table with the pager buttons
                Dim tblPager As Table
                tblPager = pagerContainerCell.Controls(0)
                tblPager.CellPadding = 0
                'Add the pager to the cell
                pagerCell.Controls.Add(tblPager)
            End If

            'Add the cell to the row
            newPagerRow.Controls.Add(pagerCell)

            'Add the row to the table
            newPagerTable.Controls.Add(newPagerRow)
            'Render the new pager row (table+row+3 new cells with controls)
            newPagerTable.RenderControl(writer)

            If Me.AllowPaging Then
                ' remove the original (default) pager row, otherwise we have two
                gridViewTable.Controls.RemoveAt(0)
            End If

            'Render the gridview
            gridViewTable.RenderControl(writer)

        Catch ex As Exception
            ControlErrorHandler(ex, "RenderChildren")
        End Try

    End Sub

    Private Sub ControlErrorHandler(ByVal ex As Exception, ByVal methodName As String)
        Throw New ApplicationException(Me.GetType().ToString & "." & methodName & " failed due to the inner error - " & ex.Message, ex)
    End Sub

    <Description("The current page index of the gridview."), DefaultValue(""), Themeable(False), Category("Appearance")> _
    Public Property CurrentPageIndex() As Integer
        Get
            If Not ViewState(VSKeyCurrPageIndex) Is Nothing Then
                Return Integer.Parse(ViewState(VSKeyCurrPageIndex))
            Else
                Return 0
            End If
        End Get
        Set(value As Integer)
            ViewState(VSKeyCurrPageIndex) = value
        End Set
    End Property


End Class

The calling page has this:

Private Sub gridViewResults_PageSizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridViewResults.PageSizeChanged
        '
    End Sub

I have looked at almost every related issue here on Stackoverflow, as well as other possible solutions in posts and article online. Any insight or suggestions will be appreciated.

Nick P
  • 33
  • 3
  • Hi and welcome on Stackoverflow. This is a good question for SO, thanks for posting it. The reason that you got no answer so far, is probably more due to the actual problem, not the question itself. Good luck anyway. Remember, in case you found the solution yourself, you are very welcome to post it as an answer to your own question. This may be very helpful to others and helps to prevent reposts of this question in the future. – Marcel Jun 26 '14 at 08:18
  • Have you looked into this question: http://stackoverflow.com/q/570294/79485 Although this is not exactly a duplicate, it could contain useful information for you. – Marcel Jun 26 '14 at 08:21

0 Answers0