razza_b
asked on
Creating & calling javascript function through silverlight event
Hi
I'm working on a Visual Studio.NET 2010 Silverlight Application using C#. I've got a Silverlight Navigation Template which I've added a Master Page to. What i'd like to know is how to mimic the ClientScript.RegisterClien tScriptBlo ck using SilverLight.
I want one Silverlight event to write the javascript function to the Master Page and another Silverlight event to call that Javascript Function.
I have to do it this way as I'm using an embedded print runtime object which can extract the printer attached to the local pc.
Thanks
I'm working on a Visual Studio.NET 2010 Silverlight Application using C#. I've got a Silverlight Navigation Template which I've added a Master Page to. What i'd like to know is how to mimic the ClientScript.RegisterClien
I want one Silverlight event to write the javascript function to the Master Page and another Silverlight event to call that Javascript Function.
I have to do it this way as I'm using an embedded print runtime object which can extract the printer attached to the local pc.
Thanks
you can store the script into a string in the first event you want to create it and then where you want to invoke it you can do
System.Windows.Browser.Htm lPage.Wind ow.Eval(sc ript);
System.Windows.Browser.Htm
ASKER
I tried the following statement and got the following error:
HtmlPage.Window.Eval("<scr ipt>alert( 'Print Job finished!');</script>");
System.InvalidOperationExc eption was unhandled by user code
Message=Microsoft JScript compilation error Syntax error
StackTrace:
at System.Windows.Browser.Htm lWindow.Ev al(String code)
at SilverlightNavigationAppli cationTest .MainPage. button2_Cl ick(Object sender, RoutedEventArgs e)
at System.Windows.Controls.Pr imitives.B uttonBase. OnClick()
at System.Windows.Controls.Bu tton.OnCli ck()
at System.Windows.Controls.Pr imitives.B uttonBase. OnMouseLef tButtonUp( MouseButto nEventArgs e)
at System.Windows.Controls.Co ntrol.OnMo useLeftBut tonUp(Cont rol ctrl, EventArgs e)
at MS.Internal.JoltHelper.Fir eEvent(Int Ptr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
InnerException:
I tried adding the type and got the following error:
HtmlPage.Window.Eval("<scr ipt type='text/javascript'>ale rt('Print Job finished!');</script>");
System.InvalidOperationExc eption was unhandled by user code
Message=Microsoft JScript compilation error Syntax error
StackTrace:
at System.Windows.Browser.Htm lWindow.Ev al(String code)
at SilverlightNavigationAppli cationTest .MainPage. button2_Cl ick(Object sender, RoutedEventArgs e)
at System.Windows.Controls.Pr imitives.B uttonBase. OnClick()
at System.Windows.Controls.Bu tton.OnCli ck()
at System.Windows.Controls.Pr imitives.B uttonBase. OnMouseLef tButtonUp( MouseButto nEventArgs e)
at System.Windows.Controls.Co ntrol.OnMo useLeftBut tonUp(Cont rol ctrl, EventArgs e)
at MS.Internal.JoltHelper.Fir eEvent(Int Ptr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
InnerException:
If I add the javascript function to my master page and use this it works.
HtmlPage.Window.Invoke("Cr eatePrinte rScript", null);
My issue is that I can’t write the javascript function to the master page.
I’ve even tried creating a Scriptable Managed Type with no success.
HtmlPage.Window.Eval("<scr
System.InvalidOperationExc
Message=Microsoft JScript compilation error Syntax error
StackTrace:
at System.Windows.Browser.Htm
at SilverlightNavigationAppli
at System.Windows.Controls.Pr
at System.Windows.Controls.Bu
at System.Windows.Controls.Pr
at System.Windows.Controls.Co
at MS.Internal.JoltHelper.Fir
InnerException:
I tried adding the type and got the following error:
HtmlPage.Window.Eval("<scr
System.InvalidOperationExc
Message=Microsoft JScript compilation error Syntax error
StackTrace:
at System.Windows.Browser.Htm
at SilverlightNavigationAppli
at System.Windows.Controls.Pr
at System.Windows.Controls.Bu
at System.Windows.Controls.Pr
at System.Windows.Controls.Co
at MS.Internal.JoltHelper.Fir
InnerException:
If I add the javascript function to my master page and use this it works.
HtmlPage.Window.Invoke("Cr
My issue is that I can’t write the javascript function to the master page.
I’ve even tried creating a Scriptable Managed Type with no success.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The HtmlPage.Window.Eval(yours cript) worked. The reason it wasn't working before for me was because I was building the javascript string with the <script> tags and the function declaration.
I tested it first with HtmlPage.Window.Eval("aler t('Test!') ;") It succeeded so it was a matter of stripping the headers from the code and testing again by passing my string var, HtmlPage.Window.Eval(JScri pt)
And this worked. So thanks.
I tested it first with HtmlPage.Window.Eval("aler
And this worked. So thanks.
In silverlight you can run javascript directly by using:
HtmlPage.Window.Invoke();
/Carl.