Click here to Skip to main content
12,294,775 members (60,527 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB 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 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
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  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

As it turns out, you cannot use a disposable to create an eventHandler!
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 :

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160525.2 | Last Updated 22 Dec 2015
Copyright © CodeProject, 1999-2016
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