views:

5771

answers:

6

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?

+1  A: 

It is possible for a silverlight app to call out into javascript on the page which in turn could force your page refresh. So yes this is definitely possible!

Blounty
+7  A: 

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");
Patrick McDonald
HtmlPage.Window.Invoke("location.reload(true);"); didn't work for me after all. When I find out exactly what does, I'll post it here. thanks anyway.
Scott Ferguson
ok, this was 90% of the way there.All I needed was to create a javascript client function on the ASP.Net page in my case, called reload(), that did the location.reload(true).Then it was a simple matter from my C# code to have this line:HtmlPage.Window.Invoke("reload");Thanks again.
Scott Ferguson
nice one, I'll update my answer to include this new info
Patrick McDonald
+6  A: 

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.

R4cOON
This was exactly the sort of thing I was looking for. I'll check it out soon, and +1 your answer if it works the way I was expecting. Thanks!
Scott Ferguson
This works fine locally but doesn't work on the server.
ThePower
What do you mean? Of course it won't work on the server, Silverlight is on the client side!Do you mean you want to restart your web server from the client?
R4cOON
A: 

Here is a step by step walkthrough I wrote. Check it out. Feel free to check out the other articles and follow us on twitter!

http://www.meanbyte.com/thelockerroom/post/2009/06/25/Reloading-a-SilverLight-control-without-full-PostBack.aspx

Will

+1  A: 

In my case I didn't want to do asp.net kind of postback and lose my Silverlight page context so I refreshed my page by navigating to it. That way my language changes I made in my nav bar were reflected on my page or View as they come in 2008 template.

this.ContentFrame.Navigate(new Uri("", UriKind.Relative));

In this case I was on my home page. "" means home page if you examine the Silverlight templated navigation solution.

Jesse
A: 

i use this method on log out of my Silverlight page: System.Windows.Browser.HtmlPage.Document.Submit();

In IE everything works fine but in Firefox i get a white page without any error!!!

Does anyone now what possible happens here???

mercedakias