frustrating trouble with events and placeholder
Hi
I picked an example from the internet to show my problem. I add a linkbutton to a placeholder and add a handler 'bt.Click' for an event 'HandleEvent'. The event itself should now be in effect:
Private Sub HandleEvent(ByVal sender As Object, ByVal e As System.EventArgs) handles bt.Click
I understand that from this point I can leave out the 'handles bt.Click'. This is exactly what I want, because in real life I do not know in front the name of the LinkButton. But the event does not fire without a 'handles whatevercontrol.click'. There are many many examples of events on internet without a 'handles' clause, but why does this never work with me?
When I add 'handles MyBase.init', within the event i can get the name of the page (sender.ID = the name of the page), but not of the control (the linkbutton or dropdownlist).
I actually do not add linkbuttons in my placeholder, but some dropdownlists. So my event should handle dropdownlist.SelectedIndexChange. But I have the same trouble.
Any ideas are highly appreciated!!
Sub SimpleSub
Dim bt As New LinkButton
With bt
.Text = "MyButton"
.ID = "MyButtonID"
.CommandName = "TheCommand"
End With
AddHandler bt.Click, AddressOf HandleEvent
placeHolder.Controls.Add(bt)
End Sub
Private Sub HandleEvent(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Write(sender.CommandName)
End Sub
|