Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

refresh xap file from javascript RSS

3 replies

Last post Dec 22, 2008 02:21 PM by MarkMonster

(0)
  • skm.software@yahoo.com

    skm.software...

    Member

    236 Points

    298 Posts

    refresh xap file from javascript

    Dec 10, 2008 01:21 PM | LINK

    Hi all

    I have default.aspx page contains a button and silverlight control like <asp:silverlight id=".." source="path of xap file">
    I have  silverlight xap file, on click of this button in default.aspx, I want
    that its initparams change and xap file reload or refresh, this should be from javascript i.e no postback should be there.

    Can anyone specify the solution.

    Sachin Mukhija
    "Please Mark as answer if answer is correct".
  • Ken Tucker

    Ken Tucker

    All-Star

    23246 Points

    3532 Posts

    Re: refresh xap file from javascript

    Dec 10, 2008 04:47 PM | LINK

     I am not aware of any method to force a silverlight to reload without a postback.  You can make some of the silverlight app's functions callable by javascript. 

     

    http://msdn.microsoft.com/en-us/library/cc221414(VS.95).aspx

     

    But the html bridge allows silverlight to interact with the html page it is hosted on

    http://msdn.microsoft.com/en-us/library/cc645076(VS.95).aspx
  • Amanda Wang - MSFT

    Amanda Wang...

    All-Star

    17236 Points

    1466 Posts

    Microsoft

    Re: refresh xap file from javascript

    Dec 15, 2008 07:40 AM | LINK

     Hi,

    skm.software@yahoo.com

    I want
    that its initparams change and xap file reload or refresh, this should be from javascript i.e no postback should be there.

    Can anyone specify the solution.

     

    You can try to place the  silverlight control into a UpdatePanel to avoid the whole page postback.

    for example:

     <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/testChat.xap" InitParameters="user="  MinimumVersion="2.0.30923.0" Width="100%" Height="100%" />
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
            </Triggers>
        </asp:UpdatePanel>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click"  />

    the code behind of the button click  event:

    protected void Button1_Click(object sender, EventArgs e)
            {
                this.Xaml1.InitParameters = "user=susan";
            }

     

    Amanda Wang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • MarkMonster

    MarkMonster

    Star

    7590 Points

    1497 Posts

    Re: refresh xap file from javascript

    Dec 22, 2008 02:21 PM | LINK

    I created a blogpost on the solution here:

    The quick solution is as follows.

    A Javascript function to refresh the silverlight HostElement.

    function CreateSilverlight(hostElement, source, initParams) {
        var pluginId = hostElement.id + "PluginId";
        hostElement.innerHTML = Silverlight.createObject(source,
            null, pluginId,
            {
                width: "200", height: "50",
                background: "white", alt: "<!--Silverlight 2.not installed-->",
                version: "2.0.31005.0", autoUpgrade: true
            },
            { onError: onSLError, onLoad: onSLLoad },
            initParams, hostElement.id);
    }

     

    The inital load is from the window.load:

    window.onload = function() {
        CreateSilverlight(silverlightControlHost, "ClientBin/MM.Silverlight.Experiments2.SUI.xap", "param1=value1,param2=value2");       
    }

    The full html looks as follows:

      

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
        <title>MM.Silverlight.Experiments2.SUITestPage</title>
    
        <style type="text/css">
        html, body {
            height: 100%;
            overflow: auto;
        }
        body {
            padding: 0;
            margin: 0;
        }
        </style>
        <script type="text/javascript" src="Silverlight.js"></script>
        <script type="text/javascript">
            window.onload = function() {
                CreateSilverlight(silverlightControlHost, "ClientBin/MM.Silverlight.Experiments2.SUI.xap", "param1=value1,param2=value2");        
            }
    
            function CreateSilverlight(hostElement, source, initParams) {
                var pluginId = hostElement.id + "PluginId";
                hostElement.innerHTML = Silverlight.createObject(source,
                    null, pluginId,
                    {
                        width: "200", height: "50",
                        background: "white", alt: "<!--Silverlight 2.not installed-->",
                        version: "2.0.31005.0", autoUpgrade: true
                    },
                    { onError: onSLError, onLoad: onSLLoad },
                    initParams, hostElement.id);
            }
    
            function onSLLoad(plugIn, userContext, sender) {
                window.status +=
                    plugIn.id + " loaded into " + userContext + ". ";
            }
    
            function onSLError(sender, args) {
                // Display error message.
            }
        </script>
    </head>
    
    <body>
        <!-- Runtime errors from Silverlight will be displayed here.
    	This will contain debugging information and should be removed or hidden when debugging is completed -->
    	<div id='errorLocation' style="font-size: small;color: Gray;"></div>
    
        <div id="silverlightControlHost">
            
        </div>
        <iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>
    </body>
    </html>
    
     

    This solution can also be used from within Silverlight. You can call the javascript function very easily from Silverlight as follows:

    HtmlPage.Window.Eval(string.Format(
        @"CreateSilverlight(silverlightControlHost,
                            'ClientBin/MM.Silverlight.Experiments2.SUI.xap',
                            'currenttime={0},otherparam=othervalue');"
    , DateTime.Now));

     I hope this helps you out more.

     

    Mark Monster

    Mark Monster - Silverlight MVP - MCPD Enterprise
    Blog
    Silverlight and Expression Insiders UG

    Dont forget to click "Mark as Answer" on the post that helped you.