Read and Display ASP.NET Query Strings using Silverlight

Posted by: Malcolm Sheridan , on 4/25/2009, in Category Silverlight 2, 3, 4 and 5
Views: 32441
Abstract: The following article demonstrates how to read and display ASP.NET query strings using Silverlight.
Read and Display ASP.NET Query Strings using Silverlight
The ASP.NET query string is a great way to share data between web pages and different web sites. This information is accessible in Silverlight thanks to the System.Windows.Browser namespace. The following article will explain how to enumerate through the query string collection. From there I will walk you through an example of binding the collection to a Silverlight ComboBox.
To access the query string collection, you’ll need to reference the System.Windows.Browser namespace in your code:
using System.Windows.Browser;
Imports System.Windows.Browser
The namespace allows you to access the HtmlDocument.QueryString property. This property returns a read-only collection of name/value pairs that represent the query string parameters on the current page’s URL. The values cannot be modified and if there are no query strings, an empty collection is returned. The following code demonstrates how to access the query string collection:
HtmlDocument doc = HtmlPage.Document;
foreach (KeyValuePair<string, string> item in doc.QueryString)
      System.Diagnostics.Debug.WriteLine(item.Key + " " + item.Value);
Dim doc As HtmlDocument = HtmlPage.Document
For Each item As KeyValuePair(Of String, String) In doc.QueryString
       System.Diagnostics.Debug.WriteLine(item.Key & " " & item.Value)
Next item
The QueryString property returns an IDictionary<string,string> object. Using the KeyValuePair class you can enumerate through each item in the collection and see the Key and Value properties. The key relates to the name of the query string and the value is the value of the query string. 
To me this is yet another reason why Silverlight is cool! The next step is to use the query strings and display them to the user. To begin with open Visual Studio 2008 and choose File > New > Project > Silverlight > Silverlight Application. Leave the web application alone as we won’t be touching that project for now. Navigate to the Silverlight application and add the following xaml to the Page.xaml file:
<UserControl x:Class="SilverlightApplication1.Page"
    Width="200" Height="50">
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <ComboBox x:Name="cboQueryString" Height="25" Width="125">           
                    <TextBlock x:Name="txtQueryString" Text="{Binding Key}"                     ToolTipService.ToolTip="{Binding Value}" />
In the xaml above, I have one ComboBox control that has a DataTemplate that is in charge of describing the visual structure of the object. The TextBlock inside of the DataTemplate will be populated by data binding the ComboBox to the query string collection. The query string key will be bound to the Text property and the query string value will be bound to the tooltip. Open the Page.xaml.cs or .vb file and add the following code to implement the data binding:
public Page()
public void BindQueryString()
      cboQueryString.ItemsSource = HtmlPage.Document.QueryString;
      cboQueryString.SelectedIndex = 0;
Public Sub New()
End Sub
Public Sub BindQueryString()
      cboQueryString.ItemsSource = HtmlPage.Document.QueryString
       cboQueryString.SelectedIndex = 0
End Sub
In the above code I have created a method called BindQueryString. This method assigns the query string collection to the ComboBox via the ComboBox.ItemsSource property. 
The last thing left to do is create some query strings when you debug the application. Right click the web application and choose Properties and click on the Web tab. Add the following text to the Specific Page text box:
Run the project and you’ll see the ComboBox will be populated with the query strings. Pretty cool if you ask me!
The entire source code of this article can be downloaded from here
If you liked the article,  Subscribe to the RSS Feed or Subscribe Via Email  
Malcolm Sheridan is an independent contractor who has been working with Microsoft technologies since VB4. Malcolm has worked with .NET since its inception and thoroughly enjoys ASP.NET. 

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Malcolm Sheridan is a Microsoft awarded MVP in ASP.NET, a Telerik Insider and a regular presenter at conferences and user groups throughout Australia and New Zealand. Being an ASP.NET guy, his focus is on web technologies and has been for the past 10 years. He loves working with ASP.NET MVC these days and also loves getting his hands dirty with jQuery and JavaScript. He also writes technical articles on ASP.NET for SitePoint and other various websites. Follow him on twitter @malcolmsheridan

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Santosh Barnawal on Thursday, September 15, 2011 9:16 AM
Dear Sir,
I want to pass Displayed Data of Combo Box to a string having Different Value Member of the same combo box. But I am not able to yet. Would you please give me the solution.
Santosh PB






jQuery CookBook