Thursday, September 09, 2010
Download

 PassingParametersToSilverlight.zip

Purpose

We want to be able to pass paramaeters from ASP.NET pages to Silverlight 2 application. Here is one scenerios why this would be useful.

When consuming the web service in development, the web service will point to localhost whereas in QA and UAT web service url will change to qa.myserver.com and uat.SomeServer.com. We will store web service URL in appSettings of web.config allow avoiding hardcoded values in Silverlight 2 application.

Covered Topics

  • How to use asp:Silverlight control.

  • How to build InitParameters exposed by asp:Silverlight control.

  • How to consume InitParameters from Silverlight 2 application.

 PassingParametersToSilverlight.zip

Purpose

We want to be able to pass paramaeters from ASP.NET pages to Silverlight 2 application. Here is one scenerios why this would be useful.

When consuming the web service in development, the web service will point to localhost whereas in QA and UAT web service url will change to qa.myserver.com and uat.SomeServer.com. We will store web service URL in appSettings of web.config allow avoiding hardcoded values in Silverlight 2 application.

Covered Topics

  • How to use asp:Silverlight control.

  • How to build InitParameters exposed by asp:Silverlight control.

  • How to consume InitParameters from Silverlight 2 application.



Step By Step

Follow the instruction from Creating Silverlight 2 Project

Demo.aspx

<%@ Page Language="C#" AutoEventWireup="true" 
  CodeBehind="Demo.aspx.cs" Inherits="PassingParametersToSilverlight.Web.Demo" %>
 
<!DOCTYPE html PUBLIC 
  "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<%@ Register Assembly="System.Web.Silverlight" Namespace="System.Web.UI.SilverlightControls"
  TagPrefix="asp" %>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <asp:ScriptManager ID="ScriptManager1" runat="server" />
      <asp:Silverlight 
        ID="Xaml1" runat="server" Source="~/ClientBin/PassingParametersToSilverlight.xap"
        MinimumVersion="2.0.31005.0" Width="300px" Height="200px" />
    </div>
    <br /><br />
    Name:  <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    <br /><br />
    Content:  <asp:TextBox ID="txtContent" runat="server"></asp:TextBox>
    <br /><br />
    <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />
  </form>
</body>
</html>

Demo.aspx.cs

Xaml1.InitParameters can be used from asp.net to pass comma delimited name value key pairs.

using System;
using System.Text;
using System.Configuration;
 
namespace PassingParametersToSilverlight.Web
{
  public partial class Demo : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!this.IsPostBack)
      {
        Xaml1.InitParameters = BuildSilverlightParams(null, null);
      }
    }
 
    private string BuildSilverlightParams(string name, string content)
    {
      if (string.IsNullOrEmpty(name))
      {
        name = ConfigurationManager.AppSettings["Name"];
      }
      if(string.IsNullOrEmpty(content))
      {
        content = ConfigurationManager.AppSettings["Content"];
      }
 
      StringBuilder sb = new StringBuilder();
      sb.AppendFormat("Name={0}", name);
      sb.AppendFormat(",Content={0}", content);
 
      return sb.ToString();
    }
 
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
      Xaml1.InitParameters = BuildSilverlightParams(txtName.Text, txtContent.Text);
    }
  }
}

Web.config

<appSettings>
    <add key="Name" value="Betsy Moo"/>
    <add key="Content" value="Betsy is my dog's name!"/>
</appSettings>

Page.xaml

<UserControl x:Class="PassingParametersToSilverlight.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300"      
xmlns
:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns
:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc
:Ignorable="d">
    <Grid x:Name="LayoutRoot" Background="White">
      <TextBox Margin="31,64,145,0"          
Text
="" TextWrapping="Wrap" VerticalAlignment="Top"
Height
="60" x:Name="txtMsg"/>
  </Grid>
</UserControl>

Page.xaml.cs

using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Text;
 
namespace PassingParametersToSilverlight
{
  public partial class Page : UserControl
  {
    public Page(IDictionary<string, string> paramValues)
    {
      InitializeComponent();
      BuildMessage(paramValues);
    }
 
