如何将事件从VB.Net ClassLibrary / UserControl(ActiveX)提升为JavaScript?

时间:2021-07-13 00:02:15

I've created a VB.Net ClassLibrary with a UserControl in it. I can load it from an HTML page and call the methods that I created. This works as expected. I've tried several examples for how to raise an event from the VB code to the js caller, and none of them seem to work (I'm using IE7).

我已经创建了一个带有UserControl的VB.Net ClassLibrary。我可以从HTML页面加载它并调用我创建的方法。这按预期工作。我已经尝试了几个例子来说明如何将事件从VB代码提升到js调用者,并且它们似乎都没有工作(我正在使用IE7)。

What I'm doing:

我在做什么:

Public Class ctrlABC
   Public Event TestEvent()

Then when I want to raise this event, I call:

然后,当我想举起这个活动时,我打电话给:

RaiseEvent TestEvent()

In my JS file, I've tried the following:

在我的JS文件中,我尝试了以下内容:

<OBJECT id="myControl1" name="myControl1" classid="ABC.dll#ABC.ctrlABC" width=400 height=400></OBJECT>

<SCRIPT LANGUAGE=javascript FOR=myControl1 EVENT=TestEvent> 
    myControl1_TestEvent()  
</SCRIPT>

<script>
function myControl1_TestEvent(){
    alert("raised");
}
</script>

================== and then I tried ===================

==================然后我尝试了===================

<OBJECT id="myControl1" name="myControl1" classid="ABC.dll#ABC.ctrlABC" width=400 height=400></OBJECT>

<script>
function myControl1::TestEvent(){
    alert("raised");
}
</script>

===========================

but neither seems to get the event. Does anyone have any ideas?

但似乎都没有得到这个事件。有没有人有任何想法?

Thanks!

1 个解决方案

#1


I've not used <object>'s like that so I cant be sure, but I would expect you declare the event handler like regular events in Javascript.

我没有使用这样的,所以我无法确定,但我希望你在Javascript中声明事件处理程序像常规事件。

In the elements attributes:

在元素属性中:

<object id="myControl1" .... onTestEvent="functionName"></object>

or as a function in the object: (might not be the correct term)

或作为对象中的函数:(可能不是正确的术语)

document.getElementById("myControl1").onTestEvent = functionName

document.getElementById(“myControl1”)。onTestEvent = functionName

or using the event registration function(s): document.getElementById("myControl1").addEventListener("TestEvent", functionName, false);

或者使用事件注册函数:document.getElementById(“myControl1”)。addEventListener(“TestEvent”,functionName,false);

(these are browser specific, this one is for firefox, not sure about others)

(这些是浏览器特定的,这个是针对Firefox的,不确定其他人)

with each of these "functionName" is a function you declare in the <head>.

每个“functionName”都是您在中声明的函数。

Might want to have a look at Quirksmode - Advanced event registration

可能想看看Quirksmode - 高级活动注册

#1


I've not used <object>'s like that so I cant be sure, but I would expect you declare the event handler like regular events in Javascript.

我没有使用这样的,所以我无法确定,但我希望你在Javascript中声明事件处理程序像常规事件。

In the elements attributes:

在元素属性中:

<object id="myControl1" .... onTestEvent="functionName"></object>

or as a function in the object: (might not be the correct term)

或作为对象中的函数:(可能不是正确的术语)

document.getElementById("myControl1").onTestEvent = functionName

document.getElementById(“myControl1”)。onTestEvent = functionName

or using the event registration function(s): document.getElementById("myControl1").addEventListener("TestEvent", functionName, false);

或者使用事件注册函数:document.getElementById(“myControl1”)。addEventListener(“TestEvent”,functionName,false);

(these are browser specific, this one is for firefox, not sure about others)

(这些是浏览器特定的,这个是针对Firefox的,不确定其他人)

with each of these "functionName" is a function you declare in the <head>.

每个“functionName”都是您在中声明的函数。

Might want to have a look at Quirksmode - Advanced event registration

可能想看看Quirksmode - 高级活动注册