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

Simple ASP.NET SWF container control with Flash variables

, 1 Sep 2008
Rate this:
Please Sign up or sign in to vote.
Easy to use ASP.NET SWF container control with Flash variables.

Introduction

Most SWF containers I've stumbled upon were complex, would cost me a lot, or simply didn't have the functionality I required. This small Web Control is a container for any SWF control, and also dynamically receives Flash variables.

Using the code

To use this code, simply add the class EmbeddedObject to your web app, don't forget to change the name of the namespace to suit your requirements. Once added, compile the application so that the toolbox items will refresh, and drag the embedded SWF object to the web page of your selection.

To dynamically add Flash variables at runtime, use the AddVariable method.

The new Flash variables will be rendered into the control and sent to the client.

EmbeddedObject1.AddVariable("FLV_PATH", txtFlvPath.Text)

The simplicity of the code is reflected in the rendering method, which creates a simple Flash object with the embedded Flash variables. If you look at the source code of any web page that contains Flash commercials, you will probably see something similar.

protected override void RenderContents(HtmlTextWriter writer)
{
     try
     {
         StringBuilder sb = new StringBuilder();
         string flashVariableString = "";

         foreach (FlashVariable fv in FlashVariables)
             flashVariableString += fv.Name + "=" + fv.Value + "&";
         if (FlashVariables.Count > 0)
            flashVariableString = flashVariableString.Substring(0, 
                                  flashVariableString.Length - 1);

         sb.Append(&quot;<object classid=\&quot;clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\&quot; &quot;);
         sb.Append(&quot;codebase=\&quot;http://download.macromedia.com/pub/&quot; + 
                   &quot;shockwave/cabs/flash/swflash.cab#version=5,0,2,0\&quot; ID=Moshe Width=&quot; + 
                   Width.Value.ToString() + &quot; Height=&quot; + Height.Value.ToString() + &quot; > &quot;);
         sb.Append(&quot;<param name=movie value=\&quot;&quot; + FilePath.ToString() + &quot;\&quot;> &quot;);
         sb.Append(&quot;<param name=FlashVars value=\&quot;&quot; + flashVariableString + &quot;\&quot;>&quot;);
         sb.Append(&quot;<param name=quality value=high> &quot;);
         sb.Append(&quot;<param name=BGCOLOR value=#FFFFFF>&quot;);
         sb.Append(&quot;<embed src=\&quot;&quot; + FilePath.ToString() + &quot;\&quot; &quot;);
         sb.Append(&quot;pluginspage=\&quot;http://www.macromedia.com/shockwave/download/&quot; + 
                   &quot;index.cgi?P1_Prod_Version=ShockwaveFlash\&quot; type=\&quot;application&quot; + 
                   &quot;/x-shockwave-flash\&quot; &quot;);
         sb.Append(&quot;Width = &quot; + Width.Value.ToString() + &quot; &quot;);
         sb.Append(&quot;Height = &quot; + Height.Value.ToString() + &quot; &quot;);
         sb.Append(&quot;bgcolor=#FFFFFF &quot;);
         sb.Append(&quot;FlashVars=\&quot;&quot;+ flashVariableString + &quot;\&quot; &quot;);
         sb.Append(&quot;TYPE=\&quot;application/x-shockwave-flash\&quot; &quot;);
         sb.Append(&quot;><embed></object>&quot;);
         writer.Write(sb.ToString());
     }
     catch(Exception ex)
     {
         writer.RenderBeginTag(HtmlTextWriterTag.Div);
         writer.Write(&quot;Custom Flash Control&quot;);
         writer.RenderEndTag();
     }
}

Points of Interest

This little piggy saved me a lot of trouble when trying to use other purchased SWF wrapper controls. But sometimes, it's best searching for extended functionality.

If you are a web designer that works a lot with Flash, you will find this very helpful since you can communicate between your C# code in the web server and your Flash controls easily.

License

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

About the Author

maorray
Team Leader DBSophic
Israel Israel
I started my IT life in the military where I served as an oracle DBA and a computer programmer for several years.
After that I spent 3 years I've been a consultant for Java and oracle technologies.
Now adays I'm running a software development team in a .net winforms environment.
I also do some freelancing here and there.

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberapurav27-Jul-10 4:41 
RantProblems PinmemberRichard Deeming12-Sep-08 8:26 
QuestionFlash embedding.... Pinmembermkalyan198010-Sep-08 3:06 
GeneralInteresting work PinmemberGriffinPeter1-Sep-08 22:02 
GeneralRe: Interesting work Pinmembermaorray1-Sep-08 23:03 
Generalou, I'm gonna check this one out :D PinmemberSeishin#1-Sep-08 9:48 

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
Web02 | 2.8.140721.1 | Last Updated 1 Sep 2008
Article Copyright 2008 by maorray
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid