asp.net页面传值的五种方法:QueryString,Session,Application,Request.Cookies,Server.Transfer
其中Cookie和Server.Transfer两种方式不同于前面三种,Server.Transfer充分体现了面向对象思想。

首先新建webForm项目,然后添加WebForm1和WebForm2页面。

WebForm1.aspx页面客户端对象代码如下
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server" method="POST" action="WebForm1.aspx">
<label>用户名:</label>
<input type="text" id="UserName" name="UserName"/><br/>
<label>密码:</label>
<input type="text" id="Pwd" name="Pwd"/><br/>
<input type="submit" />
</form>
</body>
</html>
WebForm1.aspx.cs代码如下
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
 
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
string user;
string pwd;
public string UserName
{
get { return this.user; }
}
 
public string Pwd
{
get { return this.pwd; }
}
 
protected void Page_Load(object sender, EventArgs e)
{
//1.获取表单提交数据,使用QueryString获取提交过来的值,发送出去
#region
//if (IsPostBack)
//{
// string user = Request.Form["UserName"];
// string pwd = Request.Form["Pwd"];
// Response.Redirect("WebForm2.aspx?UserName=" + user + "&Pwd=" + pwd);
//}
#endregion
 
//2.获取表单提交数据,使用Session获取提交过来的值,发送出去
#region
//if (IsPostBack)
//{
// string user = Request.Form["UserName"];
// string pwd = Request.Form["Pwd"];
// //string user = Request["UserName"];
// //Response.Write("<script>alert('" + user + pwd + "')</script>");
// if (!string.IsNullOrEmpty(user))
// {
// Session["UserName"] = user;
// Session["Pwd"] = pwd;
// Response.Redirect("WebForm2.aspx");
// }
//}
#endregion
 
//3.获取表单提交数据,使用Application获取提交过来的值,发送出去
#region
//if (IsPostBack)
//{
// string user = Request.Form["UserName"];
// string pwd = Request.Form["Pwd"];
////string user = Request["UserName"];
////Response.Write("<script>alert('" + user + pwd + "')</script>");
// if (!string.IsNullOrEmpty(user))
// {
// Application["UserName"] = user;
// Application["Pwd"] = pwd;
// Response.Redirect("WebForm2.aspx");
// }
//}
#endregion
 
//4.获取表单提交数据,使用Server.Transfer获取提交过来的值,发送出去
#region
//if (IsPostBack)
//{
// user = Request.Form["UserName"];
// pwd = Request.Form["Pwd"];
// //string user = Request["UserName"];
// //Response.Write("<script>alert('" + user + pwd + "')</script>");
// if (!string.IsNullOrEmpty(user))
// {
// Server.Transfer("WebForm2.aspx");
// }
//}
#endregion
 
//5.获取表单提交数据,使用HttpCookie获取提交过来的值,发送出去
#region
//if (IsPostBack)
//{
// string user = Request.Form["UserName"];
// string pwd = Request.Form["Pwd"];
// HttpCookie cookieName = new HttpCookie("UserName", user);
// HttpCookie cookiePwd = new HttpCookie("Pwd", pwd);
// Response.AppendCookie(cookieName);
// Response.AppendCookie(cookiePwd);
// Server.Transfer("WebForm2.aspx");
//}
#endregion
}
}
}
WebForm2.aspx客户端无需修改,WebForm2.aspx.cs代码如下
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
 
namespace WebApplication1
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//1.QueryString获取值
#region
//string i = Request.QueryString["UserName"] + Request.QueryString["Pwd"]; ;
//Response.Write("<script>alert('" + i + "')</script>");
#endregion
 
//2.Session获取值
#region
//string info = Session["UserName"].ToString() + Session["Pwd"].ToString();
//Response.Write("<script>alert('" + info + "')</script>");
#endregion
 
//3.Application获取值
#region
//string infos = Application["UserName"].ToString() + Application["Pwd"].ToString();
//Response.Write("<script>alert('" + infos + "')</script>");
#endregion
 
//4.Server.Transfer获取值
#region
//WebForm1 valuePage;
//valuePage = (WebForm1)Context.Handler;
//string a = valuePage.UserName + valuePage.Pwd;
//Response.Write("<script>alert('" + a + "')</script>");
#endregion
 
//5.Request.Cookies获取值(不同点是:①Cookies是存在于Request中的,②注意要加Cookies[]之后不要忘记加上.Value)
#region
//string c = Request.Cookies["UserName"].Value.ToString() + Request.Cookies["Pwd"].Value.ToString();
//Response.Write("<script>alert('"+c+"')</script>");
#endregion
}
}
}

 

