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. UART接口

    1.UART UART(Universal Asynchronous Receiver and Transmitter)通用异步收发器(异步串行通信口),是一种通用的数据通信协议,它包括了RS232. ...

  2. 用于 Visual Studio 和 ASP.NET 的 Web 应用程序项目部署常见问题

    https://msdn.microsoft.com/zh-cn/library/ee942158(v=vs.110).aspx#can_i_exclude_specific_files_or_fol ...

  3. 运行出现 Multiple dex files define Landroid/support/annotation/AnimRes 解决方法

    1. 拷贝google-play-services_lib/libs/目录下的android-support-v4.jar到xxxr/libs/下 2. 工程进行如下设置:

  4. Path Sum II——LeetCode

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  5. java--I/O流总结

    java中的I/O流从所操作的数据单元上分为两类:字节流和字符流. 1.字节流 字节流主要有两个抽象基类:InputStream和OutputStream:其中InputStream是输入字节流,Ou ...

  6. Diamond Armor - The most expensive Suit: 2.8 Mio Swiss Francs

    Diamond Armor - The most expensive Suit: 2.8 Mio Swiss Francs Diamond Armor

  7. QT5 TK1 串口通信

    对TK1中基于QT5的串口通信过程进行总结.按照软件安装及通信实现的顺序. 1.QT5安装 较简洁方式:打开软件中心(类似A形),搜索qtcreator,点击安装即可. 2.串口通信库安装 采用上述方 ...

  8. USACO lamps

    IOI 98称号.然后,它似乎没有很困难.即使我能做到这一点微弱的残留物.所有的button按两次不按,高达因此实际上总的等效按4二级,首先C往下<=4,则搜索将能直接照射,总共只有16状态(事 ...

  9. $.each 和$(selector).each()的差别

    Home » jQuery » $.each() $.each() Posted on 2012 年 3 月 15 日 in jQuery, jQuery函数 | by Jason | 译自官方手冊: ...

  10. DecimalFormat用法

    DecimalFormat用法   DecimalFormat 是 NumberFormat 的一个具体子类,用于格式化十进制数字. DecimalFormat 包含一个模式 和一组符号 符号含义: ...