Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Silverlight initiate Page Refreshes?

UPDATE: An alternative title for this could be: How do I call javascript from my silverlight 2.0 application.

Here is a quick question for all you Silverlight gurus.

I have a Silverlight app that displays a stopwatch countdown. The app is hosted in an ASP.Net web application, What I want it to do is when the stopwatch hits zero, the app forces a server page refresh of the hosting page.

Is this possible?

If so, any chance of a code snippet?

like image 1000
Scott Ferguson Avatar asked Feb 16 '09 09:02

Scott Ferguson


2 Answers

Why not simply stay on the Silverlight side and call

System.Windows.Browser.HtmlPage.Document.Submit();

Works a treat for me. The whole page gets reloaded and the Silverlight control kicks backs in.

like image 57
R4cOOn Avatar answered Oct 24 '22 22:10

R4cOOn


Apparently you can call a JS script from Silverlight using

HtmlPage.Window.CreateInstance

or

HtmlPage.Window.Invoke

The JavaScript to refresh a page is

location.reload(true)

I'm not a Silverlight or JavaScript expert though, so not sure if it works in all browsers, or even at all.

EDIT:

Scott posted a comment to this answer with his final solution.

He needed to create a JavaScript client function on the ASP.Net page called reload() that did the location.reload(true). Then it was a simple matter from his C# code to reload:

HtmlPage.Window.Invoke("reload");

As @R4cOON suggested, you can also use:

System.Windows.Browser.HtmlPage.Document.Submit();
like image 42
Patrick McDonald Avatar answered Oct 24 '22 21:10

Patrick McDonald