Event not firing from dynamically added linkbutton

J

JoakimR

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
 
J

John Saunders

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
 
J

JoakimR

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?
 
J

John Saunders

JoakimR said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top