轉帖|其它|編輯:郝浩|2011-04-15 11:28:20.000|閱讀 1128 次
概述:在本節中將講述Silverlight和ASP.NET頁面的相互傳參的兩種常用方式:Cookie和QueryString。首先我們新建一個名為 SLConnectASP.NET的Silverlight應用程序,然后在SLConnectASP.NET.web項目中添加一個 Index.aspx的頁面。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
在本節中將講述Silverlight和ASP.NET頁面的相互傳參的兩種常用方式:Cookie和QueryString。首先我們新建一個名為SLConnectASP.NET的Silverlight應用程序,然后在SLConnectASP.NET.web項目中添加一個Index.aspx的頁面。
一、Silverlight和ASPX頁面的QueryString傳參
實現思路:在Silverlight端跳轉到頁面到Index.aspx并且傳遞一個QueryString參數ID,在該Index.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端創建一個Key名為FirCookieKey,Value值為FirCookieValue的Cookie,然后跳轉頁面到Index.aspx頁面,在該頁面顯示出來。在該頁面有一個按鈕可以修改這個Cookie,并且跳轉回Silverlight端,在Silverlight端讀取這個已經被修改過的Cookie并且顯示出來。
首先我們看Silverlight端的后臺代碼MainPage.xaml.cs:包括步驟一創建Cookie、步驟四讀取被修改過的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操作二和Cookie操作三
#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
最后這兩個實例的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>
本實例采用VS2010+Silverlight 4.0編寫。如需源碼請點擊 SLConnectASP.NET.rar 下載。本實例效果圖如下:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:博客園