Avatar of razza_b
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.RegisterClientScriptBlock 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
Microsoft DevelopmentMicrosoft ApplicationsASP.NET

Avatar of undefined
Last Comment
razza_b

8/22/2022 - Mon
carlnorrbom

Hi,

In silverlight you can run javascript directly by using:

HtmlPage.Window.Invoke();

/Carl.
Gautham Janardhan

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.HtmlPage.Window.Eval(script);
razza_b

ASKER
I tried the following statement and got the following error:

  HtmlPage.Window.Eval("<script>alert('Print Job finished!');</script>");


System.InvalidOperationException was unhandled by user code
  Message=Microsoft JScript compilation error Syntax error
  StackTrace:
       at System.Windows.Browser.HtmlWindow.Eval(String code)
       at SilverlightNavigationApplicationTest.MainPage.button2_Click(Object sender, RoutedEventArgs e)
       at System.Windows.Controls.Primitives.ButtonBase.OnClick()
       at System.Windows.Controls.Button.OnClick()
       at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
       at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
  InnerException:

I tried adding the type and got the following error:

  HtmlPage.Window.Eval("<script type='text/javascript'>alert('Print Job finished!');</script>");

System.InvalidOperationException was unhandled by user code
  Message=Microsoft JScript compilation error Syntax error
  StackTrace:
       at System.Windows.Browser.HtmlWindow.Eval(String code)
       at SilverlightNavigationApplicationTest.MainPage.button2_Click(Object sender, RoutedEventArgs e)
       at System.Windows.Controls.Primitives.ButtonBase.OnClick()
       at System.Windows.Controls.Button.OnClick()
       at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
       at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
       at MS.Internal.JoltHelper.FireEvent(IntPtr 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("CreatePrinterScript", 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.
Your help has saved me hundreds of hours of internet surfing.
fblack61
SOLUTION
Gautham Janardhan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Gautham Janardhan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
razza_b

ASKER
The HtmlPage.Window.Eval(yourscript) 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("alert('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(JScript)

And this worked. So thanks.