Click here to Skip to main content
15,885,182 members
Articles / Web Development / HTML
Article

SWFObject Server Control for ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.56/5 (5 votes)
15 May 2007 127.8K   72   33
A server control that implements the SWFObject JavaScript library.

Introduction

SWFObject is a JavaScript library for dynamically embedding Flash content in your HTML pages. It's cross-browser capable and it gets rid of the annoying 'Click to enable control' message in Internet Explorer, plus other useful features. For more details, visit this blog.

Using the code

This is how you would use SWFObject in an ASP.NET page:

JavaScript
<div id="container">
   <script type="text/javascript">
      var flash = new SWFObject(<%= ("\"" + 
                  ResolveUrl("~/swf/movie.swf") + 
                  "\"") %>, "", "600", "200", "6", "");
      with (flash) {
         addParam("menu", "false");
         addVariable("approot", <%= ( "\"" + 
                     Request.ApplicationPath + "\"") %>);
         write("container");
      }
   </script>
</div>

Using the server control I built, the code would look like this:

HTML
<div id="container" runat="server">
   <flash:SWFObject id="flash1" runat="server" 
           Movie="~/swf/movie.swf" Width="600" 
           Height="200" FlashVersion="6">
      <flash:SWFParameter runat="server" Name="menu" Value="false" />
      <flash:SWFVariable runat="server" Name="approot" 
             Value='<%#Request.ApplicationPath %>' />
   </flash:SWFObject>
</div>

Note that you can use databinding and add as many parameters and variables as you want. Here is the source code:

C#
[ParseChildren(typeof(SWFInput))]
[PersistChildren(true)]
public class SWFObject : Control {

   string _containerID = null, 
       _movie = "", _width = "", _height = "", _flashVersion = "";
   
   WModeEnum wmode = WModeEnum.NotSet;
   bool _menu = false;

   public string ContainerID { get { return _containerID; } set { 
       _containerID = value; } }
   public string Movie { get { return _movie; } set { _movie = value; } }
   public string Width { get { return _width; } set { _width = value; } }
   public string Height { get { return _height; } set { _height = value; } }
   public string FlashVersion { get { return _flashVersion; } set { 
       _flashVersion = value; } }

   public bool Menu { get { return _menu; } set { _menu = value; } }
   public WModeEnum WMode { get { return wmode; } set { wmode = value; } }
   
   // If you don't implement a webresource for 
   //the javascript file then delete this method.
   protected override void OnPreRender(EventArgs e) {
      base.OnPreRender(e);

      Page.ClientScript.RegisterClientScriptInclude(
         this.GetType(),
         "swfobject.js",
         Page.ClientScript.GetWebResourceUrl(this.GetType(), "swfobject.js")
      );
   }

   public override void RenderControl(HtmlTextWriter writer) {

      string movie = ResolveClientUrl(_movie);

      string id = (_containerID == null) ? Parent.ClientID : _containerID;

      string jsVar = id + "_SWFObject";

      writer.WriteLine(string.Format("<!-- \"{0}\" with SWFObject START -->", 
          movie));

      writer.WriteBeginTag("script");
      writer.WriteAttribute("type", "text/javascript");
      writer.WriteLine(HtmlTextWriter.TagRightChar);

      writer.WriteLine(string.Format("var {0} = new SWFObject
      ('{1}', '', '{2}', '{3}', '{4}', '');", new object[] 
      { jsVar, movie, _width, _height, _flashVersion }));

      writer.WriteLine(string.Format("with ({0}) {{", jsVar));

      foreach (Control control in Controls) {
         if (control is SWFInput) 
            RenderInput((SWFInput)control, writer);
      }
      
      RenderInput(new SWFParameter("menu", _menu.ToString()), writer);
      if (wmode != WModeEnum.NotSet) 
         RenderInput(new SWFParameter("wmode", wmode.ToString()), writer);

      writer.WriteLine(string.Format("write('{0}');", id));
      writer.WriteLine("}");
      writer.WriteEndTag("script");
      writer.WriteLine();

      writer.Write(string.Format("<!-- \"{0}\" with SWFObject END -->", 
          movie));

   }

   protected virtual void RenderInput(SWFInput input, HtmlTextWriter writer) {

      writer.WriteLine(
         string.Format("add{0}('{1}', '{2}');", 
         ((input is SWFParameter) ? "Param" : "Variable"), 
         input.Name, input.Value)
      );
   }

   #region Nested Types

   public enum WModeEnum { NotSet, Window, Opaque, Transparent }

   #endregion
}

public class SWFParameter : SWFInput {

   public SWFParameter() { }

   public SWFParameter(string name, string value) {
      Name = name;
      Value = value;
   }
}

public class SWFVariable : SWFInput { }

[ParseChildren(true)]
public abstract class SWFInput : Control {
   string _name = "";
   string _value = "";

   public string Name { get { return _name; } set { _name = value; } }
   public string Value { get { return _value; } set { _value = value; } }
}

History

  • 12 Dec, 2006 - Original version posted
  • 21 Dec, 2006 - Updated
  • 15 May, 2007 - Updated

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here



