Addhandler没有在动态按钮上触发 [英] Addhandler not firing on dynamic button

查看:65
本文介绍了Addhandler没有在动态按钮上触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段简单的代码,它为html按钮对象添加了一个处理程序,我会按照你的预期进行激活。



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()





我错过了什么,因为使用相同的种子对象添加另一个按钮根本不会破坏第一个按钮。 />


我想尽可能经常使用使用结束使用对,但是在t中他的情况似乎并没有像我预期的那样发挥作用。



任何建议我做错了都会很好,因为我已经花了一整个上午搞这个部分



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

推荐答案

在最后一行中,您创建一个新控件并立即将其处理掉。不要这样做。看起来你仍然无法理解一些非常基本的东西。



我怀疑事件实际上是被调用的,但是你看不到它。至少,这是最常见的错误。在调试器下执行它,在 AddHandler 上添加一个brea kpoint,另一个在 buttonEventHandler 的第一个语句中;你会看到所谓的,什么不是最快的。你没有显示这种方法,所以我不知道它是做什么的。



-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

事实证明,你不能使用一次性来创建一个eventHandler!

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





以上操作无效,因为新按钮处于结束使用状态..还处理汉dler。这就是为什么它不会在活动中开火。



你将不得不使用一个明确的按钮,如:





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)





这意味着您必须在为其分配AddHandlers时唯一地命名所有按钮。



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


这篇关于Addhandler没有在动态按钮上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