Sharker Khaleed Mahmud Silverlight Tips & Tricks

March 4, 2010

Tricks and Tips 34 : Pass parameter to Javascript from Silverlight

This example will show how to pass parameter to javascript function from silverlight.

TIPSTestPage.aspx

<script type=”text/javascript”>
        function CallMe(text) {
            alert(“this messagebox is from javascript:” + text)
        }
</script> 

MainPage.xaml

<UserControl x:Class=”TIPS.MainPage”
    xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation
    xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml
    xmlns:d=”http://schemas.microsoft.com/expression/blend/2008
    xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006
    mc:Ignorable=”d” d:DesignWidth=”640″ d:DesignHeight=”480″
    xmlns:dark=”clr-namespace:System.Windows.Controls.Theming;assembly=System.Windows.Controls.Theming.ExpressionDark”
    xmlns:local=”clr-namespace:TIPS”>
    <dark:ExpressionDarkTheme>
        <StackPanel>
            <Button x:Name=”btn” Content=”Call Javascript” Width=”120″ Height=”30″ />
        </StackPanel>
    </dark:ExpressionDarkTheme>
</UserControl>

MainPage.xaml.cs

using System.Windows.Controls;
using System.Windows.Browser;

namespace TIPS
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            this.btn.Click += (a, b) => {
                HtmlPage.Window.CreateInstance(“CallMe”,”Info from silverlight.”);
            };
        }
    }
}

Sharker Khaleed Mahmud
Web Developer

Older Posts »

Blog at WordPress.com.