Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
I have a simple piece of code that adds an handler to a htmlbutton object and i fires as you would expect.
 
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
 
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 :
 
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 13-Jan-13 23:22pm
Edited 13-Jan-13 23:24pm
v2
Comments
Sergey Alexandrovich Kryukov at 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

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,696
1 Sergey Alexandrovich Kryukov 6,675
2 CPallini 5,315
3 George Jonsson 3,584
4 Gihan Liyanage 2,650


Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 14 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100