It’s fairly easy to pass querystring’s to silverlight.
IDictionary<string, string> queryStrings = System.Windows.Browser.HtmlPage.Document.QueryString;
So, for my example, if a querystring is passed in I grab the values and do the search, else get all and bind to my silverlight datagrid
IDictionary<string, string> queryStrings = System.Windows.Browser.HtmlPage.Document.QueryString;
proxy = new One.PilotStatService.PilotStatsServiceClient();
proxy.GetPilotStatsCompleted += new EventHandler<One.PilotStatService.GetPilotStatsCompletedEventArgs>(proxy_GetPilotStatsCompleted);
proxy.GetPilotStatsByNameCompleted += new EventHandler<One.PilotStatService.GetPilotStatsByNameCompletedEventArgs>(proxy_GetPilotStatsByNameCompleted);
proxy.SavePilotCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_SavePilotCompleted);
if (queryStrings.Count > 0 && !string.IsNullOrEmpty(queryStrings["Name"]))
proxy.GetPilotStatsByNameAsync(queryStrings["Name"]);
else
proxy.GetPilotStatsAsync();
An alternative is to pass parameters in from the page controls ‘inputparameters’ collection. From the aspx silverlight control:
asp:Silverlight ID=”Xaml1″ InitParameters=”Name=JG53″ runat=”server” Source=”~/ClientBin/One.xap” MinimumVersion=”2.0.30523″ Width=”100%” Height=”100%”
In this case, I’ve hardcoded a default value: InitParameters=”Name=JG53″
If you are using the straight html method vs. the aspx control:
<object data="data:application/x-silverlight," type="application/x-silverlight-2-b2" width="100%" height="100%">
<param name="source" value="ClientBin/One.xap"/>
<param name="onerror" value="onSilverlightError" />
<param name="background" value="white" />
<param name="initParams" value="Name=JG53" />
<a href="http://go.microsoft.com/fwlink/?LinkID=115261" style="text-decoration: none;">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
</a>
</object>
You capture these values in the silverlight ‘app’ (App.xaml.cs):
private void Application_Startup(object sender, StartupEventArgs e)
{
string inputParm = e.InitParams["Name"];
this.RootVisual = new Page(inputParm);
}
I modified the Page constructor to accept the parameter:
public Page(string inputParam)
So, let’s say I don’t want this hardcoded, ie. I receive the value from a control, etc… with the Silverlight aspx control I can access the inputParameter property. For example, with a asp:Silverlight control id of ‘Xaml1′:
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Xaml1.InitParameters = "Name=JG53";
}
</script>
A tad off topic, but another interesting feature of Silverlight is the ability to manipulate the html dom within managed code!
A simple example, building off the above: Let’s add a simple input html control to the page :
<form id="form1" runat="server" style="height:100%;">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<input id="MyTestBox" type="text" />
<div style="height:100%;">
<asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/One.xap" MinimumVersion="2.0.30523" Width="100%" Height="100%" />
</div>
</form>
The input ‘MyTextBox’ – now, when the control loads we are setting the InitParameters to ‘Name=JG53′. Within the Silverlight Page constructor I can access the input box:
HtmlDocument doc = HtmlPage.Document;
IDictionary<string, string> queryStrings = doc.QueryString;
doc.GetElementById("MyTestBox").SetProperty("value", inputParam);
This obviously offers up a ton of added capability, being able to manipulate the html dom from within managed code!
Finally, to cap this all off, if I want to build a search page, but keep the input box for search parameters within the html vs. inside the silverlight UI (or both), I can access the html from within Silverlight and retrieve values:
HtmlDocument doc = HtmlPage.Document;
string test = doc.GetElementById("MyTestBox").GetProperty("value").ToString();
So, I hope this has covered the many options of interfacing with Silverlight!