how to let an embedded .NET winform usercontrol fire an event (via javascript) in webpage

J

jyanmin.fang

Hi,

In my current project, I need to embed an .NET winform usercontrol in
the aspx page (via <Object> tag). This winform usercontrol has an
event called DoEvent (void DoEvent()). This winform usercontrol will
fire this event upon certain action by the user. I tried to let the
aspx page subscribe to this event via

<script for="winObj1" event="DoEvent()" language="javascript">
window.alert("window control event happen!");
</script>

but the event was never fired. Any idea?

here are some code snippet, for aspx page
<script for="winObj1" event="DoEvent()" language="javascript">
window.alert("window control event happen!");
</script>
....
<body>
<Object id="winObj1" classid="WinObj.dll#WinObj.UserControl1"
VIEWASTEXT>
</Object
<body>

for winform usercontrol

delegate void DoEventHandler();
public event DoEventHandler DoEvent;

public void onDoEvent()
{
if(DoEvent != null)
DoEvent();
}
 
B

bruce barker

your winform has to implement an idispatch interface and be set to full
trust (user sets website to full trust or uses caspol.exe) to raise
events in the browser:

define idispatch click event:

[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
public interface IMyEvents {
[DispId(1)]//Each event must have a unique DispId
void DoEvent();
}

then your winform control must implement it:

[ComSourceInterfaces(typeof(IMyEvents))]
public class MyControl : Control
{
public event DoEventHandler DoEvent;

.....


-- bruce (sqlwork.com)
 
J

jyanmin.fang

Thanks, Bruce. This works nicely. The event is fired now.

One more question, if I don't set the Attribute to the class, then in
the javascript I can access the winform's Property. here is the code
(ClientID is the public property defined in th winform control)

<Object id="winObj1" classid="WinObj.dll#WinObj.UserControl1"
VIEWASTEXT>
<param name="ClientID" value="<%= Label1.ClientID%>" />
</Object>

<script language="javascript">
function doJS()
{
var o = document.GetElementByID("winObj1");
alert(o.ClientID);
}

But if I set the Attribute to the winform class, then o.ClientID is
undefined. I assume this problem can be fixed by setting some
Attribute to the property.

Thanks

Jimmy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top