Comments and Discussions

 
QuestionSWFObject is undefined ? Pin
Stephanois1416-Feb-08 21:13
Stephanois1416-Feb-08 21:13 
AnswerRe: SWFObject is undefined ? Pin
dffefqefe17-Feb-08 3:48
dffefqefe17-Feb-08 3:48 
GeneralRe: SWFObject is undefined ? Pin
Stephanois1417-Feb-08 7:33
Stephanois1417-Feb-08 7:33 
AnswerRe: SWFObject is undefined ? Pin
dffefqefe17-Feb-08 8:13
dffefqefe17-Feb-08 8:13 
GeneralRe: SWFObject is undefined ? Pin
Stephanois1418-Feb-08 10:28
Stephanois1418-Feb-08 10:28 
You right !
I downloaded Reflector and I'm now sure that the name matches the assembly. I also found the information on msdn : The WebResource definition must include the default namespace of the assembly (FlashPlayer) and the name of the .js file.

I'm now able to run a video file with a swf file but in fact, I would like to run a flv file with the free video player : flowplayer (http://flowplayer.org/player/quick-start.html[^]).

I tried the following code but the browser indicates an error in the SWFVariable tag (expected ')' near honda.flv):

<body>
<form id="form1" runat="server">
<div id="container" runat="server">
<cc1:SWFObject id="flash1" runat="server"
Movie="FlowPlayerLP.swf" Width="400"
Height="200" FlashVersion="6" WMode="Opaque" >
<cc1:SWFVariable runat="server" Name="config"
Value="{videoFile: 'honda.flv'}" />
</cc1:SWFObject>
</div>
</form>
</body>

The html ouput is like this :

<script src="/EmbeddedFlowPlayer/WebResource.axd?d=hyuIfFuFLLPoqm6pTUu-kOhd5B5DfufKUqcRC_uVX51gy_IKS3FsaWvYlD1AlxkV0&amp;t=633389673879375000" type="text/javascript"></script>
<div id="container">
<!-- "FlowPlayerLP.swf" with SWFObject START -->
<script type="text/javascript">
var container_SWFObject = new SWFObject('FlowPlayerLP.swf', '', '400', '200', '6', '');
with (container_SWFObject) {
addVariable('config', '{videoFile: 'honda.flv'}');
addParam('menu', 'False');
addParam('wmode', 'Opaque');
write('container');
}
</script>
<!-- "FlowPlayerLP.swf" with SWFObject END -->
</div>

I would like to add the flv video as a parameter. Any support would be appreciable.
Thanks for your support.
GeneralRe: SWFObject is undefined ? Pin
dffefqefe18-Feb-08 12:06
dffefqefe18-Feb-08 12:06 
AnswerRe: SWFObject is undefined ? Pin
Stephanois1420-Feb-08 6:43
Stephanois1420-Feb-08 6:43 
QuestionSetting variables in C# rather than ASP.NET Pin
MarcusNaderus24-Oct-07 13:27
MarcusNaderus24-Oct-07 13:27 
AnswerRe: Setting variables in C# rather than ASP.NET Pin
dffefqefe24-Oct-07 13:32
dffefqefe24-Oct-07 13:32 
QuestionProblem with databinding? Pin
jpbosk3-May-07 0:10
jpbosk3-May-07 0:10 
AnswerRe: Problem with databinding? Pin
dffefqefe3-May-07 5:03
dffefqefe3-May-07 5:03 
QuestionRe: Problem with databinding? Pin
jpbosk10-May-07 21:55
jpbosk10-May-07 21:55 
AnswerRe: Problem with databinding? Pin
dffefqefe12-May-07 16:25
dffefqefe12-May-07 16:25 
QuestionRe: Problem with databinding? Pin
jpbosk20-May-07 23:07
jpbosk20-May-07 23:07 
AnswerRe: Problem with databinding? Pin
dffefqefe21-May-07 6:20
dffefqefe21-May-07 6:20 
GeneralAlternative with full design time support Pin
g00fyman5-Feb-07 22:49
g00fyman5-Feb-07 22:49 
GeneralRe: Alternative with full design time support Pin
dffefqefe6-Feb-07 3:32
dffefqefe6-Feb-07 3:32 
GeneralRe: Alternative with full design time support Pin
g00fyman6-Feb-07 11:45
g00fyman6-Feb-07 11:45 
GeneralRe: Alternative with full design time support Pin
dffefqefe6-Feb-07 12:12
dffefqefe6-Feb-07 12:12 
GeneralRe: Alternative with full design time support Pin
g00fyman6-Feb-07 13:08
g00fyman6-Feb-07 13:08 
GeneralRe: Alternative with full design time support Pin
Juan_IBD22-Mar-07 2:39
Juan_IBD22-Mar-07 2:39 
GeneralRe: Alternative with full design time support Pin
Michael B. Hansen15-May-07 19:53
Michael B. Hansen15-May-07 19:53 
GeneralRe: Alternative with full design time support Pin
g00fyman16-May-07 4:05
g00fyman16-May-07 4:05 
QuestionSending Commands to Flash Pin
mark_e_mark18-Jan-07 0:04
mark_e_mark18-Jan-07 0:04 
AnswerRe: Sending Commands to Flash Pin
dffefqefe18-Jan-07 2:41
dffefqefe18-Jan-07 2:41 

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.