Solved

Creating & calling javascript function through silverlight event

Posted on 2010-11-09
6
1,020 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
Comment Utility
Hi,

In silverlight you can run javascript directly by using:

HtmlPage.Window.Invoke();

/Carl.
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 29

Assisted Solution

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

Accepted Solution

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

Author Comment

by:razza_b
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Viewers will learn the different options available in the Backstage view in Excel 2013.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now