Click here to Skip to main content
12,700,395 members (34,922 online)
Click here to Skip to main content
Add your own
alternative version

Stats

93.5K views
148 downloads
72 bookmarked
Posted

SWFObject Server Control for ASP.NET

, 15 May 2007
Rate this:
Please Sign up or sign in to vote.
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:

<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

Share

About the Author


You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionSWFObject is undefined ? Pin
Stephanois1416-Feb-08 22:13
memberStephanois1416-Feb-08 22:13 
AnswerRe: SWFObject is undefined ? Pin
maxtoroq17-Feb-08 4:48
membermaxtoroq17-Feb-08 4:48 
GeneralRe: SWFObject is undefined ? Pin
Stephanois1417-Feb-08 8:33
memberStephanois1417-Feb-08 8:33 
AnswerRe: SWFObject is undefined ? Pin
maxtoroq17-Feb-08 9:13
membermaxtoroq17-Feb-08 9:13 
GeneralRe: SWFObject is undefined ? Pin
Stephanois1418-Feb-08 11:28
memberStephanois1418-Feb-08 11:28 
GeneralRe: SWFObject is undefined ? Pin
maxtoroq18-Feb-08 13:06
membermaxtoroq18-Feb-08 13:06 
AnswerRe: SWFObject is undefined ? Pin
Stephanois1420-Feb-08 7:43
memberStephanois1420-Feb-08 7:43 
QuestionSetting variables in C# rather than ASP.NET Pin
MarcusNaderus24-Oct-07 14:27
memberMarcusNaderus24-Oct-07 14:27 
AnswerRe: Setting variables in C# rather than ASP.NET Pin
maxtoroq24-Oct-07 14:32
membermaxtoroq24-Oct-07 14:32 
QuestionProblem with databinding? Pin
jpbosk3-May-07 1:10
memberjpbosk3-May-07 1:10 
AnswerRe: Problem with databinding? Pin
maxtoroq3-May-07 6:03
membermaxtoroq3-May-07 6:03 
QuestionRe: Problem with databinding? Pin
jpbosk10-May-07 22:55
memberjpbosk10-May-07 22:55 
AnswerRe: Problem with databinding? Pin
maxtoroq12-May-07 17:25
membermaxtoroq12-May-07 17:25 
QuestionRe: Problem with databinding? Pin
jpbosk21-May-07 0:07
memberjpbosk21-May-07 0:07 
AnswerRe: Problem with databinding? Pin
maxtoroq21-May-07 7:20
membermaxtoroq21-May-07 7:20 
GeneralAlternative with full design time support Pin
g00fyman5-Feb-07 23:49
memberg00fyman5-Feb-07 23:49 
GeneralRe: Alternative with full design time support Pin
maxtoroq6-Feb-07 4:32
membermaxtoroq6-Feb-07 4:32 
GeneralRe: Alternative with full design time support Pin
g00fyman6-Feb-07 12:45
memberg00fyman6-Feb-07 12:45 
GeneralRe: Alternative with full design time support Pin
maxtoroq6-Feb-07 13:12
membermaxtoroq6-Feb-07 13:12 
GeneralRe: Alternative with full design time support Pin
g00fyman6-Feb-07 14:08
memberg00fyman6-Feb-07 14:08 
GeneralRe: Alternative with full design time support Pin
Juan_IBD22-Mar-07 3:39
memberJuan_IBD22-Mar-07 3:39 
GeneralRe: Alternative with full design time support Pin
Michael B. Hansen15-May-07 20:53
memberMichael B. Hansen15-May-07 20:53 
GeneralRe: Alternative with full design time support Pin
g00fyman16-May-07 5:05
memberg00fyman16-May-07 5:05 
QuestionSending Commands to Flash Pin
mark_e_mark18-Jan-07 1:04
membermark_e_mark18-Jan-07 1:04 
AnswerRe: Sending Commands to Flash Pin
maxtoroq18-Jan-07 3:41
membermaxtoroq18-Jan-07 3:41 
AnswerRe: Sending Commands to Flash Pin
mark_e_mark18-Jan-07 4:07
membermark_e_mark18-Jan-07 4:07 
QuestionSource Files Pin
HarleyRoadKing20-Dec-06 4:52
memberHarleyRoadKing20-Dec-06 4:52 
AnswerRe: Source Files Pin
maxtoroq21-Dec-06 8:10
membermaxtoroq21-Dec-06 8:10 
QuestionWhy? Pin
Wayne Gibson18-Dec-06 23:59
memberWayne Gibson18-Dec-06 23:59 
AnswerRe: Why? Pin
maxtoroq19-Dec-06 3:53
membermaxtoroq19-Dec-06 3:53 
Generalheight property Pin
matt from ne18-Dec-06 11:25
membermatt from ne18-Dec-06 11:25 
GeneralRe: height property Pin
maxtoroq18-Dec-06 11:28
membermaxtoroq18-Dec-06 11:28 
GeneralNice job Pin
christopherbarrow12-Dec-06 9:56
memberchristopherbarrow12-Dec-06 9:56 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 15 May 2007
Article Copyright 2006 by dffefqefe
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid