none
How to refer a js file in xmal file

    Question

  • Hai,

    I have created a javascript file named 'MyJSFunctions.js' . In that file i have a function as follows:

     

    function alertText(text)
     {
    
         alert(text);
    }

     In my xmal.cs , on a button click i have written the following code:

      HtmlPage.Window.Invoke("alertText", new string[] {"Hi!.." });

     I im getting the following exception:

    "Failed to Invoke: alertText."

    How can i give reference to my javascript file in xmal

    Regards,

    Devu

    Friday, February 05, 2010 12:57 AM

Answers

  • HI,

    Just include that Javascript file to the html or aspx page on which your Silverlight control is hosted. It should work then.

    (if you still have problem then let me know otherwise mark reply as answer.)

    Friday, February 05, 2010 1:14 AM

All replies

  • HI,

    Just include that Javascript file to the html or aspx page on which your Silverlight control is hosted. It should work then.

    (if you still have problem then let me know otherwise mark reply as answer.)

    Friday, February 05, 2010 1:14 AM
  • Hai,

    Thanks for your reply. I  have linked my javascript file to my aspx page. Then it is working now.

    Regards,

    Devu.

    Friday, February 05, 2010 3:53 AM
  • hai,

    I want to know  how can i call a javascript function directly on a event in xmal file.

    Forexample i want something like the following.. i want to know is there any way to do as i underlined.

    <Button x:Name="btnInvokeJs" Click="MyJSFunction" Margin="100,200,160,48" VerticalAlignment="Bottom" Content="Invoke Button " Width="100" Height="20"/>

     

    Friday, February 05, 2010 4:22 AM
  • No Not possible. you have to call it from Code.
    Friday, February 05, 2010 8:13 AM
  •  agree

    Friday, February 05, 2010 1:03 PM
  • Hi,

    I was able to link my js file to the aspx page hosting the Silverlight control and invoke a js function. Can the Javascript code access the Silverlight control? How?

     

    Tuesday, February 09, 2010 5:30 PM
  • Tuesday, February 09, 2010 5:36 PM
  • To access Silvelright method from Javascript, you need to follow this steps

    1.) Give "ID" to the Silverlight object in HTML/ASPX page. eg:

     <asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/SilverlightJScriptInterop_App.xap" 
          MinimumVersion="2.0.30523" Width="100%" Height="100%" /> 
    
     
    2.) Declare the class as [ScriptableType] (the class in which the method is defined) eg:
    [ScriptableType]
    public partial class Page : UserControl
    {
      public Page()
      {            
        InitializeComponent();            
        Loaded += new RoutedEventHandler(Page_Loaded);       
      } 
    } 

    3.) Declare the method as [ScriptableMember] (the method that you want to access from the Javascript) eg: 

    [ScriptableMember]
    public void SetText(string text)     
    {           
    lblTest.Text = text;   


    4.) Call the method from the javascript eg:

    function setSilverlightText(){ 
    
       var pluginObject = $find("<%=Xaml1.ClientID%>"); 
    
       var plugin = pluginObject.get_element(); 
    
       plugin.Content.myObject.SetText(getText());
    
    } 
    
    function getText(){ 
    
       var obj = document.getElementById("<%=txtTextAspx.ClientID%>");  
    
      return obj.value;
    
    } 
    
     
    Wednesday, February 10, 2010 1:38 AM
  • When I insert the asp:silverlight element into my aspx page, I get

    "Element 'Silverlight' is not a known element. This can occur if there is a compilation error in the Web site, or the web.config file is missing."

    I have a web.config and the only error is "Element 'Silverlight' is not a known element"

    My aspx file is the test page created automatically when I selected to add a Silverlight project to my ASP.NET project. So it has the following:

    <div id="silverlightControlHost">
    <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
    <param name="source" value="ClientBin/SilverlightApplication.xap"/>
    <param name="onError" value="onSilverlightError" />
    <param name="background" value="white" />
    <param name="minRuntimeVersion" value="3.0.40624.0" />
    <param name="autoUpgrade" value="true" />
    <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration:none">
    <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
    </a>
    </object>
     

    I think the object is my Silverlight project. Where/How do I add asp:Silverlight in this case? I tried putting it after the object and I still get "Silverlight unknown" error. Do I even have to add it?

    Another thing - I tried to add a reference to system.web.silverlight.dll to the project hosting the control and could not find it in the list. But I am able to see the Silverlight templates when I create a new project.

    Hope you can help.

    Wednesday, February 10, 2010 12:28 PM
  • ......one more thing - I do have the Silverlight 3.0 tools and the Silverlight 3.0 SDK installed. I didn't have any previous versions of Silverlight installed that could interfere w my install. Also got no error messages.

    Wednesday, February 10, 2010 12:35 PM
  • I want to know  how can i call a javascript function directly on a event in xmal file.

     

    Hi Devika,

    This is not possible using XAML. You must have to call it from the code behind. Implement the Button_Click event in the code behind & call the Invoke method by passing the JsFunctionName with the parameters.

    Wednesday, February 10, 2010 12:47 PM
  • I can't find the dll in C:\Program Files\Microsoft SDKs\Silverlight\v3.0\Libraries\Server. Sorry for posting messages as I  go along. I am new to using forums.

    I shall have to uninstall and reinstall I guess. Don't need help for now.

    Wednesday, February 10, 2010 12:50 PM
  • http://silverlight.net/learn/videos/all/calling-javascript-functions-from-net-code/

    is a video where the demo in visual studio uses

     var plugin = $get("Xaml1");

    Like julie000 I am creating via Visual Web Developer Express 2008 and I do not know what syntax to use. Could anybody advise please?

    Saturday, February 27, 2010 8:58 PM