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. LeetCode_ 4 sum

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  2. SET NOCOUNT (Transact-SQL)

    阻止在结果集中返回显示受 Transact-SQL 语句或存储过程影响的行计数的消息. 语法 SET NOCOUNT { ON | OFF } 注释  当 SET NOCOUNT 为 ON 时,不返回 ...

  3. 黑魔法__attribute__((cleanup))

    原文地址:http://blog.sunnyxx.com/2014/09/15/objc-attribute-cleanup/ 编译器属性__attribute__用于向编译器描述特殊的标识.检查或优 ...

  4. 3D视频可能出现的质量问题 (MSU出品)

    俄罗斯的MSU Graphics & Media Lab (Video Group)提出的3D视频存在的几种问题.有一定的参考价值,在此翻译一下. 他们目前总结出4种问题: 水平视差(Hori ...

  5. (Relax 水题1.2)POJ 1032 Parliament(将n分解成若干个互不相等的整数的和,并且是这些整数的乘积最大)

    题意:给出一个数n,将其拆分为若干个互不相等的数字的和,要求这些数字的乘积最大. 分析:我们可以发现任何一个数字,只要能拆分成两个大于1的数字之和,那么这两个数字的乘积一定大于等于原数.也就是说,对于 ...

  6. Oralce新建数据库、新建远程登录用户全过程

    Oracle安装完后,其中有一个缺省的数据库,除了这个缺省的数据库外,我们还可以创建自己的数据库.     对于初学者来说,为了避免麻烦,可以用'Database Configuration Assi ...

  7. AutoCompleteTextView ArrayAdapter

    AutoCompleteTextView   继承于EditText,拥有EditText所有属性和方法 在输入框中输入我们想要输入的信息就会出现其他与其相关的提示信息 首先在activity_mai ...

  8. 如何在Ubuntu 12.04 Server 中安装图形用户界面

    root@ubuntu:~# gedit /etc/environment root@ubuntu:~# gedit /etc/profile ---------------------------- ...

  9. lvs keepalived 安装配置详解【转】

    lvs keepalived 安装配置详解 张映 发表于 2012-06-20 分类目录: 服务器相关 前段时间看了一篇文章,lvs做负载均衡根F5差不多,说实话不怎么相信,因为F5没玩过,也无法比较 ...

  10. [转] 使用Spring Boot和Gradle创建项目

    Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的 ...