Click here to Skip to main content
15,868,141 members
Articles / Web Development / IIS

A .NET 2.0 Server Control for ActiveX Activation Fix

Rate me:
Please Sign up or sign in to vote.
3.78/5 (7 votes)
23 Aug 2006CPOL3 min read 46.3K   468   22   5
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:

HTML
<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:

HTML
<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.

C#
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:

JavaScript
<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:

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

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

C#
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.

JavaScript
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)


Written By
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

 
QuestionJavascript Applet Pin
dasharp11-Sep-06 10:28
dasharp11-Sep-06 10:28 
AnswerRe: Javascript Applet Pin
Seth-B12-Sep-06 4:53
Seth-B12-Sep-06 4:53 

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

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