Click here to Skip to main content
Click here to Skip to main content

A .NET 2.0 Server Control for ActiveX Activation Fix

, 23 Aug 2006
Rate this:
Please Sign up or sign in to vote.
A .NET 2.0 server control to fix the ActiveX activation issue caused by Internet Explorer updates.

Introduction

As part of a series of recent Internet Explorer updates pushed out by Microsoft, ActiveX controls, including the Flash Player and QuickTime player, have started prompting the user to click the controls to activate them.

This behavior has suddenly created less attractive web pages, broken existing applications, and caused headaches for web developers. The workaround for this problem has been clearly documented, but is still a hassle to implement. This article describes a .NET 2.0 control designed not only to fix the problem, but to make the fix easy to implement.

Background

The way to workaround the issue of getting the activation prompt is simple.

  1. Create a reference in your HTML file to an external JavaScript file. The JavaScript file should contain a function that will document.write() the <object> tag and all of the associated HTML code that would ordinarily be put directly in the HTML file.
  2. Call the JavaScript function from your webpage.

The problem with the above approach is that in order to implement it, you have to write custom JavaScript each time, and writing JavaScript to "document.write()" can be a very error prone and time consuming process.

The Server Control

Instead of writing Javascript, another approach is to create a server control to simply wrap your existing HTML code on your website. Below is what your existing HTML code might look like:

<object id="flash1" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" 
  codebase="http://fpdownload.macromedia.com/pub/shockwave/
            cabs/flash/swflash.cab#version=8,0,0,0" 
  width="419" height="320" align="middle">
        <param name="allowScriptAccess" value="sameDomain" />
        <param name="movie" value="/myFlash.swf" />
        <param name="quality" value="high" />
        <param name="bgcolor" value="#ffffff" />
        <embed src="/myFlash.swf" quality="high" bgcolor="#ffffff" 
          width="419" height="320" name="map" align="middle" 
          allowScriptAccess="sameDomain" type="application/x-shockwave-flash" 
          pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>

Below is how your HTML will change with the .NET 2.0 server control:

<form runat="server">
<WH:JsWriter runat="server" >
    <object id="flash1" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" 
            codebase="http://fpdownload.macromedia.com/pub/
                      shockwave/cabs/flash/swflash.cab#version=8,0,0,0" 
            width="419" height="320" align="middle">
        <param name="allowScriptAccess" value="sameDomain" />
        <param name="movie" value="/myFlash.swf" />
        <param name="quality" value="high" />
        <param name="bgcolor" value="#ffffff" />
        <embed src="/myFlash.swf" quality="high" 
          bgcolor="#ffffff" width="419" height="320" 
          name="map" align="middle" allowScriptAccess="sameDomain" 
          type="application/x-shockwave-flash" 
          pluginspage="http://www.macromedia.com/go/getflashplayer" />
    </object>
</WH:JsWriter>
</form>

The .NET server control takes the literal content and puts it into a JavaScript variable. The Render method of the server control is written to output JavaScript code.

protected override void Render(HtmlTextWriter output)
{
    string[] htmlLines = (this.InnerHtml.Trim()).Split('\n');
    string outString = "";
    for (int i = 0; i < htmlLines.Length; i++)
    {
        string currentLine = htmlLines[i].Replace("'", 
                    "\\'").Replace("\r", "");
        if (i > 0)
            outString += "+";
        if (i == htmlLines.Length - 1)
            outString += "'" + currentLine + "';\n";
        else
            outString += "'" + currentLine + "'\n";
    }
    string varName = "objStr_" + this.UniqueID;
    output.Write("<script language="\""javascript\">");
    output.Write(Environment.NewLine);
    output.Write("var " + varName + " = " + outString);
    output.Write(Environment.NewLine);
    output.Write("InsertObjectToDocument(document," + varName + ");");
    output.Write(Environment.NewLine);
    output.Write("</script>");
}

The above code when run creates the output:

<script language="Javascript">
var objStr = ''
+'<object id=\"flash1\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" ' 
+ 'codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/' 
+ 'swflash.cab#version=8,0,0,0\" width=\"419\" height=\"320\" align=\"middle\">'
+'<param name=\"allowScriptAccess\" value=\"sameDomain\" />'
+'<param name=\"movie\" value=\"/myFlash.swf\" />'
+'<param name=\"quality\" value=\"high\" />'
+'<param name=\"bgcolor\" value=\"#ffffff\" />'
+'<embed src=\"/myFlash.swf\" quality=\"high\" ' 
+ 'bgcolor=\"#ffffff\" width=\"419\" height=\"320\" ' 
+ 'name=\"map\" align=\"middle\" allowScriptAccess=\"sameDomain\" ' 
+ 'type=\"application/x-shockwave-flash\" ' 
+ 'pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />'
+'</object>';  
InsertObjectToDocument(document,objStr);
</script>

This resulting JavaScript code will output the ActiveX control, without the annoying "Click to activate and use this control" message. The method InsertObjectToDocument is defined in a JavaScript file that is embedded into the DLL as explained below.

.NET 2.0 Benefits

This code could have worked in .NET 1.1, however we took advantage of a really cool .NET 2.0 feature that rendered this control non-backwards compatible. Using the web resource functionality, this control is able to self-contain its own JavaScript functions. This means that you can simply drop the control onto an ASPX page and not worry about copying over JavaScript files, like you had to in .NET 1.1.

This entry has to be put into AssemblyInfo.cs:

[assembly: WebResource("JavascriptWriters.JavascriptWriters.js", "text/Javascript")]

The following code is put in the class file for the server control, JsWriter.cs:

protected override void OnPreRender(EventArgs e)
{
   Page.ClientScript.RegisterClientScriptInclude("jsWritersScript", 
     Page.ClientScript.GetWebResourceUrl(this.GetType(), 
                       "JavascriptWriters.javascriptWriters.js"));
   base.OnPreRender(e);
}

Then inside JavaScriptWriters.js, the supporting JavaScript functions are put in.

Save the FORM Tag!

If you don't want to use your precious form tag just for this workaround, you can simply omit form runat="server" and instead just include the following JavaScript function in a JavaScript file that is already included on your webpage.

function InsertObjectToDocument(toDocument, insertObject)
{
    toDocument.write(insertObject); 
}

Design Time Support

One of the benefits of this control is that it helps preserve design time layout functionality for your object tag in tools like Dreamweaver and Visual Studio.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Seth-B
Chief Technology Officer
United States United States
Seth Berger is a Technology Director at WHITTMANHART Interactive. At WHITTMANHART he develops and manages large .NET projects for some of the world's most successful companies. He is skilled in a large number of languages and development environments but his primary focus is on ASP.NET development, and has worked on it since it was in beta. He previously co-founded Estco Medical, a web development and software company focused on the life-science industry.

Comments and Discussions

 
Generaladobe flash player Pinmemberzmrcic16-Apr-07 3:23 
QuestionJavascript Applet Pinmemberdasharp11-Sep-06 10:28 
AnswerRe: Javascript Applet PinmemberSeth-B12-Sep-06 4:53 
GeneralLooks familiar... PinmemberRichard Deeming24-Aug-06 8:11 
GeneralWas about to create similar control... PinmemberAshley van Gerven24-Aug-06 1:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 23 Aug 2006
Article Copyright 2006 by Seth-B
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid