From: http://www.dotblogs.com.tw/lolota/archive/2008/12/16/6410.aspx
We know that Silverlight can be "embedded" in aspx, but you have thought about Silverlight and ASP. net (except for the Ajax part that 1.0 can be used )?
What methods can we consider?
Through session? This is not feasible because Silverlight runtime has no processing method.
Querystring? Well, it seems feasible.
Let's take a look:
1.
First, open Visual Studio 2008, and then add a Silverlight example (if you do not know how to add it, please refer to [Silverlight] Hello siverlight ----- Day 1 ).
2.
In page. XAML, set the program parameters as follows (set a textblock and two buttons, one for querystring and the other for querystring ):
<Usercontrol X: class = "sl_test1_site.page"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Width = "400" Height = "300" xmlns: D = "http://schemas.microsoft.com/expression/blend/2008" xmlns: MC = "http://schemas.openxmlformats.org/markup-compatibility/2006" MC: ignorable = "D">
<Grid X: Name = "layoutroot" background = "white">
<Textblock X: Name = "uxtxt" text = "hello" verticalalignment = "TOP" Height = "48" margin = ","/>
<Button X: Name = "uxsetquerystring" Click = "uxsetquerystring_click" content = "set querystring" width = "100" Height = "30" horizontalalignment = "Left" margin = "92,88, 0, 0 "verticalignment =" TOP "D: layoutoverrides =" width, height "/>
<Button X: Name = "uxgetquerystring" Click = "uxgetquerystring_click" content = "Get querystring" width = "100" Height = "30" margin = "," D: layoutoverrides = "width, height" verticalignment = "TOP" horizontalalignment = "right"/>
</GRID>
</Usercontrol>
3.
In the Click Event of the button from which querystring is sent, the following program appears:
Private void uxsetquerystring_click (Object sender, routedeventargs E)
{
System. windows. browser. htmlpage. window. eval ("location = '" + application. current. host. source. absoluteuri. replace (application. current. host. source. absolutepath, "") + "/default. aspx? Id = 100 ';");
}
We can use the system. Windows. browser. htmlpage. Window. Eval method to help us compile JavaScript, and then use JavaScript to redirect the website.
Therefore, if you want to use JavaScript, you can call through the following methods:
System. Windows. browser. htmlpage. Window. eval ("alert ('Hello Silverlight !! ');");
You can also perform the following redirection:
Uri direct = new uri ("http://www.google.com/Default.aspx? Id = 100 ';");
Htmlpage. Window. navigate (direct );
4.
Then we place two buttons in the default. aspx file of the. Web Browser case. One is used to set the querystring and the other is used to receive the querystring.
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "receive querystring"/>
<Asp: button id = "button2" runat = "server" onclick = "button2_click" text = "set querystring"/>
Then, in the button#click event, write the following program example:
Protected void button#click (Object sender, eventargs E)
{
Response. Write ("this is the number of records received from Silverlight:" + this. Request. querystring ["ID"]. tostring ());
}
5.
Press f5.
6.
Similarly, in default. aspx, set another 'click' event:
Protected void button2_click (Object sender, eventargs E)
{
Response. Redirect (request. url. absoluteuri + "? (ID = 999 ");
}
The button click event that Silverlight receives querystring is as follows:
Private void uxgetquerystring_click (Object sender, routedeventargs E)
{
Uxtxt. Text = "ID:" + htmlpage. Document. querystring ["ID"]. tostring ();
}
7.
Press f5.
Through this example, we can see that Silverlight and ASP. NET can pass through querystring for communication.
You can also use cookies to make it accessible (if you are interested, you can watch it ).
Program downloads
Our next meeting !!