Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I have a simple piece of code that adds an handler to a htmlbutton object and i fires as you would expect.

VB
Dim newbutton As HtmlButton
newbutton = New HtmlButton
newbutton.ID = "somebutton"
newbutton.InnerText = "Click"
AddHandler newbutton.ServerClick, AddressOf Me.buttonEventHandler
resultBox.Controls.Add(newbutton)


For some reason I wanted to clean things up and changed the code to

VB
Using newbutton As New HtmlButton
    newbutton.ID = "somebutton"
    newbutton.InnerText = "Click"
    AddHandler newbutton.ServerClick, AddressOf Me.buttonEventHandler
    resultBox.Controls.Add(newbutton)
End Using


This piece of code fails to fire the eventhandler. And when I dispose of the object after it has been placed in its parent object the eventhandler also fails :

VB
Dim newbutton As HtmlButton
newbutton = New HtmlButton
newbutton.ID = "somebutton"
newbutton.InnerText = "Click"
AddHandler newbutton.ServerClick, AddressOf Me.buttonEventHandler
resultBox.Controls.Add(newbutton)
newbutton.Dispose()


Am I missing something, because added another button using the same seed object doesnt destroy the first button at all.

I would like to using the "Using""End Using" pairs as often as possible, but in this case it doesnt seem to work as I expected.

Any suggestions what I am doing wrong would be very nice as I already spend a full morning figuring this part out
Posted
Updated 13-Jan-13 23:24pm
v2
Comments
Sergey Alexandrovich Kryukov 14-Jan-13 11:51am    
AddHandler is not supposed to "fire". It adds an event handler to the invocation list of an event instance.
It looks like you understand it (or almost), so why are you asking this way?
—SA

In your last line, you create a fresh control and immediately dispose it. Don't do it. It looks like you still fail to understand something really basic.

I suspect that the event is actually invoked, but you fail to see it. It least, this is the most usual mistake. Execute it under the debugger, put one brea kpoint on AddHandler, another on the very first statement of your buttonEventHandler; and you will see what is called and what not in no time. You don't show this method, so I don't know what it does.

—SA
 
Share this answer
 
As it turns out, you cannot use a disposable to create an eventHandler!
VB
Using newbutton As New HtmlButton
    newbutton.ID = "somebutton"
    newbutton.InnerText = "Click"
    AddHandler newbutton.ServerClick, AddressOf Me.buttonEventHandler
    resultBox.Controls.Add(newbutton)
End Using


The above won't work because the newbutton is disposed at "End Using".. Also disposing the handler. That is why it won't fire at the event.

You will have to use a explicit button like :

VB
Dim button_buttonName as New HTMLButton
    button_buttonName.ID = "buttonName"
    button_buttonName.InnerText = "Click"
    AddHandler button_buttonName.ServerClick, AddressOf Me.buttonEventHandler
    resultBox.Controls.Add(button_buttonName)


This mean you have to name all buttons uniquely when you assign AddHandlers to them.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900