views:

397

answers:

2

I've created a windows form control which works successfully hosted in Internet Explorer. I'd like to give it an event and be able to respond to the event through javascript. I found a link that talks about it here. It shows me how to create the interfaces but I'm not sure how to fire the event from my control?

Here's my code snippetS:

//Control Code:
public class CardReader : Panel,ICardReaderEvents, ICardReaderProperties
{
   public void Error()
   {
   }
   public void Success()
   {
   }
}

//Interface for events
[Guid("DD0C202B-12B4-4457-9FC6-05F88A6E8BC5")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ICardReaderEvents
{
    [DispId(0x60020000)]
    void Error();

    [DispId(0x60020001)]
    void Success();
}

//Interface for public properties/methods
public interface ICardReaderProperties
{
     ...
}

//JavaScript to handle events
<SCRIPT FOR="CardReader1" EVENT="Error">
    window.status = "Error...";
</SCRIPT>

<SCRIPT FOR="CardReader1" EVENT="Success">
    window.alert("Success");
    window.status = "";
</SCRIPT>
+1  A: 

You are implementing it wrong in your CardReader class:

public event Error;
public event Success;

protected void OnError()
{
    if(Error != null)
        Error();
}

protected void OnSuccess()
{
    if(Success != null)
        Success();
}

If your ICardReaderEvents interface changes to have Error and Success take parameters, then just call them in OnError and OnSuccess.

Brian Genisio
Thanks. So when does the event handler get set? I've checked in the Load event of the user control and the eventhandler is still null, so either I haven't gotten something hooked up right, or it just hasn't been set yet.
Jeremy
See my next answer
Brian Genisio
A: 

So, now you need to know how to hook it in the Javascript? Here is how I know how to do it:

<object id="CR" ...></object>

<script type="text/javascript">
  function CR::Error()
  {
    alert("Error!");
  }

  function CR::Success()
  {
    alert("Success");
  }
</script>
Brian Genisio
Thanks for all the help. I'm getting an object undefined CR.
Jeremy
Is your Object ID set to CR? Is the object loading appropriately? I had a typo... it should read CR::Success(), not CR:Success(). Editing it now.
Brian Genisio