I am trying to have a simple block of JS code call my Silverlight control via the HTML Bridge. All of the dozen or so samples I have read ALL show using a HTML button with a wired up JS function on the click like so:
<input id="Button1" type="button" value="Test Call To SL" onclick="return CallSilverlight()" />
function CallSilverlight() {
var SLControl = document.getElementById("MySLControl");
SLControl.Content.Page.UpdateText("Hello from Javascript!");
}
The above works perfectly. The JS function ‘CallSilverlight’ is called and a Silverlight method I have is called. OK in my scenario I need to press an ASP.NET Server Control button, do some processing and then have the JS function called. Easy enough, in the finally block of the ASP.NET button click event I register the JS function: CallSilverlight() and indeed it does get called (breakpoint works). However I always get an “unknown exception” message from the debugger when making the call to the Silverlight method and that’s it!
I assume the PostBack is causing issue because that’s the main difference between the HTML button and the ASP.NET one. Is there any problem with using an ASP.NET button and then calling the JS as I have, and if not what might I be doing incorrectly?
Once again figured this out quite soon after posting the question – ASP.NET server controls (i.e. Buttons) can be used. However, the client side events must be used to call the JS function that calls the Silverlight scriptable methods rather than trying to register JS on a PostBack. So the button needs to look like the following:
Button.OnClientClick Property:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx