ASP.NET的SWFObject服务器控件

分享于 

6分钟阅读

Web开发

  繁體 雙語

介绍

SWFObject是一个用于在HTML页面中动态嵌入 Flash 内容的JavaScript库。 它是 跨浏览器 功能,它摆脱了 IE 中烦人的'单击以启用控制'消息,还有其他有用的特性。 要了解更多细节,请访问博客。

使用代码

这是在 ASP.NET 页面中使用SWFObject的方式:


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



使用生成的服务器控件,代码如下所示:


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



注意,你可以使用数据绑定并添加任意数量的参数和变量。 下面是源代码:


[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 Typespublicenum WModeEnum { NotSet, Window, Opaque, Transparent }
 #endregion}publicclass SWFParameter : SWFInput {
 public SWFParameter() { }
 public SWFParameter(string name, stringvalue) {
 Name = name;
 Value = value;
 }
}publicclass SWFVariable : SWFInput { }
[ParseChildren(true)]publicabstractclass SWFInput : Control {
 string _name = "";
 string _value = "";
 publicstring Name { get { return _name; } set { _name = value; } }
 publicstring Value { get { return _value; } set { _value = value; } }
}

历史记录

  • 12 2006年月 - 已经发布的原始版本
  • 21 2006年月 - 已经更新
  • 15 2007年05月 - 已经更新

Server  控制  asp  asp-net  SWF  Swfobject  
相关文章