Click here to Skip to main content
14,216,761 members
Click here to Skip to main content
Article
Posted 26 Dec 2007

Stats

35.4K views
621 downloads
36 bookmarked

Custom Control to Embed Flash

,
Rate this:
4.00 (4 votes)
Please Sign up or sign in to vote.
4.00 (4 votes)
5 Jan 2008     CPOL    
Insert Flash into a page without "Click to activate and use this control" in IE.

Introduction

In some recent versions of Internet Explorer, Flash movies need a click to activate them. It is frustrating, especially if your Flash is part of the page design. But, some solutions are available to fix this issue. In other words, we need some code for IE and a different one for Opera, Firefox, Safari, and other favorite browsers. An ASP.NET custom control can make this possible.

Using the code

Just add this control to your toolbar, and that's all. Drag and drop on the page, select an SWF file to embed.

Points of interest

Let's consider details. First of all, we need to know the browser. No problems: Page.Request.Browser.Browser will return the browser name. So, now, we know what browser requested our page. The next step is to render the correct HTML for the browser. The main idea is to write a Flash <object> node from an external JavaScript file inside some container (div in this implementation). But, it's not very good to carry a JavaScript file with our custom control. So, we are going to embed the JavaScript inside our DLL. Here is our JavaScript:

function ActivateFlash(id,content)
{
    document.getElementById(id).innerHTML = content;
}

Fairly simple stuff... But we can't write it on the same page, it's important to put it in an external JavaScript resource. So, we have the file: activate.js.

To embed it inside our assembly, we need to change the build action to "Embedded Resource" and add this line to AssemblyInfo.cs:

[assembly: System.Web.UI.WebResource("EmbedFlash.activate.js",
 "application/x-javascript")]

Also, we need to provide the following properties: Width, Height, WMode (Windowed, Opaque, Transparent), and the Flash file URL (SWFURL). But this is simple stuff.

Another interesting thing, we might want to browse for the SWF file in design mode. To do this, we need to add the following attribute to our SWFURL property:

[EditorAttribute(typeof(System.Web.UI.Design.UrlEditor), typeof(UITypeEditor))]

It tells the designer to use the UrlEditor class as UITypeEditor for this property.

More information can be found inside the source code.

History

This article was originally posted on ASP.NET Cafe: Tips and Tricks, and then with a few changes posted on CodeProject. Flash Embedding was tested in IE 6 and 7, FireFox, Opera, and Safari.

  • January 5th... I found a big ugly bug here, and I corrected it.

License

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

Share

About the Author

Dmitry Salko
Web Developer
Ukraine Ukraine
No Biography provided

Comments and Discussions

 
GeneralMy Vote of 4 [modified] Pin
Oakman22-Nov-09 5:05
memberOakman22-Nov-09 5:05 
The article is good but hardly finished. By leaving out the loop parameter, you cut way down on the functionality and by forgetting to include the WMode in your JavaScript emitter (even though it is a property) you earned a 4.
Here's my rewrite of your code (obviously you also have to create a loop property for the control.)
string obj = "";
obj += "<OBJECT classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"";

obj += " codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\"";

obj += " ID=\""+this.ClientID+"\" WIDTH=\"" + this.Width.ToString() + "\" HEIGHT=\"" +

       this.Height.ToString() + "\" ALIGN=\"\">";
obj += "<PARAM NAME=movie VALUE=\"" + client_sfwurl + "\">";
obj += "<PARAM NAME=menu VALUE=" + ShowMenu.ToString().ToLower() + ">";
obj += "<PARAM NAME=LOOP VALUE=" + this.Loop.ToString() +">";
obj += "<PARAM NAME=WMode VALUE=" + this.WMode   + ">";
obj += "<PARAM NAME=quality VALUE=high><PARAM NAME=bgcolor VALUE=" + ColorTranslator.ToHtml(this.BackColor) + ">";
obj += "<EMBED src=\""+client_sfwurl+"\" menu=" + ShowMenu.ToString().ToLower() + 
       " quality=high bgcolor=\"" + ColorTranslator.ToHtml(this.BackColor) + "\" WIDTH=\"" +

       Width.ToString() + "\" HEIGHT=\"" + Height.ToString() + "\" swLiveConnect=true ID=\"" +

       this.ClientID+"\" NAME=\""+this.ClientID + 

       "\" ALIGN=\"\" TYPE=\"application/x-shockwave-flash\" PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\">
</EMBED>";
obj += "</OBJECT>";

Nonetheless, good job. They're selling dll's like this for a couple of hundred bucks.

Jon

Rob Graham wrote:
100% subsidies are very popular among the 50% of the population that pays no income tax...


Soap Box 1.0: the first, the original, reborn troll-less

modified on Monday, November 23, 2009 9:41 AM

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.