To communicate between a Silverlight control and javascript in an ASP.net page you can use the HTML bridge. Essentially this allows you call specially marked methods in your Silverlight control as well call javascript routines from Silverlight.
Marking a Silverlight method or attribute as accessible to javascript:
public partial class Page : UserControl
{
void Page_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
// Register this object in the page as Scriptable
// so it can be accessed from within JavaScript
HtmlPage.RegisterScriptableObject("Page", page);
}
[ScriptableMember]
public string HelloFromSilverlight()
{
return "Hello";
}
[ScriptableMemberAttribute]
public int SomeValue
{
get
{ return _someValue;}
}
}
Calling the Silverlight method from javascript:
// Get a reference to the actual Silverlight
// plugin element within the page
var plugin = pluginObject.get_element();
// Call the HelloFromSilverlight method of our Silverlight object
var text = plugin.Content.Page.HelloFromSilverlight();
Also see other examples:
- http://www.dotnetspider.com/resources/36450-How-work-with-HTML-DOM-SIlverlight.aspx
- http://geekswithblogs.net/PeterTweed/archive/2009/08/08/html-bridge---silverlight-javascript-interop.aspx