W
WolfyUK
Hello,
I have a standard asp
ataGrid called CasesGrid that I wish to write my
own paging controls for. The aim is to get something like the following
rendered to screen:
<< First < Previous 1 2 3 4 5 ... Next > Last >>
I have achieved the first/previous/next/last buttons quite easily as
follows in the ASPX (1.1) page:
<asp
anel ID="GridNavPanel" Runat="server">
<asp:LinkButton ID="FirstPageLink" Runat="server"
OnCommand="GridNavigationCommand" CommandName="first"><<
First</asp:LinkButton>
<asp:LinkButton ID="PreviousPageLink" Runat="server"
OnCommand="GridNavigationCommand" CommandName="previous"><
Previous</asp:LinkButton>
<asp:LinkButton ID="NextPageLink" Runat="server"
OnCommand="GridNavigationCommand" CommandName="next">Next
></asp:LinkButton>
<asp:LinkButton ID="LastPageLink" Runat="server"
OnCommand="GridNavigationCommand" CommandName="last">Last
>></asp:LinkButton>
</asp
anel>
and in the VB.NET codebehind:
Sub GridNavigationCommand(ByVal sender As Object, ByVal e As
CommandEventArgs)
Select Case e.CommandName
Case "first"
CasesGrid.CurrentPageIndex = 0
Case "previous"
CasesGrid.CurrentPageIndex = CasesGrid.CurrentPageIndex
- 1
Case "next"
CasesGrid.CurrentPageIndex = CasesGrid.CurrentPageIndex
+ 1
Case "last"
CasesGrid.CurrentPageIndex = CasesGrid.PageCount - 1
Case "page"
CasesGrid.CurrentPageIndex =
Integer.Parse(e.CommandArgument)
End Select
ReBindGrid(...) ' rebind the DataGrid with some sorting
attributes
End Sub
I also have a helper function to enable these link buttons only when
they should be available, which is run on the page load and every grid
postback. This function has been modified to dynamically add the direct
page number links to a panel called PagesLinkPanel, itself held within
the GridNavPanel:
Private Sub DisplayNavButtons()
' write direct page navigation
For i As Integer = 0 To CasesGrid.PageCount - 1
Dim loSkip As Boolean = False
' do not write every page number if more than ten of them
If CasesGrid.PageCount > 10 Then
If i < (CasesGrid.CurrentPageIndex - 5) Or i >
(CasesGrid.CurrentPageIndex + 5) Then
If (i + 1) Mod 10 <> 0 Then
' skip all but five pages +/- the active one
and page numbers that are multiples of 10
loSkip = True
End If
' ellipsis
If i = 0 Or i = CasesGrid.PageCount - 1 Then
Dim loCurrentPage As New Literal
loCurrentPage.Text = "... "
PagesLinkPanel.Controls.Add(loCurrentPage)
End If
End If
End If
If Not loSkip Then
' add the page number as a link if not the active page
If i = CasesGrid.CurrentPageIndex Then
Dim loCurrentPage As New Literal
loCurrentPage.Text = i + 1
PagesLinkPanel.Controls.Add(loCurrentPage)
Else
Dim loPageLink As New LinkButton
loPageLink.ID = "Page" & i + 1 & "Link"
loPageLink.Text = i + 1
loPageLink.Attributes.Add("title", "Go to page " &
i + 1)
loPageLink.CommandName = "page"
loPageLink.CommandArgument = i
AddHandler loPageLink.Command, AddressOf
Me.GridNavigationCommand ' todo: fix this problem!
PagesLinkPanel.Controls.Add(loPageLink)
End If
If i < CasesGrid.PageCount - 1 Then
Dim loSpace As New Literal
loSpace.Text = " "
PagesLinkPanel.Controls.Add(loSpace)
End If
End If
Next
' enable/disable prev/next buttons
FirstPageLink.Enabled = False
PreviousPageLink.Enabled = False
NextPageLink.Enabled = False
LastPageLink.Enabled = False
If CasesGrid.PageCount > 1 Then
If CasesGrid.CurrentPageIndex > 0 Then
' enable first/prev buttons
FirstPageLink.Enabled = True
FirstPageLink.Attributes.Add("title", "Go to page 1")
PreviousPageLink.Enabled = True
PreviousPageLink.Attributes.Add("title", "Go to page "
& CasesGrid.CurrentPageIndex)
End If
If CasesGrid.CurrentPageIndex < CasesGrid.PageCount - 1
Then
' enable next/last buttons
NextPageLink.Enabled = True
NextPageLink.Attributes.Add("title", "Go to page " &
CasesGrid.CurrentPageIndex + 2)
LastPageLink.Enabled = True
LastPageLink.Attributes.Add("title", "Go to page " &
CasesGrid.PageCount)
End If
End If
End Sub
As you can see, the line that has the comment 'fix this problem' should
assign the Command event for each of the dynamically added LinkButtons
to the GridNavigationCommand already running the next/previous
LinkButton commands. However, when debugging it can be seen that the
latter function is not called when the event is raised by clicking on a
'direct page number' link - all I get is a basic postback.
I have seen similar problems posted on other threads/forums/sites but
cannot find a working solution.
Does anyone have any ideas on this one?
Thanks in advance,
Marc
I have a standard asp
![Big Grin :D :D](/styles/default/custom/smilies/grin.gif)
own paging controls for. The aim is to get something like the following
rendered to screen:
<< First < Previous 1 2 3 4 5 ... Next > Last >>
I have achieved the first/previous/next/last buttons quite easily as
follows in the ASPX (1.1) page:
<asp
![Stick Out Tongue :P :P](/styles/default/custom/smilies/tongue.gif)
<asp:LinkButton ID="FirstPageLink" Runat="server"
OnCommand="GridNavigationCommand" CommandName="first"><<
First</asp:LinkButton>
<asp:LinkButton ID="PreviousPageLink" Runat="server"
OnCommand="GridNavigationCommand" CommandName="previous"><
Previous</asp:LinkButton>
<asp:LinkButton ID="NextPageLink" Runat="server"
OnCommand="GridNavigationCommand" CommandName="next">Next
></asp:LinkButton>
<asp:LinkButton ID="LastPageLink" Runat="server"
OnCommand="GridNavigationCommand" CommandName="last">Last
>></asp:LinkButton>
</asp
![Stick Out Tongue :P :P](/styles/default/custom/smilies/tongue.gif)
and in the VB.NET codebehind:
Sub GridNavigationCommand(ByVal sender As Object, ByVal e As
CommandEventArgs)
Select Case e.CommandName
Case "first"
CasesGrid.CurrentPageIndex = 0
Case "previous"
CasesGrid.CurrentPageIndex = CasesGrid.CurrentPageIndex
- 1
Case "next"
CasesGrid.CurrentPageIndex = CasesGrid.CurrentPageIndex
+ 1
Case "last"
CasesGrid.CurrentPageIndex = CasesGrid.PageCount - 1
Case "page"
CasesGrid.CurrentPageIndex =
Integer.Parse(e.CommandArgument)
End Select
ReBindGrid(...) ' rebind the DataGrid with some sorting
attributes
End Sub
I also have a helper function to enable these link buttons only when
they should be available, which is run on the page load and every grid
postback. This function has been modified to dynamically add the direct
page number links to a panel called PagesLinkPanel, itself held within
the GridNavPanel:
Private Sub DisplayNavButtons()
' write direct page navigation
For i As Integer = 0 To CasesGrid.PageCount - 1
Dim loSkip As Boolean = False
' do not write every page number if more than ten of them
If CasesGrid.PageCount > 10 Then
If i < (CasesGrid.CurrentPageIndex - 5) Or i >
(CasesGrid.CurrentPageIndex + 5) Then
If (i + 1) Mod 10 <> 0 Then
' skip all but five pages +/- the active one
and page numbers that are multiples of 10
loSkip = True
End If
' ellipsis
If i = 0 Or i = CasesGrid.PageCount - 1 Then
Dim loCurrentPage As New Literal
loCurrentPage.Text = "... "
PagesLinkPanel.Controls.Add(loCurrentPage)
End If
End If
End If
If Not loSkip Then
' add the page number as a link if not the active page
If i = CasesGrid.CurrentPageIndex Then
Dim loCurrentPage As New Literal
loCurrentPage.Text = i + 1
PagesLinkPanel.Controls.Add(loCurrentPage)
Else
Dim loPageLink As New LinkButton
loPageLink.ID = "Page" & i + 1 & "Link"
loPageLink.Text = i + 1
loPageLink.Attributes.Add("title", "Go to page " &
i + 1)
loPageLink.CommandName = "page"
loPageLink.CommandArgument = i
AddHandler loPageLink.Command, AddressOf
Me.GridNavigationCommand ' todo: fix this problem!
PagesLinkPanel.Controls.Add(loPageLink)
End If
If i < CasesGrid.PageCount - 1 Then
Dim loSpace As New Literal
loSpace.Text = " "
PagesLinkPanel.Controls.Add(loSpace)
End If
End If
Next
' enable/disable prev/next buttons
FirstPageLink.Enabled = False
PreviousPageLink.Enabled = False
NextPageLink.Enabled = False
LastPageLink.Enabled = False
If CasesGrid.PageCount > 1 Then
If CasesGrid.CurrentPageIndex > 0 Then
' enable first/prev buttons
FirstPageLink.Enabled = True
FirstPageLink.Attributes.Add("title", "Go to page 1")
PreviousPageLink.Enabled = True
PreviousPageLink.Attributes.Add("title", "Go to page "
& CasesGrid.CurrentPageIndex)
End If
If CasesGrid.CurrentPageIndex < CasesGrid.PageCount - 1
Then
' enable next/last buttons
NextPageLink.Enabled = True
NextPageLink.Attributes.Add("title", "Go to page " &
CasesGrid.CurrentPageIndex + 2)
LastPageLink.Enabled = True
LastPageLink.Attributes.Add("title", "Go to page " &
CasesGrid.PageCount)
End If
End If
End Sub
As you can see, the line that has the comment 'fix this problem' should
assign the Command event for each of the dynamically added LinkButtons
to the GridNavigationCommand already running the next/previous
LinkButton commands. However, when debugging it can be seen that the
latter function is not called when the event is raised by clicking on a
'direct page number' link - all I get is a basic postback.
I have seen similar problems posted on other threads/forums/sites but
cannot find a working solution.
Does anyone have any ideas on this one?
Thanks in advance,
Marc