Monday, May 21, 2007

.NET ActiveX Example. Javascript event wireup. SSL

Okay if you read my last post and are wanting some answers (visual ones that is) then here ya go! :)

Skipping along to step#2
Here's an example of an interface used for events that you wanna hook up with in JS

    [Guid("00000000-0000-0000-0000-000000000000")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[ComVisible(true)]
public interface IExposedEvents{
[DispId(0x60050000)]
void DoSomething(string someString);
[DispId(0x60050001)]
void DoItAgain(int someInt);
}

Obviously you're gonna wanna put a valid GUID in there. So basically the second attribute, after the GUID, just says that this is a dispatch interface that will be exposed to COM. The second line is just re-iterating that this will be visible to COM. The [DispID]'s are used to uniquely identify the events to COM. These id's can be simple ints '[DispID(0)]' if you'd like, just make sure they're unique. You don't wanna have 2 events with the same dispId.

Next is the interfaces used to expose properties/methods...

    [Guid("00000000-0000-0000-0000-000000000000")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[ComVisible(true)]
public interface IExposedMethods{
void SetSomeVariable(string newVar);
string GetSomeVariable();
}

Again you want a unique GUID. This interface is nearly identical to the Events interface. The only difference is that you don't need the dispId's for the properties/methods that will be exposed.

Now here is the UserControl class

    [Guid("00000000-0000-0000-0000-000000000000")]
[ProgId("WindowsFormsUserControl.UserControl")]
[ClassInterface(ClassInterfaceType.None), ComSourceInterfaces(typeof(IExposedEvents))]
[ComVisible(true)]
public partial class MyControl: UserControl, IExposedMethods { //your code }

Okay so this one is a little different now. You still need the GUID (Well you don't HAVE to, read my last post. But if you wanna expose events in JS and use SSL then you need it). The ProgID is used to simply return the programmatic identifier for the specified COMAddin object. The next line starts off by saying what kind of interface is going to be generated for the exposed COM object. Here we are saying we don't want to generate one 'ClassInterfaceType.None'. The other 2 options are AutoDispatch and AutoDual which will auto generate an interface to expose for you (If you don't have any events to expose you could just write the user control with no interfaces and select one of these 2 options). The last part of this line is saying we want to use a specific type to expose this attributed class's events to COM.

So once you've got your control ready to go (created strong name, regasm'd it, etc) then you're ready to write the Javascript code in your .asmx file.

<object id="Bob" classid="clsid:00000000-0000-0000-0000-000000000000"></object>

This will place the custom control onto your form (if it's within the right tags, ie <div> <body> etc.). Next all you need to do is hook the event

<script for="Bob" event="DoSomething(someString)">
//Some code here... whatever you want to do :)
</script>

Like I said in my previous post you could also do it this way

<script>
function Bob::DoSomething(someString) {
//some code
}
</script>
I've had more success with the first approach, but it's silent so if you have a problem you wont know until you fire your event and it simply doesn't work.
Now you should be able to write some Custom Controls that you can hook up to JS events AND work with over SSL (Just make sure you register the assembly on the machine you want to see the controls on)
And again this is for SSL/event controls. If you just want to make a control for a public site over non-ssl without registering the controls on the clients then you don't have to take this exact approach. There is another way! Hopefully I can get some time and I'll do another post or two on this topic..

1 comment:

Ray said...

Hi Justin. Thanks for the post. I was looking for a way to handle the activeX events using pure javascript instead of script html blocks.

I'm just curious what you had experienced negatively with that approach?