Silve37.SilverlightとASP.NET相互参照の2つの一般的な方法(QueryString,Cookie)
この節ではSilverlightとASPについて述べる.NETページの相互参照の2つの一般的な方法:CookieとQueryString.まずSLConnectASPという名前を新規作成します.NETのSilverlightアプリ、そしてSLConnectASP.NET.WebプロジェクトにIndexを追加します.aspxのページ.
一、SilverlightとASPXページのQueryString伝参
実現構想:Silverlight端でページにジャンプしてIndexに移動する.Aspxは、IndexにおけるQueryStringパラメータIDを渡す.aspxページにIDが表示されます.Indexでaspxページには、Silverlightのページにパラメータaspxidを渡し、Silverlight側でaspxidパラメータを読み出して表示するボタンがあります.
まずSilverlightバックグラウンドコードを見てみましょう.
そしてIndexを見てみましょうaspx.csのコードは以下の通りです.
二、SilverlightとASPXページのCookie伝参
実装構想:Silverlight側にFirCookieKeyというKeyを作成し、Value値がFirCookieValueのCookieを作成し、Indexにページをジャンプする.aspxページは、このページに表示されます.このページには、このCookieを修正し、Silverlight側にジャンプし、Silverlight側で修正されたCookieを読み取り、表示するボタンがあります.
まずSilverlight側のバックグラウンドコードMainPageを見てみましょう.xaml.cs:ステップ1でCookieを作成し、ステップ4で修正されたCookieを読み出す
次にIndexを見てみましょうaspx.cs、Cookie操作2とCookie操作3を含む
最後の2つの例のMainPage.xamlのコードは次のとおりです.
Index.aspxのコードは以下の通りです.
この例はVS 2010+Silverlight 4.0で記述されています.ソースコードが必要な場合はSLConnectASPをクリックしてください.NET.rarダウンロード.この例の効果図は次のとおりです.
一、SilverlightとASPXページのQueryString伝参
実現構想:Silverlight端でページにジャンプしてIndexに移動する.Aspxは、IndexにおけるQueryStringパラメータIDを渡す.aspxページにIDが表示されます.Indexでaspxページには、Silverlightのページにパラメータaspxidを渡し、Silverlight側でaspxidパラメータを読み出して表示するボタンがあります.
まずSilverlightバックグラウンドコードを見てみましょう.
- #region QueryString
- //QueryString
- private void button2_Click(object sender, RoutedEventArgs e)
- {
- // Asp.net
- HtmlPage.Window.Eval("location='" +
- Application.Current.Host.Source.AbsoluteUri.Replace(
- Application.Current.Host.Source.AbsolutePath, "") + "/index.aspx?id=203';");
- }
- //QueryString
- private void ShowQueryString()
- {
- // ASP.NET QueryString
- IDictionary<String, String> paras = HtmlPage.Document.QueryString;
- if (paras.ContainsKey("aspxid"))
- {
- this.label1.Content = " ASP.NET :" + paras["aspxid"];
- }
- }
- #endregion
そしてIndexを見てみましょうaspx.csのコードは以下の通りです.
- #region ASP.NET Silverlight QueryString
- //QueryString
- private void BindQueryString()
- {
- //ASP.NET Silverlight QueryString
- if (Request.QueryString.Count > 0)
- {
- this.Label1.Text = " Silverlight QueryString :"
- + Request.QueryString["id"].ToString();
- }
- }
- //QueryString
- protected void Button1_Click(object sender, EventArgs e)
- {
- // QueryString Silverlight
- Response.Redirect("./SLConnectASP.NETTestPage.aspx?aspxid=109");
- }
- #endregion
二、SilverlightとASPXページのCookie伝参
実装構想:Silverlight側にFirCookieKeyというKeyを作成し、Value値がFirCookieValueのCookieを作成し、Indexにページをジャンプする.aspxページは、このページに表示されます.このページには、このCookieを修正し、Silverlight側にジャンプし、Silverlight側で修正されたCookieを読み取り、表示するボタンがあります.
まずSilverlight側のバックグラウンドコードMainPageを見てみましょう.xaml.cs:ステップ1でCookieを作成し、ステップ4で修正されたCookieを読み出す
- #region Cookie
- //Cookie
- private void button1_Click(object sender, RoutedEventArgs e)
- {
-
- // FirCookieKey, FirCookieValue Cookie
- string oldCookie = HtmlPage.Document.GetProperty("cookie") as String;
- DateTime expiration = DateTime.UtcNow + TimeSpan.FromDays(2000);
- string cookie = String.Format("{0}={1};expires={2}", "FirCookieKey",
- "FirCookieValue", expiration.ToString("R"));
- HtmlPage.Document.SetProperty("cookie", cookie);
- //
- HtmlPage.Window.Eval("location='" +
- Application.Current.Host.Source.AbsoluteUri.Replace(
- Application.Current.Host.Source.AbsolutePath, "") + "/index.aspx';");
-
- }
- //Cookie
- private void ShowCookie()
- {
- // FirCookieKey Cookie
- String[] cookies = HtmlPage.Document.Cookies.Split(';');
- foreach (String cookie1 in cookies)
- {
- String[] keyValues = cookie1.Split('=');
- if (keyValues[0] == "FirCookieKey")
- {
- this.textBox2.Content = "Cookie Key :" + keyValues[0];
- this.textBox1.Content = "Cookie Value :" + keyValues[1];
-
- }
- };
- }
- #endregion
次にIndexを見てみましょうaspx.cs、Cookie操作2とCookie操作3を含む
- #region ASP.NET Cookie
- //Cookie
- private void BindCookie()
- {
- // ASP.NET Silverlight Cookie
- HttpCookie cookie = Request.Cookies["FirCookieKey"];
- if (cookie.Value != null)
- {
- this.Label2.Text = " Silverlight Cookie :" + cookie.Value;
- }
-
- }
- //Cookie
- protected void Button2_Click(object sender, EventArgs e)
- {
- // Cookie Silverlight
- HttpCookie cookie = Request.Cookies["FirCookieKey"];
- cookie.Value = "NewCookieValue";
- HttpContext.Current.Response.Cookies.Add(cookie);
- Response.Redirect("./SLConnectASP.NETTestPage.aspx");
- }
- #endregion
最後の2つの例のMainPage.xamlのコードは次のとおりです.
- <Grid x:Name="LayoutRoot" Background="White">
- <sdk:Label Height="27" HorizontalAlignment="Left" Margin="41,25,0,0"
- Name="label1" VerticalAlignment="Top" Width="284" />
- <Button Content=" " Height="38" HorizontalAlignment="Left"
- Margin="41,63,0,0" Name="button2" VerticalAlignment="Top"
- Width="113" Click="button2_Click" />
-
- <sdk:Label Height="27" HorizontalAlignment="Left" Margin="41,116,0,0"
- Name="textBox2" VerticalAlignment="Top" Width="284" />
- <sdk:Label Height="27" HorizontalAlignment="Left" Margin="41,149,0,0"
- Name="textBox1" VerticalAlignment="Top" Width="284" />
- <Button Content=" Cookie " Height="38" HorizontalAlignment="Left"
- Margin="41,194,0,0" Name="button1" VerticalAlignment="Top"
- Width="113" Click="button1_Click" />
- </Grid>
Index.aspxのコードは以下の通りです.
- <div>
-
- <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
- <br />
- <br />
- <asp:Button ID="Button1" runat="server" Text=" Silverlight QueryString"
- onclick="Button1_Click" />
-
- <br />
- <br />
-
- <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
- <br />
- <asp:Button ID="Button2" runat="server"
- Text=" Silverlight Cookie" onclick="Button2_Click"
- />
-
- <br />
-
- </div>
この例はVS 2010+Silverlight 4.0で記述されています.ソースコードが必要な場合はSLConnectASPをクリックしてください.NET.rarダウンロード.この例の効果図は次のとおりです.