    private void BuildMessage(IDictionary<string, string> paramValues)
    {
      StringBuilder sb = new StringBuilder();
      sb.AppendFormat("Who is {0} ?{1}", paramValues["Name"], Environment.NewLine);
      sb.AppendFormat("{0}", paramValues["Content"]);
 
      txtMsg.Text = sb.ToString();
    }
  }
}

App.xaml.cs

e.InitParams contains name value key pairs set from asp.net page Xaml1.InitParameters

private void Application_Startup(object sender, StartupEventArgs e)
{
  this.RootVisual = new Page(e.InitParams);
}

 

Follow the instruction from Creating Silverlight 2 Project

Demo.aspx

<%@ Page Language="C#" AutoEventWireup="true" 
  CodeBehind="Demo.aspx.cs" Inherits="PassingParametersToSilverlight.Web.Demo" %>
 
<!DOCTYPE html PUBLIC 
  "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<%@ Register Assembly="System.Web.Silverlight" Namespace="System.Web.UI.SilverlightControls"
  TagPrefix="asp" %>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <asp:ScriptManager ID="ScriptManager1" runat="server" />
      <asp:Silverlight 
        ID="Xaml1" runat="server" Source="~/ClientBin/PassingParametersToSilverlight.xap"
        MinimumVersion="2.0.31005.0" Width="300px" Height="200px" />
    </div>
    <br /><br />
    Name:  <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    <br /><br />
    Content:  <asp:TextBox ID="txtContent" runat="server"></asp:TextBox>
    <br /><br />
    <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />
  </form>
</body>
</html>

Demo.aspx.cs

Xaml1.InitParameters can be used from asp.net to pass comma delimited name value key pairs.

using System;
using System.Text;
using System.Configuration;
 
namespace PassingParametersToSilverlight.Web
{
  public partial class Demo : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!this.IsPostBack)
      {
        Xaml1.InitParameters = BuildSilverlightParams(null, null);
      }
    }
 
    private string BuildSilverlightParams(string name, string content)
    {
      if (string.IsNullOrEmpty(name))
      {
        name = ConfigurationManager.AppSettings["Name"];
      }
      if(string.IsNullOrEmpty(content))
      {
        content = ConfigurationManager.AppSettings["Content"];
      }
 
      StringBuilder sb = new StringBuilder();
      sb.AppendFormat("Name={0}", name);
      sb.AppendFormat(",Content={0}", content);
 
      return sb.ToString();
    }
 
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
      Xaml1.InitParameters = BuildSilverlightParams(txtName.Text, txtContent.Text);
    }
  }
}

Web.config

<appSettings>
    <add key="Name" value="Betsy Moo"/>
    <add key="Content" value="Betsy is my dog's name!"/>
</appSettings>

Page.xaml

<UserControl x:Class="PassingParametersToSilverlight.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300"      
xmlns
:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns
:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc
:Ignorable="d">
    <Grid x:Name="LayoutRoot" Background="White">
      <TextBox Margin="31,64,145,0"          
Text
="" TextWrapping="Wrap" VerticalAlignment="Top"
Height
="60" x:Name="txtMsg"/>
  </Grid>
</UserControl>

Page.xaml.cs

using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Text;
 
namespace PassingParametersToSilverlight
{
  public partial class Page : UserControl
  {
    public Page(IDictionary<string, string> paramValues)
    {
      InitializeComponent();
      BuildMessage(paramValues);
    }
 
    private void BuildMessage(IDictionary<string, string> paramValues)
    {
      StringBuilder sb = new StringBuilder();
      sb.AppendFormat("Who is {0} ?{1}", paramValues["Name"], Environment.NewLine);
      sb.AppendFormat("{0}", paramValues["Content"]);
 
      txtMsg.Text = sb.ToString();
    }
  }
}

App.xaml.cs

e.InitParams contains name value key pairs set from asp.net page Xaml1.InitParameters

private void Application_Startup(object sender, StartupEventArgs e)
{
  this.RootVisual = new Page(e.InitParams);
}

 


Print  

NASPassingParametersToSilverlight
Minimize


Name:

Content:



 

 

Privacy Statement  |  Terms Of Use
Copyright 2009 by New Age Solution Inc.