index > ClickOnce and Setup & Deployment Projects > FYI - "Fixing" javascript events with .NET Framework 1....

FYI - "Fixing" javascript events with .NET Framework 1....

For those of you who are using or were using javascript events prior to SP1 with the .NET Framework and are having issues - there exists a fairly straight forward workaround.

Steps to take:
1. Do not use standard Javascript event syntax or object.attachEvent(...) - Both of these raise SecurityDemands for the UnmanagedCode permission - which will fail in IE unless you are trusting by zone (which we are not).

2. Convert your existing script tag or function declaration in javascript to a standard method
From this:
<script for="myControl" event="MyEvent">
alert("Did MyEvent on myControl!");
</script>
To This:
<script>
function myControl_OnMyEvent() {
  alert("Did MyEvent on myControl!");
}
</script>

3. Add a method like the following to your control
public void AddEventListener(mshtml.DispHTMLObjectElement target, string eventName, object callback)
{
  new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert();
  target.attachEvent(eventName, callback);
  SecurityPermission.RevertAssert();
}

4. In javascript call your new method (we respond to the control event Load to make sure things are really there)
myControl.AddEventListener(myControl, "MyEvent", myControl_OnMyEvent);

That's it... I'm typing this quick - if it is unclear - tell me where and why and I'll explain if I can.  If I'm a tool and could have done this some automagic way - let me know that as well...

ze
WinFormsUser
Note: The mshtml.DispHTMLObjectElement comes from a reference to the Primary Interop mshtml.dll from the Framework - don't forget to add this reference!
WinFormsUser
Hi,

How do i raise the event that i hooked it up in the server control.
I coded as you mentioned.

Thanks in advance
WinFormsUser
It sounds like you are talking about an ASP.NET server control.

A few more details would be helpful (sample code, etc).

ze
WinFormsUser

Sorry for the confusion.
I want to call a javascript function from a User control hosted inside IE.
I don't know how to call the javascript function from UserControl???


Thanks.
WinFormsUser
Updated method for attaching events (loose the Microsoft.mshtml.dll reference):

Change the signature of the AddEventListener on your Windows control to accept an object instead of the strongly typed "mshtml.DispHtmlObjectElement".

public void AddEventListener(object target, string eventName, object callback) 


   // assert permission
   new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert(); 
   
   // obtain IHTMLObjectElement type by GUID
   Type type = Type.GetTypeFromCLSID(
           new Guid("{3050F24F-98B5-11CF-BB82-00AA00BDCE0B}"));

   // invoke attachEvent
   type.InvokeMember(
                              "attachEvent", 
                              BindingFlags.InvokeMethod, 
                              null, 
                              target, 
                              new object[] {eventName, callback}
                              ); 
   
   // revert permission assertion
   SecurityPermission.RevertAssert();

}
WinFormsUser
Quick question - do you need to keep the old COMEvents interface with DispIds which used to be required to bind to the 'for' code on the page?
WinFormsUser
To be honest I'm not sure, and I don't have an easy "near by" example to suss through the details of the thing...  If I remember correctly though I ran into other issues without the supporting I[ClassName]COMEvents interface... Such as the events don't get to IE.

So without being any help at all I would say - pull them off - if it doesn't work - put them back.  Hooray for science and only understanding my perspective of the world.  

Sorry I couldn't put together a definitive answer and example...
WinFormsUser
thanks for the swift reply - I will experiment but my feeling is that it probably needs to stay in order for the events to propogate to IE as you say.
WinFormsUser
You will still require the security permissions to run unmanaged code. The difference is that you will be able to give "FullTrust" access right just for the control, instead of the whole zone.
WinFormsUser
True, true.  

You must have the unmanaged code privelege to interoperate with JS AND SP1.  It is odd but the way that the permission applied seems to have changed directions (or added another dimension) with SP1.
WinFormsUser
I've been running into the same wall.

I have a .net form control that's able to post events to javascript perfectly fine on IE 6.0.2800 on a Win2000 machine, but the control never gets the registration on my XpPro machine running IE 6.0.2900. The event handler is always NULL, no matter how I try to register the event with javascript/JScript/whatever. In my situation, I cannot force the user to change their security settings in order to use my controls. My assemblies are strongly signed and have my digital certificate signed to them. Extremely annoying.

To me, this is a serious lack of functionality. I could see some minor annoyances a .Net control in an IE browser could do to the browser, but the need to be able to post events back to the browser greatly outweigh any of these issues, IMO.

Not to mention that .Net controls won't run in other browsers...

I would greatly perfer writting .Net forms controls instead of using Flash or Java Applets, but being able to interact with the browser is a requirement for me.
WinFormsUser
reply 12

You can use google to search for other answers

 

More Articles

• .net framework 2.0
• How to translate Setup Dialogs in Setup project
• Setup and Deployment Projects
• SQLExpress can't attach Database using ClickOnce
• Click Once Publish Version #
• It works in localhost(local computer) but it does not work on int...
• Deciding which tool to use for automation of build and deployment
• Rant: .Net Code Access Security Sucks!
• What do I need to do to deploy controls that target Windows XP Th...
• Testing Deployment for the first time
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• problem deploying obfuscated app using C
• Security exceptions running a .NET 2005
• ClickOnce application StartupPath
• Deployment of Projects using Managed Dir
• Clickonce form display issues -- Help!!!
• How to add a path variable using Orca
• Server & Bandwidth Requirements
• ClickOnce Failing on one machine after w
• 100% Managed Controls
• FileIO Exception Only On First Load of C
• Question Regarding ClickOnce Security Se
• How to deploy an application
• No touch deployment - offline question.
• File Download - Security Warning
• configuration IIS

Hot Articles

• The referenced assembly is not installed
• add movie program in the installer process
• AppUpdater on WinXP client?
• -> Basic Setup Question
• How to get Authenticode to work with Cli
• how to access address bar from Explorer
• How to deploy a dotnet win application W
• My.Settings - scope problem.
• StrongNameIdentityPermission
• Install and uninstall framework,
• install client msi and application proxy
• How to run a batch file at end of install?
• How can i set JIT Debugging in VB Expres
• re-installing a setup and deployment pro
• How to best update program

Recommend Articles

• CGI Error
• Web deployment and Remoting
• HELP! Problem with Installation .NET Fra
• ClickOnce Deployment for Windows Forms A
• configuration for whidbey devMachine an
• Setup Project: How do I make a dialog co
• Deploying the framework with the applica
• Control in IE interferes with Form Post
• Can you use a ClickOnce Application in C
• Problems while publishing project
• how can i make the embedded control resi
• Oracle ODP.Net Problem with Once Click
• Newbie need help
• Another publishing problem
• Publish version v's assembly version