ASP.NET页面传值与跳转

Posted SherlockHolmes

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ASP.NET页面传值与跳转相关的知识,希望对你有一定的参考价值。

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页面传值与跳转的主要内容,如果未能解决你的问题,请参考以下文章

请问asp.net如何将整个页面或部分页面导出到word?并下载到本地?

ASP.NET session 用户长时间无操作,跳转到重新登录页面,具体怎么写,求高手帮忙啊!

asp.net web页面导出word

Kotlin返回与跳转

asp.net,连接跳转页面时怎么获取当前页面的id

PHP页面提示与跳转