ASP.NET页面传值与跳转的更多相关文章

  1. ASP.NET页面传值的几种方式

    页面传值是学习asp.net初期都会面临的一个问题,总的来说有页面传值. 存储对象传值.ajax.类.model.表单等!下面欧柏泰克和大家一起来看看asp.net页面传值方式一般有哪些?常用的较简单 ...

  2. ASP.NET页面传值不使用QueryString

    ASP.NET页面传值不使用QueryString   Asp.net中的页面传值方法: 1         Url传值 特点:主要优点是实现起来非常简单,然而它的缺点是传递的值是会显示在浏览器的地址 ...

  3. ASP.Net页面传值比较

    ASP.Net页面传值比较   作为一个ASP.Net程序员,尤其是搞B/S开发的,对于不同页面之间变量值的传递用的非常广泛,而掌握不同方式之间的区别和特点也就很有必要.本文将针对这一知识点做一个简单 ...

  4. Asp.net 页面传值的方法

    ASP.NET页面传值的方法 From:Refresh-air 在面试的时候,经常会遇到这样的问题,其实我们会对其中的几种方法比较熟悉,因为项目中经常使用.但是要全面的回答ASP.NET中页面传值的方 ...

  5. ASP.NET页面传值的方法

    ASP.NET页面传值的方法 From:Refresh-air 在面试的时候,经常会遇到这样的问题,其实我们会对其中的几种方法比较熟悉,因为项目中经常使用.但是要全面的回答ASP.NET中页面传值的方 ...

  6. net面试 ASP.NET页面传值的各种方法和分析 (copy)

    Web页面是无状态的, 服务器对每一次请求都认为来自不同用户,因此,变量的状态在连续对同一页面的多次请求之间或在页面跳转时不会被保留.在用ASP.NET 设计开发一个Web系统时, 遇到一个重要的问题 ...

  7. C/S ASP.NET页面传值汇总

    一. QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏中.如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法.但是对于传递数组或对象的话,就不 ...

  8. asp.net页面之间的跳转

    调用Request.CurrentExecutionFilePath方法返回到当前页面 站点中常常要跳转页面,调用Request.CurrentExecutionFilePath方法能够获取当前页面的 ...

  9. asp.net页面传值方法汇总

    1. Get(即使用QueryString显式传递)     方式:在url后面跟参数.     特点:简单.方便.     缺点:字符串长度最长为255个字符:数据泄漏在url中.     适用数据 ...

随机推荐

  1. cf B. Making Sequences is Fun

    http://codeforces.com/contest/373/problem/B 用二分枚举长度就可以. #include <cstdio> #include <cstring ...

  2. (转载)Python 列表(list)操作

    (转载)http://blog.csdn.net/facevoid/article/details/5338048 创建列表sample_list = ['a',1,('a','b')] Python ...

  3. 【模拟】XMU 1599 斐波那契汤

    题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1599 题目大意: 给k,m,q以及f[1]...f[k],当n<m时,f[n]= ...

  4. mysql 安装截图

    这里有3个选项, 1.Developer Machine(开发机器),个人用桌面工作站,占用最少的系统资源 2.Server Machine(服务器),MySQL服务器可以同其它应用程序一起运行,例如 ...

  5. 谈谈托管代码、IL、CLR、ISAPI?

    什么是托管代码?       托管代码是可以使用20多种支持Microsoft .NET Framework的高级语言编写的代码,这些语言包括:C#, J#, Microsoft Visual Bas ...

  6. IBM Intel 微软

    IBM是全球IT第一巨头,也是一个很奇特也很强大强大的公司,从螺丝钉键盘鼠标到CPU硬盘内存到大型机巨型机,它都可以制造,从软件到硬件到服务,它都可以提供,这在IT历史上,是否绝后我不敢说,空前应该是 ...

  7. Jsp 中文乱码,解决

    jsp 乱码 : The time on the server is 2016?2?7? ??10?45?32?. 在 jsp 中,用 jsp 语法添加 utf-8 字符集,可解决此问题 <%@ ...

  8. Gradle构建Java Web应用(转)

    转自:http://www.blogjava.net/jiangshachina/archive/2014/02/03/409285.html 本文是发布在java.net上的一篇摘自于<Gra ...

  9. C++ —— 笔记汇总

    导读 本文仅用于记录在个人在使用C++过程中的遇到一些的疑问和概念. 目录 语法和概念基础 常用函数 编程注意 编译问题 拓展链接 1.语法和概念基础 1.块域     2.static 作用域    ...

  10. SpringMVC框架图解析

    Spring框架提供了构造Web应用程序的全能MVC模块.Spring MVC分离了控制器.模型对象.分派器以及处理程序对象的角色,这种分离让它们更容易进行制定.是一个标准的MVC框架. 那你猜一猜哪 ...