|
I'm trying to implement the SWFObject in a ASP.Net Custom Control but with no success...
1) AssemblyInfo.cs
I would like to implement a webresource for the swfobject.js. I added the following code into the AssemblyInfo.cs file
//Script references
[assembly: WebResource("FlashPlayer.swfobject.js", "text/javascript")]
By right-clicking on the swfobject.js file in the Solution Explorer, I also set the Build Action property to "Embedded Resource".
2) Source code of the Custom Control
I Copyed/Pasted the source code from Codeproject.com.
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FlashPlayer
{
[ParseChildren(typeof(SWFInput))]
[PersistChildren(true)]
public class SWFObject : System.Web.UI.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);
//string resourceName = "FlashPlayer.swfobject.js";
//ClientScriptManager cs = this.Page.ClientScript;
//cs.RegisterClientScriptResource(typeof(FlashPlayer.SWFObject), ResolveClientUrl(resourceName));
Page.ClientScript.RegisterClientScriptInclude(
this.GetType(),
"swfobject.js",
Page.ClientScript.GetWebResourceUrl(this.GetType(), ResolveClientUrl("~/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; } }
}
}
3) Code aspx
After doing that, I implemented the following code :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="ClassLibraryFlowPlayer" Namespace="FlashPlayer" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Page sans titre</title>
</head>
<body>
<form id="form1" runat="server">
<div id="container" runat="server">
<cc1:SWFObject ID="SWFObject1" runat="server" FlashVersion="6" Height="100" Menu="false" Movie="honda.flv" Width="100" WMode="Window">
<cc1:SWFParameter ID="SWFParameter1" runat="server" Name="menu" Value="false" />
</cc1:SWFObject>
</div>
</form>
</body>
</html>
4) Result
Blank page !
The html page is like this :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
Page sans titre
</title></head>
<body>
<form name="form1" method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTE1MjI5NzM2OGRkX4Zn0Wzy5ZEdPmZPwiH7scpU+oc=" />
</div>
<script src="/EmbeddedFlowPlayer/WebResource.axd?d=k3q8BqSQ64eDCXxUkHBbO80tW57vd2URot1tZsOPBTMSIjOUik0QTcQ9Mm-M-Tnxj4Tvg35x3r3XX7B6SW7Vtw2&t=633387525832968750" type="text/javascript"></script>
<div id="container">
<!-- "honda.flv" with SWFObject START -->
<script type="text/javascript">
var container_SWFObject = new SWFObject('honda.flv', '', '100', '100', '6', '');
with (container_SWFObject) {
addParam('menu', 'false');
addParam('menu', 'False');
addParam('wmode', 'Window');
write('container');
}
</script>
<!-- "honda.flv" with SWFObject END -->
</div>
</form>
</body>
</html>
The warning from the browser indicates : SWFObject is undefined (pointed to line in black in the source page).
Any advice would be appreciated.
In advance, thanks your help.
|
|
|
|
|
Your problem is in the OnPreRender method. Copy it again and change "swfobject.js" with the name of your resource, which is "FlashPlayer.swfobject.js" (You don't have to use ResolveClientUrl, just pass the name of the resource). Also make sure your resource was embedded correctly and that the name matches, using Reflector.
|
|
|
|
|
I modified the OnPreRender method (see below) but same result. Page blank and SWFObject is undefined. Don't really understand the reason to use Reflector. Will work on that as soon as possible. Any other comment concerning the issue ?
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Page.ClientScript.RegisterClientScriptInclude(
this.GetType(),
"FlashPlayer.SWFObject.js",
Page.ClientScript.GetWebResourceUrl(this.GetType(), "FlashPlayer.SWFObject.js"));
}
|
|
|
|
|
I recommend Reflector so you make sure your resource was embedded, and that the name matches what you are expecting ("FlashPlayer.SWFObject.js"). Visual Studio will name the resource based on the project name and the folder in which the file resides, so maybe it got compiled with a different name.
Also make sure your control is inside a form tag with runat="server".
|
|
|
|
|
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&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.
|
|
|
|
|
Notice this line:
addVariable('config', '{videoFile: 'honda.flv'}');
The problem is with the quotation marks, maybe you can escape them or double them, or use doble quotes.
|
|
|
|
|
Great !
All works fine.
The finalized method should be :
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)
);
}
Very thanks for your stuff and your support.
|
|
|
|
|
Hello,
I'm trying to set variables to be passed to my swf movie via C# rather than Javascript. Can somebody help me in getting the correct syntax ?? (i simply cannot make it by my own - newbie newbie !!!) thanks a lot.
protected void Page_Load(object sender, EventArgs e)<br />
{<br />
myFlashMovie.Width = 600;<br />
myFlashMovie.Parameters.Quality = SWFObjectDotNET.util.flash.FlashQuality.Low;<br />
myFlashMovie.Parameters.Scale = SWFObjectDotNET.util.flash.FlashScale.ExactFit;<br />
myFlashMovie.Parameters.WMode = SWFObjectDotNET.util.flash.FlashWMode.Opaque;<br />
.....<br />
}
|
|
|
|
|
I think you are using another component, see below where it says 'Alternative with full design time support'.
|
|
|
|
|
Hello,
I have a problem using this control, witch is great and easy to use. But I have a problem with it. When I put it into a DataList's ItemTemplate, and bind some data on it with:
<xx:Variable ID="Variable8" runat="server" Name="buttonTxt" Value='<%# DataBinder.Eval(Container.DataItem, "ButtonText") %>' />
I get a "System.NullReferenceException: Object reference not set to an instance of an object." exception. When I follow the stack trace, it fails on this line:
foreach (Variable v in variables) {
v.DataBind(); // <------------
...
Anyone an idea how to fix this?
Kind regards,
Jean-Pierre Boskemper
The Netherlands
|
|
|
|
|
The problem is that the variable and parameter controls are not created as child controls of the swfobject. Therefore, when DataBind() is called on swfobject it is not propagated to the variable and parameters. As you can see in the code DataBind() for parameters and variables is called on the PreRender stage of the swfobject control, at which point the Container.DataItem is probably null.
I think this might be the problem. I had little experience when I wrote this code. Im now actually using a version that implements a webresource for the javascript.
A solution might be to override the CreateChildControls of the swfobject and add the parameters and variables to the Controls collection.
|
|
|
|
|
Well, I also have little experience with creating controls. I tried to override CreateChildControls and did the databinds of the parameters and the variables in that method. Unfortunately it also crashes with a NullReferenceException.
Can you maybe give me some sort of example how to do this? I should really appreciate that!
Kind regards,
Jean-Pierre Boskemper
The Netherlands
|
|
|
|
|
Jean-Pierre,
If recommended you to implement the CreateChildControls method so you didn't have to call DataBind from inside the control.
The code below should work. I'm using webresource for the javascript file. If you are not using webresource or don't know what it is then delete the OnPreRender method.
Tell me how it works out.
Max
-----
using System;
using System.Text;
using System.Web.UI;
namespace YourNameSpace {
[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; } }
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("", 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("", 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; } }
}
}
|
|
|
|
|
I'm sorry for the late answer, but I had a holiday last week.
I tried this version, and it works great now! But I don't get one thing: you are talking about the CreateChildControls method that should be implemented. But in your new version I don't see it. Can you explain this for me?
Furthermore, thanks for your help! This really simple but powerful control rocks!
Kind regards,
Jean-Pierre Boskemper
The Netherlands
|
|
|
|
|
In the previous version, the nested SWFParameter and SWFVariable objects were being added to a custom List property instead of the Control.Controls collection. Because of that databinding wasn't reaching them. One way of fixing that was to override the CreateChildControls method and move the items from one to collection to the other. Instead used a more simple approach, decided I didn't need the List property. The job is done by 2 attributes:
[ParseChildren(typeof(SWFInput))]
[PersistChildren(true)]
ParseChildren is telling the compiler only to accept child controls of type SWFInput. PersistChildren is telling to treat nested tags as nested controls instead of properties.
|
|
|
|
|
I wrote one like this a while back, cause i was sick of including files and writing script blocks when i was inside the VS IDE.
So i wrote a custom server control that has full design time support including smart tags.
Check it out here[^]
Maurice.
|
|
|
|
|
That's cool. Too bad you are not sharing your code though.
|
|
|
|
|
If you particularly need the source code, i can send it to you, no problems. Although it is a fairly simple control and shouldn't require modification.
If required please use the contact page on my site.
Regards,
Maurice.
|
|
|
|
|
I hit the Send button on your Contact page and got no message so I guess its sent.
|
|
|
|
|
Was working on my website, sorry about that. The source is in the mail
Maurice.
|
|
|
|
|
Hi.... i ´ve a parameter called imageURL, How do I set it to the flash movie at runtime?
Juan
|
|
|
|
|
As "maxtoroq", I think it is too bad you aren't sharing your source code as it sounds like a cool control.
What about writing a small article about your control - or just make the source code available for download?
/Michael
|
|
|
|
|
Hi there,
I have had every intention of writing my second article and it was to be on the construction of the SWFObject.NET control I created.
However, due to time constraints I have not yet done so. Now due to popular demand, nearly 1000 downloads we have made the source code available for others to extend and modify.
Please visit our Click 2 Install website at http://click2install.com.au/products/default.aspx[^] to download the control package, complete with source code.
Maurice
Click 2 Install Software.
|
|
|
|
|
Hi,
with your approach is there a way to be able to run setVariables from Javascript Buttons?
Regards
Mark
|
|
|
|
|
I think you can only add a variable before you use the write method. Sorry, I'm not a javascript to flash communication expert.
|
|
|
|
|