将链接按钮添加到ASP.NET中GridView的页脚

时间:2010-10-12 09:35:57

标签: asp.net gridview footer

我该怎么做?基本上,在GridView中的页码旁边,我想要一个链接按钮来禁用分页。这有效,但我希望GridView页脚内的按钮位于页码旁边。我该怎么做?

2 个答案:

答案 0 :(得分:2)

你可以在你的代码隐藏中添加以下内容(假设VB.Net没问题):

 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

请记住将网格的ID更改为您的ID,并在ASPX的GridView标签中添加ShowFooter="True"

编辑:添加代码以自动调整页脚的columncount和columnspan

答案 1 :(得分:0)

您可以创建GridView的PagerTemplate。在这种情况下,您可能需要从头开始创建整个寻呼机。

您还可以创建从GridView派生的自己的控件并覆盖InitializePager。在此方法中,您将调用base.InitializePager(...),之后您将在创建的控制结构中导航并添加链接。

编辑:蒂姆的解决方案更好。我的解决方案有效,但当您禁用分页时,链接将与寻呼机一起隐藏。