ASP.NET页面传值与跳转
2016-03-17 13:08
681 查看
asp.net页面传值的五种方法:QueryString,Session,Application,Request.Cookies,Server.Transfer
其中Cookie和Server.Transfer两种方式不同于前面三种,Server.Transfer充分体现了面向对象思想。
首先新建webForm项目,然后添加WebForm1和WebForm2页面。
WebForm1.aspx页面客户端对象代码如下
WebForm1.aspx.cs代码如下
WebForm2.aspx客户端无需修改,WebForm2.aspx.cs代码如下
其中Cookie和Server.Transfer两种方式不同于前面三种,Server.Transfer充分体现了面向对象思想。
首先新建webForm项目,然后添加WebForm1和WebForm2页面。
WebForm1.aspx页面客户端对象代码如下
<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="WebForm1.aspx.cs"Inherits="WebApplication1.WebForm1"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>无标题页</title>
</head>
<body>
<formid="form1"runat="server"method="POST"action="WebForm1.aspx">
<label>用户名:</label>
<inputtype="text"id="UserName"name="UserName"/><br/>
<label>密码:</label>
<inputtype="text"id="Pwd"name="Pwd"/><br/>
<inputtype="submit"/>
</form>
</body>
</html>
WebForm1.aspx.cs代码如下
usingSystem;
usingSystem.Collections;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Xml.Linq;
namespaceWebApplication1
{
publicpartialclassWebForm1:System.Web.UI.Page
{
stringuser;
stringpwd;
publicstringUserName
{
get{returnthis.user;}
}
publicstringPwd
{
get{returnthis.pwd;}
}
protectedvoidPage_Load(objectsender,EventArgse)
{
//1.获取表单提交数据,使用QueryString获取提交过来的值,发送出去
#region
//if(IsPostBack)
//{
//stringuser=Request.Form["UserName"];
//stringpwd=Request.Form["Pwd"];
//Response.Redirect("WebForm2.aspx?UserName="+user+"&Pwd="+pwd);
//}
#endregion
//2.获取表单提交数据,使用Session获取提交过来的值,发送出去
#region
//if(IsPostBack)
//{
//stringuser=Request.Form["UserName"];
//stringpwd=Request.Form["Pwd"];
////stringuser=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)
//{
//stringuser=Request.Form["UserName"];
//stringpwd=Request.Form["Pwd"];
////stringuser=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"];
////stringuser=Request["UserName"];
////Response.Write("<script>alert('"+user+pwd+"')</script>");
//if(!string.IsNullOrEmpty(user))
//{
//Server.Transfer("WebForm2.aspx");
//}
//}
#endregion
//5.获取表单提交数据,使用HttpCookie获取提交过来的值,发送出去
#region
//if(IsPostBack)
//{
//stringuser=Request.Form["UserName"];
//stringpwd=Request.Form["Pwd"];
//HttpCookiecookieName=newHttpCookie("UserName",user);
//HttpCookiecookiePwd=newHttpCookie("Pwd",pwd);
//Response.AppendCookie(cookieName);
//Response.AppendCookie(cookiePwd);
//Server.Transfer("WebForm2.aspx");
//}
#endregion
}
}
}
WebForm2.aspx客户端无需修改,WebForm2.aspx.cs代码如下
usingSystem;
usingSystem.Collections;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Xml.Linq;
namespaceWebApplication1
{
publicpartialclassWebForm2:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
//1.QueryString获取值
#region
//stringi=Request.QueryString["UserName"]+Request.QueryString["Pwd"];;
//Response.Write("<script>alert('"+i+"')</script>");
#endregion
//2.Session获取值
#region
//stringinfo=Session["UserName"].ToString()+Session["Pwd"].ToString();
//Response.Write("<script>alert('"+info+"')</script>");
#endregion
//3.Application获取值
#region
//stringinfos=Application["UserName"].ToString()+Application["Pwd"].ToString();
//Response.Write("<script>alert('"+infos+"')</script>");
#endregion
//4.Server.Transfer获取值
#region
//WebForm1valuePage;
//valuePage=(WebForm1)Context.Handler;
//stringa=valuePage.UserName+valuePage.Pwd;
//Response.Write("<script>alert('"+a+"')</script>");
#endregion
//5.Request.Cookies获取值(不同点是:①Cookies是存在于Request中的,②注意要加Cookies[]之后不要忘记加上.Value)
#region
//stringc=Request.Cookies["UserName"].Value.ToString()+Request.Cookies["Pwd"].Value.ToString();
//Response.Write("<script>alert('"+c+"')</script>");
#endregion
}
}
}
相关文章推荐
- asp.net学习之Repeater控件
- DoingOrder.aspx.cs缓存的使用方法
- 关于asp输出json对象的方法及实例
- Ubuntu 安装 CLI 并运行 ASP.NET Core 1.0
- ASP.NET MVC异常处理模块详解
- ASP.NET MVC 获得 view 中的HTML并将其中的内容自动转换成繁体中文。
- 2016-AspNet-MVC教学-1-算术加法提交及超链接测试
- Asp.net 防刷新页面弹框,一般都用这个弹框
- ASP.NET web.config中的代码
- asp.net Excel导出功能
- 一个简单的ASP.NET MVC异常处理模块
- MVC4 razor与aspx的区别以及用法1,2
- asp、php、asp.net、jsp介绍及优缺点比较
- ASP.NET(c#)生成条形码
- asp.net异步页面
- ASP.NET Core和Angular 2双剑合璧
- ASP.NET MVC异常处理模块详解
- asp.net小数点四舍五入的正确写法
- UIImageView设置UIViewContentModeScaleAspectFill 图片平铺展开
- 2016-AspNet-MVC教学-2-数据来源及超链接测试