2

From another question I recently posted, it seems that Classic ASP might not be able to deal with events that an activeX object might throw.

Can anybody verify this or if it can, how do I do it?

Graham
  • 7,807
  • 20
  • 69
  • 114

2 Answers2

1

You are correct ASP cannot handle events. It does not have the either the CreateObject signature needed to wire up script functions to events nor does it support the <script FOR syntax that you might use client-side.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
1

Is it possible to handle events from an activeX object in Classic ASP

No, if you are speaking about COM events.
But, Depending on what you mean by the word "event" the answer is "Maybe."

See Create COM/ActiveXObject in C#, use from JScript, with simple event

It's possible to create a COM class / ActiveX thing, instantiate it in JScript/Classic ASP, and have the COM class invoke a callback that is defined in the ASP page. This is not done via COM events, but it may satisfy your requirement.

The ASP code might look like:

<script language="Javascript" runat="server">

var greet = Server.CreateObject("Ionic.Greet");

greet.onHello = function(arg, num) {
    Response.Write("onHello (ASP) invoked.<br/>\n");
    Response.Write("  num = " + num + "  stat=" + arg.status + "<br/>\n");
};

response = greet.Hello("ASP");
Response.Write("response: " + response + "<br/>\n");
Response.Write("status: " + greet.status + "<br/>\n");

</script>

This works only if the COM class being created exposes a property that can be set, and then invokes the "Default method" on that property. Like this:

public partial class Greet : IGreet
{
    public Object onHello { get; set; }

    public String Hello(string name)
    {
        var r = FireEvent();
        return "Why, Hello, " + name + "!!!" + r;
    }
}

...where FireEvent is like this:

private string FireEvent()
{
    if (onHello == null) return " (N/A)";
    onHello
        .GetType()
        .InvokeMember
        ("",
         BindingFlags.InvokeMethod,
         null,
         onHello,
         new object [] {});

    return "ok";
}

Obviously if you have a COM class that cannot be changed, and it uses COM events, then this approach will not apply.

Community
  • 1
  • 1
Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • Almost there - I have a related problem - my C# knowledge is next to zero. I have adapted the code in your other answer that you referred to and I suspect my lack of understanding of C# event handling is letting me down. I am calling FireEvent like so: WebClient request = new WebClient(); request.DownloadDataCompleted += new DownloadDataCompletedEventHandler(FireEvent); I have changed the method like so: public void FireEvent(Object sender, DownloadDataCompletedEventArgs e) It compiles fine, I just can't catch the event in ASP x.onDownloadComplete = function(){Response.Write("YEE HA");} – Graham Jun 25 '12 at 11:05
  • I think you may need to post another question. it looks like this new twist is going to require some additional explanation, hard to do in a comment dialogue. – Cheeso Jun 25 '12 at 15:19