Instead of using the Silverlight server control in your custom web parts, try using the html equivalent. When you create a Silverlight Application, it creates some test pages (if you like). You can use the aspx code example in your web parts OR, alternatively, you could use the html test page example. This seems to get around a ScriptManager render issue I recently ran accross: “System.InvalidOperationException: Script controls may not be registered after PreRender“. In the following code snippet, you can render the html for the Silverlight control and dynamically set the Width, Height, and Source params. This is just a starting point. Of course, there are additional params to set and custom html you could write. This is just a snippet and you will have to change the code to suit your specific needs.
public class SilverlightWebPart : WebPart
{
private string _SourceUrl = "ClientBin/SilverlightApplication1.xap";
[Personalizable(PersonalizationScope.Shared)]
public string SourceUrl
{
get { return _SourceUrl; }
set { _SourceUrl = value; }
}
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{
writer.WriteBeginTag("div");
writer.WriteAttribute("id", "silverlightControlHost");
writer.Write(">");
writer.WriteBeginTag("object");
writer.WriteAttribute("data", "data:application/x-silverlight,");
writer.WriteAttribute("type", "application/x-silverlight-2-b2");
writer.WriteAttribute("width", this.Width.ToString());
writer.WriteAttribute("height", this.Height.ToString());
writer.Write(">");
writer.WriteBeginTag("param");
writer.WriteAttribute("name", "source");
writer.WriteAttribute("value", this.SourceUrl);
writer.Write("/>");
writer.WriteBeginTag("param");
writer.WriteAttribute("name", "onerror");
writer.WriteAttribute("value", "onSilverlightError");
writer.Write("/>");
writer.WriteBeginTag("param");
writer.WriteAttribute("name", "background");
writer.WriteAttribute("value", "transparent");
writer.Write("/>");
writer.WriteBeginTag("a");
writer.WriteAttribute("href", "http://go.microsoft.com/fwlink/?LinkID=115261");
writer.WriteAttribute("style", "text-decoration: none;");
writer.Write(">");
writer.WriteBeginTag("img");
writer.WriteAttribute("src", "http://go.microsoft.com/fwlink/?LinkId=108181");
writer.WriteAttribute("alt", "Get Microsoft Silverlight");
writer.WriteAttribute("style", "border-style: none");
writer.Write("/>");
writer.WriteEndTag("a");
writer.WriteEndTag("object");
writer.WriteBeginTag("iframe");
writer.WriteAttribute("style", "visibility:hidden;height:0;width:0;border:0px");
writer.Write(">");
writer.WriteEndTag("iframe");
writer.WriteEndTag("div");
}
}