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

  • Thread starter Thread starter jyanmin.fang
  • Start date Start date
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();
}
 
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)
 
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

Back
Top