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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Event handling with a user control that dynamically creates objects

Status
Not open for further replies.

JonClough

Programmer
Apr 16, 2003
7
GB
Hi,

I cannot seem to dynamically create a button that responds to a button click. It's a user control that is called from a aspx page and creates dropdown and a button. I have a label to test that either says clicked or unclicked but when I run the code it is always unclicked!

Any help appreciated!

here's the code:

Public Class FavouriteManagerDropDownControl : Inherits System.Web.UI.Control
Dim favourites_dropdown As new System.Web.UI.WebControls.dropdownlist()
Dim withevents favourites_button As new System.Web.UI.htmlControls.htmlinputbutton("button")
Dim test as new label()

Private Sub favourites_button_ServerClick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles favourites_button.ServerClick

test.text="ooh I've been clicked"
End Sub


Protected Overrides Sub createchildcontrols()
favourites_dropdown.id = "favourites_dropdown"
Controls.Add(favourites_dropdown)
favourites_button.ID="favourites_button"
favourites_button.Value="Go!"
Controls.Add(favourites_button)
test.text="unlclicked"
Controls.Add(test)
'AddHandler favourites_button.ServerClick, AddressOf favourites_button_ServerClick
End Sub

end class
 
Your AddHandler statement is commented out there... The code won't fire unless you wire it up explicitly to the button. You might also do it before adding it to the controls collection.

There's no need for the withevents access modifier there, either. It's for pages & user controls, and what you have here is a server control.

Also, why did you choose an HtmlInputButton instead of a regular Button control?

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Link9,

Thanks for the reply. I've modified the code based on your suggestions but still no joy! The label says unclicked even when I click the button. Perhaps the createchildcontrols overrides the buttonclick function. Anyway, any help would be appreciated, the modified code is below:

Public Class FavouriteManagerDropDownControl : Inherits System.Web.UI.Control
Dim favourites_dropdown As new System.Web.UI.WebControls.dropdownlist()
Dim favourites_button As new button()
Dim test as new label()

Private sub favourites_button_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'When the button is clicked
test.text="Ooh, I've been clicked"
End Sub


Protected Overrides Sub createchildcontrols()
favourites_dropdown.id = "favourites_dropdown"
Controls.Add(favourites_dropdown)
favourites_button.ID="favourites_button"
favourites_button.text="Go!"
AddHandler favourites_button.Click, AddressOf me.favourites_button_Click
Controls.Add(favourites_button)
test.text="Unclicked"
Controls.Add(test)
End Sub
 
I'd put a breakpoint on the event handler to see if it's being called at all...

Let us know -

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
I'm not using visual studio - can I still put in a breakpoint?

I have tried putting the line:

HttpContext.Current.response.redirect "/hadrian/filemanager.aspx?docpath=d:\")

Into the click handler subroutine and it didn't fire so I'm pretty sure it doesn't get called.

Cheers

Jonathan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top