473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

LinkButton Event Handling (Dynamic controls)

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
2 2680
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
3
by: CodeRazor | last post by:
Hi, I am trying to dynamically create linkbuttons. They need an event handler, so i can respond to the user's click. I try to add the eventhandler on the fly, but when i click on the link, the...
0
by: Pat Sagaser via .NET 247 | last post by:
I'm trying to add LinkButtons to a Repeater control using adynamic template. The docs state that you should be able tobubble the click event to the containing Repeater. There areplenty of examples in...
5
by: UnknownServices | last post by:
I've added a LinkButton to a repeater control and I set the link button as follows: <asp:LinkButton ID="DLLinkButton" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "DownloadID")%>'...
4
by: Fueled | last post by:
Hi everyone! I've made quite a lot of research on this, and I've tried a couple of proposed solutions. Nothing has worked for me, but I feel there's not much I'm missing. So I'm turning to this...
1
by: geronimi | last post by:
I want to create a linkbutton in a cell because not every row needs one (so I can't setup a linkbuttoncolumn instead of a boundcolumn.) First, i create a linkbutton in the datagrid_ItemDataBound...
1
by: Andrew Robinson | last post by:
I have a <asp:table> control with a large number of dynamically created LinkButtons. I am using the command event, command name and command argument values in my LinkButtons to trigger actions...
13
by: rn5a | last post by:
In a shopping cart app, suppose a user has placed 5 orders, I want to show him 5 LinkButtons (one for each order) so that when he clicks the first LinkButton, he would be shown the details of his...
5
by: =?Utf-8?B?TWFyYyBXb29sZnNvbg==?= | last post by:
Hi, I have a strange issue occurring with LinkButtons that are dynamically added to each (data) row of my DataGrid on that grid's ItemDataBound event. Each LinkButton is assigned its own event...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.