By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,088 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,088 IT Pros & Developers. It's quick & easy.

LinkButton Event Handling (Dynamic controls)

P: n/a
Hello,

I have a standard asp:DataGrid 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:Panel ID="GridNavPanel" Runat="server">
<asp:LinkButton ID="FirstPageLink" Runat="server"
OnCommand="GridNavigationCommand" CommandName="first">&lt;&lt;
First</asp:LinkButton>
<asp:LinkButton ID="PreviousPageLink" Runat="server"
OnCommand="GridNavigationCommand" CommandName="previous">&lt;
Previous</asp:LinkButton>
<asp:LinkButton ID="NextPageLink" Runat="server"
OnCommand="GridNavigationCommand" CommandName="next">Next
&gt;</asp:LinkButton>
<asp:LinkButton ID="LastPageLink" Runat="server"
OnCommand="GridNavigationCommand" CommandName="last">Last
&gt;&gt;</asp:LinkButton>
</asp:Panel>

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

Oct 6 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
If I under stand your description above , all you have to do is call your
helper function ( which generates dynamic controls)
on page init not on page load .

thanks
Baski

"WolfyUK" <we*******@first-title.co.ukwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
Hello,

I have a standard asp:DataGrid 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:Panel ID="GridNavPanel" Runat="server">
<asp:LinkButton ID="FirstPageLink" Runat="server"
OnCommand="GridNavigationCommand" CommandName="first">&lt;&lt;
First</asp:LinkButton>
<asp:LinkButton ID="PreviousPageLink" Runat="server"
OnCommand="GridNavigationCommand" CommandName="previous">&lt;
Previous</asp:LinkButton>
<asp:LinkButton ID="NextPageLink" Runat="server"
OnCommand="GridNavigationCommand" CommandName="next">Next
&gt;</asp:LinkButton>
<asp:LinkButton ID="LastPageLink" Runat="server"
OnCommand="GridNavigationCommand" CommandName="last">Last
&gt;&gt;</asp:LinkButton>
</asp:Panel>

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

Oct 6 '06 #2

P: n/a
Hi,

Thanks for the reply.

The problem with running that procedure from the page init is that the
DataGrid has not databound yet, and so it is unknown which
controls/control properties to add to the page. I also thought it was
bad practice to play around with the page init?

I have managed to successfully perform a similar task in the past upon
page load, but this was binding custom buttons _within_ a DataGrid
during its ItemDataBound event to that grid's ItemCommand event.

Marc

On Oct 6, 7:53 pm, "Baski" <toba...@google.comwrote:
If I under stand your description above , all you have to do is call your
helper function ( which generates dynamic controls)
on page init not on page load .

thanks
Baski
Oct 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.