views:

347

answers:

1

Hi,

sorry if this is a very simple question but how do I add a silverlight 3 object, i.e. a XAP, from the code behind of an asp.net application ??

A: 

You have two options. The first implies more code, but better "safety", and the other lets you add any custom HTML.

So either use:

HtmlGenericControl myHtmlObject = new HtmlGenericControl("object");

myHtmlObject.Attributes["data"] = "data:application/x-silverlight-2";

myHtmlObject.Attributes["type"] = "application/x-silverlight-2";

myHtmlObject.Attributes["width"] = "100%";
myHtmlObject.Attributes["height"] = "100%";
this.Page.Controls.Add(myHtmlObject);

HtmlGenericControl mySourceParam = new HtmlGenericControl("param");
mySourceParam.Attributes["name"] = "source";
mySourceParam.Attributes["value"] = "ClientBin/MySilverlightApplication.xap";
myHtmlObject.Controls.Add(mySourceParam);

HtmlGenericControl myOnErrorParam = new HtmlGenericControl("param");
myOnErrorParam .Attributes["name"] = "onError";
myOnErrorParam .Attributes["value"] = "onSilverlightError";
myHtmlObject.Controls.Add(myOnErrorParam);

// ... and so on ...

or:

LiteralControl myHtmlSnippet = new LiteralControl(
@"<object data=""data:application/x-silverlight-2,"" type=""application/x-silverlight-2"" width=""100%"" height=""100%"">
<param name=""source"" value=""ClientBin/SilverlightApplication10.xap"" />
<param name=""onError"" value=""onSilverlightError"" />

<!-- etc... -->

</object>
");

this.Page.Controls.Add(myHtmlSnippet);

    // enter code here
FaReSs