Professional, Software

Url Parameters

I wanted to write a small sample about how to consume URL parameters that are passed in to html page in Silverlight application. Managed Browser DOM OM in Silverlight makes it really easy.

I created a simple Silverlight application with just one page.

<Canvas
        xmlns="http://schemas.microsoft.com/client/2007" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        x:Name="parentCanvas" 
        Loaded="Page_Loaded" 
        x:Class="UrlParam.Page;assembly=ClientBin/UrlParam.dll"
        Width="640"
        Height="480"
        Background="White"
        >
    <TextBlock x:Name="txtParam" Width="324" Height="27" 
             Canvas.Left="79" Canvas.Top="67" Text="Param:" 
             TextWrapping="Wrap"/>
</Canvas>

Page has a TextBlock element that can display the value that is passed in.

using System;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Collections.Generic;

namespace UrlParam
{
    public partial class Page : Canvas
    {
        public void Page_Loaded(object o, EventArgs e)
        {
            // Required to initialize variables
            InitializeComponent();
            Dictionary<string, string> urlparams = HtmlPage.QueryString as Dictionary<string,string>;
            string s = string.Empty;
            urlparams.TryGetValue("Param1", out s);
            txtParam.Text = "Param1: " + s;
        }
    }
}

Code uses HTMLPage class and its static QueryString property to get to the Url parameters

Advertisements
Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s