Create new account I forgot my password    

Read and Display ASP.NET Query Strings using Silverlight
Rating: 8 user(s) have rated this article Average rating: 3.3
Posted by: Malcolm Sheridan, on 4/25/2009, in category "Silverlight 2, 3 and 4"
Views: this article has been read 10717 times
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:
C#
using System.Windows.Browser;
VB.NET
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:
C#
HtmlDocument doc = HtmlPage.Document;
foreach (KeyValuePair<string, string> item in doc.QueryString)
{
      System.Diagnostics.Debug.WriteLine(item.Key + " " + item.Value);
}
 
VB.NET
 
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"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="200" Height="50">
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <ComboBox x:Name="cboQueryString" Height="25" Width="125">           
            <ComboBox.ItemTemplate>
                <DataTemplate>                   
                    <TextBlock x:Name="txtQueryString" Text="{Binding Key}"                     ToolTipService.ToolTip="{Binding Value}" />
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>       
    </Grid>
</UserControl>
 
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:
 
C#
 
public Page()
{
      InitializeComponent();
      BindQueryString();
}
       
public void BindQueryString()
{
      cboQueryString.ItemsSource = HtmlPage.Document.QueryString;
      cboQueryString.SelectedIndex = 0;
}
 
VB.NET
 
Public Sub New()
      InitializeComponent()
       BindQueryString()
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:
 
SilverlightApplication1TestPage.aspx?One=1&Two=2
 
SilverlightQueryString
 
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. 
 
 









Page copy protected against web site content infringement by Copyscape


How would you rate this article?

User Feedback

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel

NEWSLETTER