博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Silverlight实用窍门系列:37.Silverlight和ASP.NET相互传参的两种常用方式(QueryString,Cookie)【附带实例源码】...
阅读量:7219 次
发布时间:2019-06-29

本文共 4936 字,大约阅读时间需要 16 分钟。

 在本节中将讲述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编写。如需源码请点击  下载。本实例效果图如下:

2011041317211622.jpg2011041317212319.jpg

本文转自程兴亮博客园博客,原文链接:http://www.cnblogs.com/chengxingliang/archive/2011/04/14/2015085.html,如需转载请自行联系原作者

你可能感兴趣的文章
[ZJOI2009]假期的宿舍
查看>>
Lingo 做线性规划 - Game Thoery
查看>>
Eight-内置函数和匿名函数
查看>>
Android 代码中使用Color工具类 parseColor
查看>>
冒泡思路
查看>>
核桃说笔记
查看>>
查询某个字段所在的表名
查看>>
微信jsapi退款操作
查看>>
压测出现各种奇葩问题,求围观
查看>>
团队-象棋游戏-开发环境搭建过程
查看>>
C#:SqlHelper
查看>>
MIT自然语言处理第五讲:最大熵和对数线性模型(第二部分)
查看>>
UVa10131
查看>>
Kakfa揭秘 Day2 Kafka内核再揭秘
查看>>
jeecg入门操作—字典配置
查看>>
centos安装php5.6
查看>>
ios上取得设备唯一标志的解决方案
查看>>
iOS下日期的处理
查看>>
Java多线程总结(二)锁、线程池
查看>>
使用ThinkPHP实现生成缩略图及显示
查看>>