Silverlight学習ノートのSilverlightとaspxページは互いに値を伝えます


kagula
2012-2-27

学習環境


Win7SP1
VS2010SP1
 

第一種類:GET方式の値伝達


Silverlightソース
 
using System;
using System.Collections.Generic;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows;

namespace SilverlightApplication2
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            ShowQueryString();
        }

        //   aspx  
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            HtmlPage.Window.Eval("location='" + Application.Current.Host.Source.AbsoluteUri.Replace(
                Application.Current.Host.Source.AbsolutePath, "") + "/index.aspx?id=123';");
        }

        //   ASP.NET       QueryString 
        private void ShowQueryString()
        {             
            IDictionary<String, String> paras = HtmlPage.Document.QueryString;
            if (paras.ContainsKey("aspxid"))
            {
                textBox1.Text = "   ASP.NET  :" + paras["aspxid"]; //this.label1.Content =
            }
        }
    }
}

aspxソースコード
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SilverlightApplication2.Web
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindQueryString();
        }

        //ASP.NET    Silverlight     QueryString  
        private void BindQueryString(){             
            if (Request.QueryString.Count > 0) { 
                this.Label1.Text = "    Silverlight   QueryString  :" + Request.QueryString["id"].ToString(); 
            }
        }

        //  QueryString  Silverlight         
        protected void Button1_Click(object sender, EventArgs e){             
            Response.Redirect("./SilverlightApplication2TestPage.aspx?aspxid=321");
        }
    }
}

 

2つ目:Cookie方式の値伝達


Silverlightソース
 
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Browser;

namespace SilverlightApplication3
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            ShowCookie();
        }

        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.Text = "Cookie Key  :" + keyValues[0];            
                    this.textBox1.Text = "Cookie Value  :" + keyValues[1];        
                }    
            };
        }

        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';");
        }
    }
}

aspxソースコード
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SilverlightApplication3.Web
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindCookie();
        }
        
        private void BindCookie(){    
            // ASP.NET    Silverlight     Cookie     
            HttpCookie cookie = Request.Cookies["FirCookieKey"];    
            if (cookie.Value != null)    {        
                this.Label2.Text = "    Silverlight   Cookie :" + cookie.Value;    
            }
        }
        
        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("./SilverlightApplication3TestPage.aspx");
        }
    }
}

 

参考資料


[1]『Silverlight実用的なコツシリーズ:37.SilverlightとASP.NETが互いにパラメータを伝達する2つの一般的な方法(QueryString,Cookie)【インスタンスソースコード付き】
http://www.cnblogs.com/chengxingliang/archive/2011/04/14/2015085.html