Sunday, August 8, 2010

Pass Parameters from ASP.NET to Silverlight

It is very straight forward to pass parameters from an asp.net page to a silverlight application. In my case, I had an asp.net application and wanted to integrate silverlight in one of my pages. I needed to pass some values such as ID and user role. Let's look at some code. First, I created a class, in my Silverlight application, to encapsulate my values. The constructor of my class takes in an IDictionary.

public class Dashboard
{
private string _ParishId;
private bool _IsReadOnly;
private bool _IsGSTSOrSuperParish;
internal Dashboard(IDictionary<string, string> parameters)
{
_ParishId = parameters["pid"];
_IsReadOnly = Convert.ToBoolean(parameters["iro"]);
_IsGSTSOrSuperParish = Convert.ToBoolean(parameters["igs"]);
}
public string ParishId
{
get { return _ParishId; }
}
public bool IsReadOnly
{
get { return _IsReadOnly; }
}
public bool IsGSTSOrSuperParish
{
get { return _IsGSTSOrSuperParish; }
}
}

Now, in the App.xaml application start up event i new up an instance of the class i created passing in the initParams of the StartupEventArgs.
Below is the markup needed for this.

private void Application_Startup(object sender, StartupEventArgs e)
{
Dashboard db = new Dashboard(e.InitParams);
this.RootVisual = new MainPage(db.ParishId,db.IsReadOnly,db.IsGSTSOrSuperParish);
}

Now that i have things set up for the mainPage.xaml, all i have to do is get those values on mainPage. And that's it on the silverlight side. I still have to set up how to get the actual values from ASP.NET. Below is the code for the mainPage.xaml.

public partial class MainPage : UserControl
{
private string _id;
private bool _IsReadOnly;
private bool _IsGSTSOrSuperParish;
public MainPage(string id, bool isReadOnly, bool isGSTSOrSuperParish)
{
InitializeComponent();
_id = id;
_IsReadOnly = isReadOnly;
_IsGSTSOrSuperParish = isGSTSOrSuperParish;
Loaded += new RoutedEventHandler(Page_Loaded);
}
...

Finally, I added a parameter (to the asp.net user control where my silverlight reference is) and called it initParam and gave it an ID of 'prm' so I can use it in code behind of this user control. First, let's take a look at the HTML.

<div id="silverlightControlHost">
<object id="tempSil" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="../ClientBin/DB.Silverlight.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="4.0.50401.0" />
<param name="autoUpgrade" value="true" />
<param name="initParams" runat="server" id="prm" />
<param name="windowless" value="true" />
 
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50401.0" style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
</a>
</object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>

And here is the code behind and is pretty straight forward..just passing in values.

public partial class UserControls_VE_db : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
using (var proxy = new CapitalAreaService.ServiceClient())
{
this.prm.Attributes["value"] = string.Format("pid={0},iro={1},igs={2}", 
proxy.GetParishId(SystemRole.GetParisAbbreviation()),SystemRole.IsReadOnly(),
SystemRole.IsGSTSOrSuperParish());
}
}
}

That's all it take to pass values from ASP.NET to Silverlight as needed.

No comments:

Post a Comment