Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Buttons not firing events

Status
Not open for further replies.

MikeDamone

Programmer
Oct 21, 2003
106
US
I have a series of dynamic controls that are created by the same sub. There are textboxes, buttons and dropdowns all being created together.

The click event of the buttons are not firing even though they an AddHandler has been added. I know the dynamic controls are being rendered correctly after a postback because any value I add to the textboxes is still present.

Also, the exact same code worked fine in VS 2003 but when the project was upgraded to VS 2005, the button click events are not firing. Can anyone help? Thanks! Here is a portion of the code:

Protected Overrides Sub MakeControls

txt = New TextBox
With txt
.Text = Me._SelectedValue
.Visible = True
End With
Controls.Add(txt)

btn = New Button
With btn
.Text = "Find"
.Visible = True
End With
AddHandler btn.Click, AddressOf MyClick
Controls.Add(btn)


ddl = New DropDownList
ddl.Visible = False
Controls.Add(ddl)
MyBase.MakeControls()

End Sub


Public Sub MyClick(ByVal sender As Object, ByVal e As EventArgs)

'Some code that is never being fired


End Sub

 
Can you try explicitly setting each controls ID.

i.e.

Code:
    btn = New Button
        With btn
            .Text = "Find"
            .Visible = True
            .Id = "btnFind"
        End With
        AddHandler btn.Click, AddressOf MyClick
        Controls.Add(btn)
 
also, make sure to add the dynamic controls durning the Page.Init event so the controls fall into the page life cycle correctly.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thanks to you both for responding. Setting the id did not work. I have seen some articles that say to create the controls in the page init, but the controls are built based on user input so I don't see how I can add their creation to the page init. Is that the only way this is going to work?
 
In order for the controls to behave and render correctly, they need to be created in the Init event. Otherwise, you will run into errors at some point due to the ViewState.
 
another reason why webforms causing more harm than good :) adding events to an event-less environment.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
The thing that drives me nuts is this exact code worked fine in VS 2003 but does not work in VS 2005.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top