Adding Link Button to footer of GridView in ASP.NE

2019-05-18 14:36发布

问题:

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?

回答1:

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



回答2:

You can create GridView's PagerTemplate. In that case you will probably have to create whole pager from scratch.

You can also create your own control derived from GridView and override InitializePager. In this method you will call base.InitializePager(...) and after that you will navigate in created control structure and add your link.

Edit: Tim's solution is better. My solution works but when you disable paging the link will be hidden together with pager.