Click here to Skip to main content
15,881,173 members
Articles / Web Development / ASP.NET
Article

Simple ASP.NET SWF container control with Flash variables

Rate me:
Please Sign up or sign in to vote.
3.38/5 (4 votes)
1 Sep 2008CPOL1 min read 49.8K   1.6K   19   6
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.

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

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


Written By
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 Pin
apurav27-Jul-10 4:41
apurav27-Jul-10 4:41 
RantProblems Pin
Richard Deeming12-Sep-08 8:26
mveRichard Deeming12-Sep-08 8:26 
QuestionFlash embedding.... Pin
mkalyan198010-Sep-08 3:06
mkalyan198010-Sep-08 3:06 
GeneralInteresting work Pin
CARPETBURNER1-Sep-08 22:02
CARPETBURNER1-Sep-08 22:02 
GeneralRe: Interesting work Pin
maorray1-Sep-08 23:03
maorray1-Sep-08 23:03 
Generalou, I'm gonna check this one out :D Pin
Seishin#1-Sep-08 9:48
Seishin#1-Sep-08 9:48 

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.