Search code examples
asp.netgridviewfooter

Adding Link Button to footer of GridView in ASP.NET


How do I do this? Basically, next to the page numbers in the GridView I want a link button to disable paging. This works, but I want the button inside the footer of the GridView, next to the page numbers. How do I do this?


Solution

  • you could add following to your codebehind(assuming VB.Net is ok):

     Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
        Select Case e.Row.RowType
            Case DataControlRowType.Footer
                Dim LnkBtnPaging As New LinkButton
                LnkBtnPaging.ID = "LnkBtnPaging"
                LnkBtnPaging.Text = IIf(GridView1.AllowPaging, "no paging", "paging").ToString
                AddHandler LnkBtnPaging.Click, AddressOf LnkBtnPagingClicked
                e.Row.Cells(0).Controls.Add(LnkBtnPaging)
                e.Row.Cells(0).ColumnSpan = e.Row.Cells.Count
                While e.Row.Cells.Count > 1
                    e.Row.Cells.RemoveAt(e.Row.Cells.Count - 1)
                End While
        End Select
    End Sub
    
    Private Sub LnkBtnPagingClicked(ByVal sender As Object, ByVal e As EventArgs)
        Me.GridView1.AllowPaging = Not Me.GridView1.AllowPaging
    End Sub
    

    Remember to change the grid's ID to your's and to add ShowFooter="True" in the GridView's Tag on the ASPX.

    EDIT: added code to auto-adjust footer's columncount and columnspan