Passing Querystring Parameters to a Silverlight Control

Two good screencasts on passing parameters to a Silverlight 2.0 Control are available on Silverlight.Net. The first from Mike Taulty demonstrates InitParameters as a property in the ASPX Silverlight control.  The second from Tim Heuer demonstrates using InitParameters as well as how to pass in Querystring parameters. Both are worth a look.

The approach to passing Querystring parameters to a Silverlight 2.0 Control that works for me is pretty simple.  We check for the presence of Querystrings in the app.xaml Application_Startup using the Silverlight HTML Bridge and pass those values to the page’s constructor.

APP.XAML ——————————–

private void Application_Startup(object sender, StartupEventArgs e)
{
    int fid = HtmlPage.Document.QueryString[“fid”] != null ?
        int.Parse(HtmlPage.Document.QueryString[“fid”].ToString()) : -1;
    int pid = HtmlPage.Document.QueryString[“pid”] != null ?
        int.Parse(HtmlPage.Document.QueryString[“pid”].ToString()) : -1;

    this.RootVisual = new InField.Grid(fid, pid);

}

GRID.XAML STARTUP PAGE ———————–

private int employeeID = 1;
private int postID = 1;

public Grid(int _employeeID, int _postID)
{

    employeeID = _employeeID;
    postID = _postID;

    InitializeComponent();
    PopulateGrid();
    editing = false;
}

That’s the rough cut.  The videos cover additional concepts.  Tim Heuer’s video includes downloadable sample code in both VB and C#.  Lots of useful information also in MSDN Library “Intergrating Silverlight with a Web Page.”

Article written by

A long time developer, I was an early adopter of Linux in the mid-90's for a few years until I entered corporate environments and worked with Microsoft technologies like ASP, then .NET. In 2008 I released Sueetie, an Online Community Platform built in .NET. In late 2012 I returned to my Linux roots and locked in on Java development. Much of my work is available on GitHub.