Solved

Creating & calling javascript function through silverlight event

Posted on 2010-11-09
6
1,051 Views
Last Modified: 2013-11-12
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
0
Comment
Question by:razza_b
  • 3
  • 2
6 Comments
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 34099884
Hi,

In silverlight you can run javascript directly by using:

HtmlPage.Window.Invoke();

/Carl.
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 34099986
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);
0
 
LVL 1

Author Comment

by:razza_b
ID: 34100565
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.
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 29

Assisted Solution

by:Gautham Janardhan
Gautham Janardhan earned 500 total points
ID: 34100854
System.Windows.Browser.HtmlPage.Window.Eval("alert('hi there')");
0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
ID: 34100859
"<script>" tags are not required
0
 
LVL 1

Author Comment

by:razza_b
ID: 34100908
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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The canonical version of this article is on my web site here: http://iconoun.com/articles/collisions/ A companion presentation is available here: http://iconoun.com/articles/collisions/Unicode_Presentation.pdf
This collection of functions covers all the normal rounding methods of just about any numeric value.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question