asp.net页面间传值的几种方法
来源:互联网 发布:农村淘宝网址是多少 编辑:程序博客网 时间:2024/05/20 19:33
表单提交
传送页面代码
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>表单提交</title>
<script type="text/javascript" language="javascript">
function post()
{
forPost.action="DestinationPage.aspx";
forPost.submit();
}
</script>
</head>
<body>
<form id="forPost" method="post">
方式一:表单提交<br />
<input type="text" id="SourceData2" runat="server"/>
<input type="button" id="btnTransfer1" value="提交" onclick="post();" />
</form>
</body>
</html>
接收页面代码
protected void Page_Load(object sender, EventArgs e)
{
string a = Request.Form["SourceData2"].ToString();
txt1.Value = a;
}
QueryString传值
传送页面代码
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>QueryString</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="txt1" runat="server" />
<input type="text" id="txt2" runat="server" />
<input type="button" id="btn1" value="提交" runat="server" onserverclick="btn1_ServerClick" />
</div>
</form>
</body>
</html>
protected void btn1_ServerClick(object sender, EventArgs e)
{
string aa = txt1.Value;
string bb = txt2.Value;
string url = "DestinationPage5.aspx?parameter1=" + aa + "¶meter2=" + bb;
Response.Redirect(url, false);
}
接收页面代码
protected void Page_Load(object sender, EventArgs e)
{
txt1.Value = Request.QueryString["parameter1"].ToString();
txt2.Value = Request.QueryString["parameter2"].ToString();
}
链接地址传值
传送页面代码
<a href="DestinationPage6.aspx?param1=1111¶m2=2222">跳转</a>
接收页面代码
protected void Page_Load(object sender, EventArgs e)
{
txt1.Value = Request["param1"];
txt2.Value = Request["param2"];
}
Context传值
通过Context传值,在传送页面之前,将需要传递到其他页面的值存在Context中。
传送页面代码
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Context</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="txt1" runat="server" />
<input type="button" id="btn1" value="提交" runat="server" onserverclick="btn1_ServerClick" />
</div>
</form>
</body>
</html>
protected void btn1_ServerClick(object sender, EventArgs e)
{
Context.Items["value"] = txt1.Value;
Server.Transfer("DestinationPage3.aspx");
}
接收页面代码
protected void Page_Load(object sender, EventArgs e)
{
string a = Context.Items["value"].ToString();
txt1.Value = a;
}
Server.Transfer传值
传送页面代码
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Server.Transfer</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="txt1" runat="server" />
<input type="button" id="btn1" value="提交" runat="server" onserverclick="btn1_ServerClick" />
</div>
</form>
</body>
</html>
protected void btn1_ServerClick(object sender, EventArgs e)
{
Server.Transfer("DestinationPage4.aspx");
}
public string Value
{
get { return txt1.Value; }
}
接收页面代码
public partial class Transfer_DestinationPage4 : System.Web.UI.Page
{
private Transfer_SourcePage4 sourcePage;
protected void Page_Load(object sender, EventArgs e)
{
sourcePage = (Transfer_SourcePage4)Context.Handler;
string aa = sourcePage.Value;
txt1.Value = aa;
}
}
Cookie传值
传送页面代码
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Cookie</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="txt1" runat="server" />
<input type="button" id="btn1" value="提交" runat="server" onserverclick="btn1_ServerClick" />
</div>
</form>
</body>
</html>
protected void btn1_ServerClick(object sender, EventArgs e)
{
string aa = txt1.Value;
HttpCookie cookie = new HttpCookie("MyCookie", aa);
Response.Cookies.Add(cookie);
string url = "DestinationPage8.aspx";
Response.Redirect(url, false);
}
接收页面代码
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie myCookie = Request.Cookies["MyCookie"];
txt1.Value = myCookie.Value;
}
Session传值
通过Session取值,在一个页面中赋值,在其他页面中共享。为避免造成Session值的混乱无序,应尽量少用Session传非公共的变量。Session比较适合用来保存一些公共变量。
传送页面代码
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Session传值</title>
</head>
<body>
<form runat="server">
<input type="text" id="txt1" runat="server" />
<input type="button" id="btn1" value="提交" runat="server" onserverclick="btn1_ServerClick" />
</form>
</body>
</html>
protected void btn1_ServerClick(object sender, EventArgs e)
{
Session["value"] = txt1.Value;
Response.Redirect("DestinationPage2.aspx");
}
接收页面代码
protected void Page_Load(object sender, EventArgs e)
{
txt1.Value = Session["value"].ToString();
}
Application传值
此种方法不常使用,因为Application在一个应用程序域范围共享,所有用户可以改变及设置其值,故只应用于计数器等需要全局变量的地方。
传送页面代码
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Application</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="txt1" runat="server" />
<input type="button" id="btn1" value="提交" runat="server" onserverclick="btn1_ServerClick" />
</div>
</form>
</body>
</html>
protected void btn1_ServerClick(object sender, EventArgs e)
{
string aa = txt1.Value;
Application["param1"] = aa;
string url = "DestinationPage7.aspx";
Response.Redirect(url, false);
}
接收页面代码
protected void Page_Load(object sender, EventArgs e)
{
txt1.Value = Application["param1"].ToString();
}
http://www.cnblogs.com/xiaoyusmile/archive/2012/03/20/2408797.html
阅读全文
0 0
- asp.net 页面间传值的几种方法
- asp.net Context.Handler 页面间传值的几种方法
- ASP.NET 页面间传值的几种方法
- asp.net页面间传值的几种方法
- ASP.NET 页面间传值的几种方法
- asp.net页面间传值的几种方法
- Asp.Net页面的传值的几种方法
- Asp.net 页面导航的几种方法与比较
- Asp.net 页面导航的几种方法与比较
- Asp.net 页面导航的几种方法与比较
- Asp.net页面之间传递参数的几种方法
- Asp.net页面之间传递参数的几种方法
- Asp.net 页面导航的几种方法与比较
- ASP.NET页面传值的几种方法
- Asp.net页面之间传递参数的几种方法
- Asp.net页面之间传递参数的几种方法
- Asp.net 页面导航的几种方法与比较
- ASP.NET页面跳转的几种方法
- wait和notify的理解与使用
- 关于MediaRecorder中的setAudioEncoder和setOutputFormat
- linux 解压缩 压缩命令 备忘
- 人工智能必学知识
- 第十四章 代码重用
- asp.net页面间传值的几种方法
- 软件测试--Bug等级划分方法
- linux虚拟机桌面化
- 手把手教你使用U盘安装Ubuntu系统
- 每周LeetCode算法题(十一) 题目: 322. Coin Change
- David Silver深度强化学习第1课
- 电商网站搭建架构图
- 读书-思考力|《金字塔原理》
- spring boot配置文件:application.yml (yml与properties文件优缺对比)