10 Replies - 1445 Views - Last Post: 04 April 2013 - 08:34 PM Rate Topic: -----

#1 tvellalott   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 15-August 12

Dynamically Created Button NOT Firing

Posted 04 April 2013 - 05:37 PM

    Private Sub populatePHcomments()

        Dim tbComments As TextBox = New TextBox
        Dim btnActivateComments As New Button
        If activateComments = True Then
            tbComments.Height = 100
            tbComments.Width = 400
            phComments.Controls.Add(tbComments)
        Else
            btnActivateComments.Text = "Comments?"
            phComments.Controls.Add(btnActivateComments)
            AddHandler btnActivateComments.Command, AddressOf btnComments_Click
        End If
    End Sub

    Public Sub btnComments_Click(ByVal sender As Object, ByVal e As WebControls.CommandEventArgs)
        MsgBox("Click registered", MsgBoxStyle.Critical)
    End Sub



The populatePHcomments section works.
The If statement fires.
The button is created.
The AddHandler (which is what every tutorial on dynamic button creation I've read (at least 10 now) suggests I use, in exactly this syntax) doesn't seem to work. When you click the button the button disappears (which is fine actually, because clicking the button is supposed to rerun the populatePHcomments sub but with activateComments set to true) and the message box is not displayed. I've also tried a .Click event like this...

AddHandler btnActivateComments.Click, AddressOf btnComments_Click



With the _click subroutine looking like this...
    Private Sub btnComments_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MsgBox("Click registered", MsgBoxStyle.Critical)
    End Sub



I have absolutely no idea what's wrong here.

Is This A Good Question/Topic? 0
  • +

Replies To: Dynamically Created Button NOT Firing

#2 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1532
  • View blog
  • Posts: 3,859
  • Joined: 01-February 13

Re: Dynamically Created Button NOT Firing

Posted 04 April 2013 - 05:55 PM

Hi,
I would try changing this line back to the Click event
AddHandler btnActivateComments.Click, AddressOf btnComments_Click


and changing the sub to use (e as System.EventArgs)
    Public Sub btnComments_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        MsgBox("Click registered", MsgBoxStyle.Critical)
    End Sub


:bigsmile:
Was This Post Helpful? 0
  • +
  • -

#3 tvellalott   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 15-August 12

Re: Dynamically Created Button NOT Firing

Posted 04 April 2013 - 06:01 PM

Thanks for the fast reply.
Unfortunately, I've already tried that and it has the same result; the button disappears and the _click sub is not called.

There is something more insidious here...
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Dynamically Created Button NOT Firing

Posted 04 April 2013 - 06:22 PM

Is this Winforms or ASPX?
Was This Post Helpful? 0
  • +
  • -

#5 tvellalott   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 15-August 12

Re: Dynamically Created Button NOT Firing

Posted 04 April 2013 - 06:23 PM

The populatePHcomments sub populates a ASP:Placeholder
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Dynamically Created Button NOT Firing

Posted 04 April 2013 - 07:03 PM

The button does not exist after postback - so nor does the delegate for the event.

Persisting Controls thru postback
Was This Post Helpful? 0
  • +
  • -

#7 tvellalott   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 15-August 12

Re: Dynamically Created Button NOT Firing

Posted 04 April 2013 - 07:31 PM

I think, upon reading that thread that I'm approaching this in the wrong way anyway; the controls don't need to be created dynamically. They just need to be hidden. The reason I was creating them dynamically is that even if you make Visible = False they still take up a certain amount of space. I'm going to get my Google on and find a better way of achieving what I want.
Was This Post Helpful? 0
  • +
  • -

#8 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Dynamically Created Button NOT Firing

Posted 04 April 2013 - 07:49 PM

Sometimes when you ask for guidance if you describe what your doing we may have other ideas to offer.
Was This Post Helpful? 0
  • +
  • -

#9 lar3ry   User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: Dynamically Created Button NOT Firing

Posted 04 April 2013 - 07:50 PM

View Posttvellalott, on 04 April 2013 - 08:31 PM, said:

I think, upon reading that thread that I'm approaching this in the wrong way anyway; the controls don't need to be created dynamically. They just need to be hidden. The reason I was creating them dynamically is that even if you make Visible = False they still take up a certain amount of space. I'm going to get my Google on and find a better way of achieving what I want.

If, by "still take up a certain amount of space", you are referring to window real estate, I wonder if you are aware that you can "park" the control off-window, or "park" them within the window bounds, overlapped, when not visible, making them fairly easy to deal with in the designer. Then you can move them into position and make them visible as needed.
Was This Post Helpful? 0
  • +
  • -

#10 tvellalott   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 15-August 12

Re: Dynamically Created Button NOT Firing

Posted 04 April 2013 - 07:51 PM

View Postlar3ry, on 04 April 2013 - 07:50 PM, said:

View Posttvellalott, on 04 April 2013 - 08:31 PM, said:

I think, upon reading that thread that I'm approaching this in the wrong way anyway; the controls don't need to be created dynamically. They just need to be hidden. The reason I was creating them dynamically is that even if you make Visible = False they still take up a certain amount of space. I'm going to get my Google on and find a better way of achieving what I want.

If, by "still take up a certain amount of space", you are referring to window real estate, I wonder if you are aware that you can "park" the control off-window, or "park" them within the window bounds, overlapped, when not visible, making them fairly easy to deal with in the designer. Then you can move them into position and make them visible as needed.


I didn't. I'll look to parking off-window.
Was This Post Helpful? 0
  • +
  • -

#11 lar3ry   User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: Dynamically Created Button NOT Firing

Posted 04 April 2013 - 08:34 PM

Quote

I didn't. I'll look to parking off-window.

If you do that, one of the minor annoyances you'll run into will be that the control won't be in the window in the designer, either. When I do something like this, I'll usually make the window larger in width or height, place the controls within the window, then on Form.Load, I'll shrink the window to the right size, hiding the controls only at run-time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1