Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Event not firing from dynamically added linkbutton

Reply
Thread Tools

Event not firing from dynamically added linkbutton

 
 
JoakimR
Guest
Posts: n/a
 
      01-08-2005
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

 
Reply With Quote
 
 
 
 
John Saunders
Guest
Posts: n/a
 
      01-08-2005
"JoakimR" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
JoakimR
Guest
Posts: n/a
 
      01-08-2005
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?

 
Reply With Quote
 
John Saunders
Guest
Posts: n/a
 
      01-08-2005
"JoakimR" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LinkButton click event not firing Mel ASP .Net 6 06-12-2007 01:23 PM
Dynamically added LinkButton event handling =?Utf-8?B?TWFyYyBXb29sZnNvbg==?= ASP .Net 5 05-25-2007 11:15 AM
LinkButton event not firing Mantorok ASP .Net 1 02-21-2005 01:44 PM
Event firing with dynamically added server controls MS Newsgroups ASP .Net 5 12-13-2003 02:56 PM
asp.net linkbutton event not firing webdad3 ASP .Net Building Controls 1 07-07-2003 12:11 AM



Advertisments