Event not firing from dynamically added linkbutton

Discussion in 'ASP .Net' started by JoakimR, Jan 8, 2005.

  1. JoakimR

    JoakimR Guest

    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
    JoakimR, Jan 8, 2005
    #1
    1. Advertising

  2. "JoakimR" <> wrote in message
    news:...
    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
    John Saunders, Jan 8, 2005
    #2
    1. Advertising

  3. JoakimR

    JoakimR Guest

    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?
    JoakimR, Jan 8, 2005
    #3
  4. "JoakimR" <> wrote in message
    news:...
    >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
    John Saunders, Jan 8, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MS Newsgroups
    Replies:
    5
    Views:
    1,504
    Anders Borum
    Dec 13, 2003
  2. Mantorok

    LinkButton event not firing

    Mantorok, Feb 21, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    502
    Karl Seguin
    Feb 21, 2005
  3. =?Utf-8?B?TWFyYyBXb29sZnNvbg==?=

    Dynamically added LinkButton event handling

    =?Utf-8?B?TWFyYyBXb29sZnNvbg==?=, May 24, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    485
    =?Utf-8?B?TWFyYyBXb29sZnNvbg==?=
    May 25, 2007
  4. Mel
    Replies:
    6
    Views:
    514
  5. webdad3

    asp.net linkbutton event not firing

    webdad3, Jul 6, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    179
    Victor Garcia Aprea [MVP]
    Jul 7, 2003
Loading...

Share This Page