Click here to Skip to main content
6,630,901 members and growing! (19,119 online)
Email Password   helpLost your password?
Web Development » Custom Controls » General     Intermediate

SWFObject Server Control for ASP.NET

By maxtoroq

A server control that implements the SWFObject JavaScript library.
Javascript, HTML, C# 2.0, Windows, .NET 2.0, ASP.NET, WebForms, VS2005, Dev
Posted:12 Dec 2006
Updated:15 May 2007
Views:52,650
Bookmarked:65 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
5 votes for this article.
Popularity: 3.18 Rating: 4.56 out of 5

1

2

3
2 votes, 40.0%
4
3 votes, 60.0%
5

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:

<div id="container">
   <script type="text/javascript">
      var flash = new SWFObject(<%= ("\"" + 
                  ResolveUrl("~/swf/movie.swf") + 
                  "\"") %>

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

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

[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

About the Author

maxtoroq


Member

Occupation: Web Developer
Location: Chile Chile

Other popular Custom Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 33 (Total in Forum: 33) (Refresh)FirstPrevNext
QuestionSWFObject is undefined ? PinmemberStephanois1422:13 16 Feb '08  
AnswerRe: SWFObject is undefined ? Pinmembermaxtoroq4:48 17 Feb '08  
GeneralRe: SWFObject is undefined ? PinmemberStephanois148:33 17 Feb '08  
AnswerRe: SWFObject is undefined ? Pinmembermaxtoroq9:13 17 Feb '08  
GeneralRe: SWFObject is undefined ? PinmemberStephanois1411:28 18 Feb '08  
GeneralRe: SWFObject is undefined ? Pinmembermaxtoroq13:06 18 Feb '08  
AnswerRe: SWFObject is undefined ? PinmemberStephanois147:43 20 Feb '08  
QuestionSetting variables in C# rather than ASP.NET PinmemberMarcusNaderus14:27 24 Oct '07  
AnswerRe: Setting variables in C# rather than ASP.NET Pinmembermaxtoroq14:32 24 Oct '07  
QuestionProblem with databinding? Pinmemberjpbosk1:10 3 May '07  
AnswerRe: Problem with databinding? Pinmembermaxtoroq6:03 3 May '07  
QuestionRe: Problem with databinding? Pinmemberjpbosk22:55 10 May '07  
AnswerRe: Problem with databinding? Pinmembermaxtoroq17:25 12 May '07  
QuestionRe: Problem with databinding? Pinmemberjpbosk0:07 21 May '07  
AnswerRe: Problem with databinding? Pinmembermaxtoroq7:20 21 May '07  
GeneralAlternative with full design time support Pinmemberg00fyman23:49 5 Feb '07  
GeneralRe: Alternative with full design time support Pinmembermaxtoroq4:32 6 Feb '07  
GeneralRe: Alternative with full design time support Pinmemberg00fyman12:45 6 Feb '07  
GeneralRe: Alternative with full design time support Pinmembermaxtoroq13:12 6 Feb '07  
GeneralRe: Alternative with full design time support Pinmemberg00fyman14:08 6 Feb '07  
GeneralRe: Alternative with full design time support PinmemberJuan_IBD3:39 22 Mar '07  
GeneralRe: Alternative with full design time support PinmemberMichael B. Hansen20:53 15 May '07  
GeneralRe: Alternative with full design time support Pinmemberg00fyman5:05 16 May '07  
QuestionSending Commands to Flash Pinmembermark_e_mark1:04 18 Jan '07  
AnswerRe: Sending Commands to Flash Pinmembermaxtoroq3:41 18 Jan '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 May 2007
Editor: Sean Ewington
Copyright 2006 by maxtoroq
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project