Wednesday, December 23, 2009

HtmlPage.Window.Invoke in Silverlight

This method will invoke the javascript method from code behind.
Example

Page.xaml
<UserControl x:Class="_22Dec2009.Page" xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="1024" Height="800"> <Grid x:Name="LayoutRoot" Background="White"> <Border BorderThickness="2" BorderBrush="AliceBlue" CornerRadius="2" Margin="3"> <Button x:Name="Test" Click="Button_Click" Content="Test" Background="AliceBlue" Width="30" Height="20"> </Button> </Border> </Grid></UserControl>
Page.xaml.cs
This is code it will call the javascript method.
private void Button_Click(object sender, RoutedEventArgs e)

{ HtmlPage.Window.Invoke("getAlert", false); }
Default.aspx
<script language="javascript">
function getAlert() {
alert("sdf"); } </script>
Have you noticed in the eventargs. here in silverlight different..

Note:
Also we can call the javascript method throgh this code.
HtmlPage.Window.CreateInstance("getAlert", false);
happy coding! Enjoy