Firstly, it is not recommended to pass parameters to SilverLight like you did above. It depends on a postback and get the parameter on server side. Usually, we use SilverLight + WebService(avoid to postback).
Secondly, if you insist on your ideas. We need to find the SilverLight control on server side, then set its InitParameters. For example,
Now we shall get the transferred InitParameters in App.xaml. For example,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Test1
{
public partial class App : Application
{ public static string tramsferParam = ""; public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
}
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
// If the app is running outside of the debugger then report the exception using
// the browser's exception mechanism. On IE this will display it a yellow alert
// icon in the status bar and Firefox will display a script error.
if (!System.Diagnostics.Debugger.IsAttached)
{
// NOTE: This will allow the application to continue running after an exception has been thrown
// but not handled.
// For production applications this error handling should be replaced with something that will
// report the error to the website and stop the application.
e.Handled = true;
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework
Firstly, it is not recommended to pass parameters to SilverLight like you did above. It need to postback. Mostly, we use SilverLight + WebService(avoid to postback).
Secondly, if you insist on your ideas. We need to find the SilverLight control on server side, then set its InitParameters. For example,
Now we shall get the InitParameters in App.xaml. For example,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Test1
{
public partial class App : Application
{ public static string tramsferParam = ""; public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
}
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
// If the app is running outside of the debugger then report the exception using
// the browser's exception mechanism. On IE this will display it a yellow alert
// icon in the status bar and Firefox will display a script error.
if (!System.Diagnostics.Debugger.IsAttached)
{
// NOTE: This will allow the application to continue running after an exception has been thrown
// but not handled.
// For production applications this error handling should be replaced with something that will
// report the error to the website and stop the application.
e.Handled = true;
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework
I'm not dead-set on using a postback - why do you advise against it? The design I have in mind involves loading a new page anyway so if it's just to avoid reloading the page then I'm ok with it. Are there other reasons to avoid a postback?
I am already using webservices to access the membership information and the diagrams in the silverlight app which are stored in a database. I thought using the variables in a querystring would probably be a more straightforward approach for this part though
and also thought it would be useful to know how to do it this way.
I'm not dead-set on using a postback - why do you advise against it? The design I have in mind involves loading a new page anyway so if it's just to avoid reloading the page then I'm ok with it. Are there other reasons to avoid a postback?
What I really mean is SilverLight has the ability to get target datas by calling a WebService without a full postback just like AJAX.
Best regards,
Jonathan
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework
MontrayDavis
Member
238 Points
70 Posts
Re: Re: Re: What's the most straightforward way of passing a post parameter in a webpage to a sil...
Jul 05, 2008 08:13 PM | LINK
Texmex5, and that is for passing PARAMS via URL ( http://myweb.com/default.aspx?Variable1=GOOGLE
Jonathan She...
All-Star
50156 Points
4951 Posts
Microsoft
Re: What's the most straightforward way of passing a post parameter in a webpage to a silverlight...
Jul 07, 2008 01:40 PM | LINK
Hi Peterdungan,
Firstly, it is not recommended to pass parameters to SilverLight like you did above. It depends on a postback and get the parameter on server side. Usually, we use SilverLight + WebService(avoid to postback).
Secondly, if you insist on your ideas. We need to find the SilverLight control on server side, then set its InitParameters. For example,
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Panel ID="Panel1" runat="server">
<asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/Test1.xap" MinimumVersion="2.0.30523" Width="100%" Height="100%" />
</asp:Panel>
</form>
protected void Page_Load(object sender, EventArgs e)
{
string param = "123";
if (Request.Params["param"]!=null)
param = Request.Params["param"].ToString();
Silverlight mysl = (Silverlight) Page.FindControl("Xaml1") ;
mysl.InitParameters = "param=" + param;
}
Now we shall get the transferred InitParameters in App.xaml. For example,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Test1
{
public partial class App : Application
{
public static string tramsferParam = "";
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
tramsferParam = e.InitParams["param"];
this.RootVisual = new Page();
}
private void Application_Exit(object sender, EventArgs e)
{
}
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
// If the app is running outside of the debugger then report the exception using
// the browser's exception mechanism. On IE this will display it a yellow alert
// icon in the status bar and Firefox will display a script error.
if (!System.Diagnostics.Debugger.IsAttached)
{
// NOTE: This will allow the application to continue running after an exception has been thrown
// but not handled.
// For production applications this error handling should be replaced with something that will
// report the error to the website and stop the application.
e.Handled = true;
try
{
string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight 2 Application " + errorMsg + "\");");
}
catch (Exception)
{
}
}
}
}
}
Finally, get the paramer in Page.xaml. For example,
public Page()
{
InitializeComponent();
Loaded += new RoutedEventHandler(Page_Loaded);
}
void Page_Loaded(object sender, RoutedEventArgs e)
{
this.myTextBox.Text = App.tramsferParam;
}
Best regards,
Jonathan
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework
Jonathan She...
All-Star
50156 Points
4951 Posts
Microsoft
Re: What's the most straightforward way of passing a post parameter in a webpage to a silverlight...
Jul 07, 2008 01:40 PM | LINK
Hi Peterdungan,
Firstly, it is not recommended to pass parameters to SilverLight like you did above. It need to postback. Mostly, we use SilverLight + WebService(avoid to postback).
Secondly, if you insist on your ideas. We need to find the SilverLight control on server side, then set its InitParameters. For example,
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Panel ID="Panel1" runat="server">
<asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/Test1.xap" MinimumVersion="2.0.30523" Width="100%" Height="100%" />
</asp:Panel>
</form>
protected void Page_Load(object sender, EventArgs e)
{
string param = "123";
if (Request.Params["param"]!=null)
param = Request.Params["param"].ToString();
Silverlight mysl = (Silverlight) Page.FindControl("Xaml1") ;
mysl.InitParameters = "param=" + param;
}
Now we shall get the InitParameters in App.xaml. For example,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Test1
{
public partial class App : Application
{
public static string tramsferParam = "";
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
tramsferParam = e.InitParams["param"];
this.RootVisual = new Page();
}
private void Application_Exit(object sender, EventArgs e)
{
}
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
// If the app is running outside of the debugger then report the exception using
// the browser's exception mechanism. On IE this will display it a yellow alert
// icon in the status bar and Firefox will display a script error.
if (!System.Diagnostics.Debugger.IsAttached)
{
// NOTE: This will allow the application to continue running after an exception has been thrown
// but not handled.
// For production applications this error handling should be replaced with something that will
// report the error to the website and stop the application.
e.Handled = true;
try
{
string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight 2 Application " + errorMsg + "\");");
}
catch (Exception)
{
}
}
}
}
}
Finally, get the paramer in Page.xaml. For example,
public Page()
{
InitializeComponent();
Loaded += new RoutedEventHandler(Page_Loaded);
}
void Page_Loaded(object sender, RoutedEventArgs e)
{
this.myTextBox.Text = App.tramsferParam;
}
Best regards,
Jonathan
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework
peterdungan
Member
232 Points
178 Posts
Re: Re: What's the most straightforward way of passing a post parameter in a webpage to a silverl...
Jul 07, 2008 02:29 PM | LINK
Thanks for your reply Jonathan.
I'm not dead-set on using a postback - why do you advise against it? The design I have in mind involves loading a new page anyway so if it's just to avoid reloading the page then I'm ok with it. Are there other reasons to avoid a postback?
I am already using webservices to access the membership information and the diagrams in the silverlight app which are stored in a database. I thought using the variables in a querystring would probably be a more straightforward approach for this part though and also thought it would be useful to know how to do it this way.
I saw this thread on the asp.net forums: http://forums.asp.net/t/1271907.aspx
Which suggests
HtmlPage.Document.QueryString["key"]
would work in the SL app?
peterdungan
Member
232 Points
178 Posts
Re: Re: Re: What's the most straightforward way of passing a post parameter in a webpage to a sil...
Jul 07, 2008 03:44 PM | LINK
Yes that works, and is as simple a solution as i'd hoped.
To access it you need to addusing
System.Windows.Browser;and set the optional enableHtmlAccess parameter of the silverlight object to true.
Thanks for the replies.
Jonathan She...
All-Star
50156 Points
4951 Posts
Microsoft
Re: Re: What's the most straightforward way of passing a post parameter in a webpage to a silverl...
Jul 07, 2008 04:11 PM | LINK
Hi Peterdungan,
Yes. We suggest that you'd better do it like this.
void Page_Loaded(object sender, RoutedEventArgs e)
{
if (HtmlPage.Document.QueryString.Keys.Contains("param"))
myTextBox.Text = HtmlPage.Document.QueryString["param"].ToString();
}
What I really mean is SilverLight has the ability to get target datas by calling a WebService without a full postback just like AJAX.
Best regards,
Jonathan
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework