473,385 Members | 1,907 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,385 software developers and data experts.

Event not firing from dynamically added linkbutton

Hello,
I have a problem with events not firing. I've created a web user
control which renders a calendar using a table control. In two cells
I'm adding LinkButtons for "prev/next month". However, the events from
these are not fired.

In the PageLoad event I check if it's a PostBack. If it isn't, I'm
calling DisplayCalendar() to render the calendar. This part works and I
get the output. If it *is* a postback (i.e. one of the linkbuttons are
clicked), the PageLoad skipps to call DisplayCalendar(), but then the
event handler for either LinkButton is supposed to be fired next
(right?) - in which the current month is updated and then the
DisplayCalendar() would be called. This is not happening, what am I
missing?? Code snippet below:

Public Class calendar
Inherits System.Web.UI.UserControl

Protected WithEvents m_lnkPrevMonth As New
Web.UI.WebControls.LinkButton
Protected WithEvents m_lnkNextMonth As New
Web.UI.WebControls.LinkButton

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If (Not IsPostBack) Then
DisplayCalendar()
End If
End Sub

Public Sub PrevMonth_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles m_lnkPrevMonth.Click
CurrentDate = DateAdd(DateInterval.Month, -1, CurrentDate)
DisplayCalendar()
End Sub

Public Sub NextMonth_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles m_lnkNextMonth.Click
CurrentDate = DateAdd(DateInterval.Month, 1, CurrentDate)
DisplayCalendar()
End Sub

Private Property CurrentDate() As Date
'Date stored in ViewState
....
End Property

Private Sub DisplayCalendar()
'Render the calendar
Dim objTable As New Web.UI.WebControls.Table
Dim objRow As Web.UI.WebControls.TableRow
Dim objCell As Web.UI.WebControls.TableCell
....
'Add link
m_lnkPrevMonth.Text = "&#171" ' «
objCell.Controls.Add(m_lnkPrevMonth)
objRow.Controls.Add(objCell)
....
'Add link
m_lnkNextMonth.Text = "&#187" ' »
objCell.Controls.Add(m_lnkNextMonth)
objRow.Controls.Add(objCell)
....

'Output the control
Me.Controls.Add(objTable)
End Sub

End Class

Nov 19 '05 #1
3 4285
"JoakimR" <jo**************@cybercomgroup.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hello,
I have a problem with events not firing. I've created a web user
control which renders a calendar using a table control. In two cells
I'm adding LinkButtons for "prev/next month". However, the events from
these are not fired.

In the PageLoad event I check if it's a PostBack. If it isn't, I'm
calling DisplayCalendar() to render the calendar. This part works and I
get the output. If it *is* a postback (i.e. one of the linkbuttons are
clicked), the PageLoad skipps to call DisplayCalendar(), but then the
event handler for either LinkButton is supposed to be fired next
(right?)

No. You have to recreate all of the controls on every request - whether or
not it's a PostBack.

John Saunders
Nov 19 '05 #2
I do recreate the controls - but if it's a postback I have to "wait"
until I've updated the "month" according to which LinkButton was
clicked. I tried to always call DisplayCalendar() in PageLoad, but then
I get two calendars on the same page, one with the "previous" month and
one with the "new". So, I added a line "Me.Controls.Clear" before
"Me.Controls.Add(objTable)" in the DisplayCalendar sub - but I thought
that was just a ugly work-around... Is there any other way?

Nov 19 '05 #3
"JoakimR" <jo**************@cybercomgroup.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I do recreate the controls - but if it's a postback I have to "wait"
until I've updated the "month" according to which LinkButton was
clicked. I tried to always call DisplayCalendar() in PageLoad, but then
I get two calendars on the same page, one with the "previous" month and
one with the "new". So, I added a line "Me.Controls.Clear" before
"Me.Controls.Add(objTable)" in the DisplayCalendar sub - but I thought
that was just a ugly work-around... Is there any other way?


Actually, you're doing it the wrong way.

At the beginning of a PostBack, you have to recreate the same control
hierarchy which was present at the end of the previous request.

You can then take into account any events which happen on this request.

You can then clear the Controls collection and create a new control
hierarchy reflecting your new events.

On the next PostBack, you will have to recreate this latter control
hierarchy...

John Saunders
Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Solomon Shaffer | last post by:
This is very interesting - and odd. I have a number of LinkButtons that are created in a custom data grid that I created. The purpose of the LinkButtons is to provide alphabetical filtering...
4
by: vatech1993 | last post by:
I'm stumped on this problem. I've created a user control that dynamically creates 5 linkbuttons in the CreateChildControls method. Each of these child controls is linked to a commandeventhandler,...
1
by: Mantorok | last post by:
Hi I have a control generator that is creating controls on the fly, one of the controls is a LinkButton, the problem is that the Click event is not firing, any ideas? Example: private void...
2
by: David | last post by:
Hi all, I am dynamically creating a LinkButton in my datagrid, however, the event that I am creating for it is not firing. I am obviously missing something? What am I missing? Here is the...
0
by: Demetri | last post by:
I have created a web control that can be rendered as either a linkbutton or a button. It is a ConfirmButton control that allows a developer to force a user to confirm if they intended to click it...
2
by: WolfyUK | last post by:
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...
2
by: =?Utf-8?B?Uml0YUc=?= | last post by:
I posted this question in the C# discussion group but am posting it here also since ASP is involved. I'm new to C# and need some help regarding an onClick event not firing. I have a data grid...
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...
6
by: Mel | last post by:
I have a website that allows the user to generate a new quote. When they are finished creating the quote, it brings them to the final page (called the Goodbye.aspx page) which just states the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.