asp.net页面传值测试实例代码(前后台)
程序员文章站
2024-02-29 16:14:22
webform_1.aspx内容如下: 复制代码 代码如下: <%@ page language="c#" autoeventwireup="true" codebe...
webform_1.aspx内容如下:
<%@ page language="c#" autoeventwireup="true" codebehind="webform_1.aspx.cs" inherits="页面传值.webform_1" %>
<!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">
<div>
<asp:table id="tablelogin" runat='server'>
<asp:tablerow>
<asp:tablecell><label>用户名:</label></asp:tablecell>
<asp:tablecell><asp:textbox id="username" runat="server" width="150px"></asp:textbox></asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell><label>密码:</label></asp:tablecell>
<asp:tablecell><asp:textbox id="password" runat="server" width="150px"></asp:textbox></asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell><label>验证密码:</label></asp:tablecell>
<asp:tablecell><asp:textbox id="confimpwd" runat="server" width="150px"></asp:textbox></asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell><asp:button id="confirm" runat="server" text="确认" width="50px" onclick="confirm_click" /></asp:tablecell>
</asp:tablerow>
</asp:table>
</div>
</form>
</body>
</html>
webform_2.aspx页面如下:
<%@ reference page="~/webform_1.aspx" %>
<%@ page language="c#" autoeventwireup="true" codebehind="webform_2.aspx.cs" inherits="页面传值.webform_2" %>
<!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">
<div>
</div>
</form>
</body>
</html>
webform_1.aspx.cs文件如下:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
namespace 页面传值
{
public partial class webform_1 : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
}
public string un//得到用户名
{
get
{
return username.text;
}
}
public string pwd//得到密码
{
get
{
return password.text;
}
}
public string conpwd//得到确认密码
{
get
{
return confimpwd.text;
}
}
/// <summary>
/// 向webform_2.aspx页面传值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void confirm_click(object sender, eventargs e)
{
//1:querystring页面传值
//string url = "webform_2.aspx?un=" + username.text + "&userpassword=" + password.text + "&conpwd=" + confimpwd.text;
//response.redirect(url);
//2:session传值
//session["un"] = username.text;
//session["pwd"] = password.text;
//session["conpwd"] = confimpwd.text;
//server.transfer("webform_2.aspx");
//3:使用cookie对象传值
//httpcookie cookie_name = new httpcookie("un");
//cookie_name.value = username.text;
//httpcookie cookie_pwd = new httpcookie("pwd");
//cookie_pwd.value = password.text;
//httpcookie cookie_conpwd = new httpcookie("conpwd");
//cookie_conpwd.value = confimpwd.text;
//response.appendcookie(cookie_name);
//response.appendcookie(cookie_pwd);
//response.appendcookie(cookie_conpwd);
//server.transfer("webform_2.aspx");
//4:使用application对象传值,类似session传值,作用范围全局所有用户
//application["un"] = username.text;
//application["pwd"] = password.text;
//application["conpwd"] = confimpwd.text;
//response.redirect("webform_2.aspx");
server.transfer("webform_2.aspx");
}
}
}
webform_2.aspx.cs文件如下:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
namespace 页面传值
{
public partial class webform_2 : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
//querytransfer();
//sessiontransfer();
//cookietransfer();
//applicationtransfer();
transfer();
}
public void querytransfer()//接收querystring传值,来自于webform_1页面的值
{
string strusername = request.querystring["un"].tostring();
string strpassword = request.querystring["userpassword"].tostring();
string strpwd = request.querystring["conpwd"].tostring();
response.write("用户名为" + strusername + "<br/>" + "密码为" + strpassword + "<br/>" + "确认密码为" + strpwd);
}
public void sessiontransfer()//接收session传值,来自于webform_1页面的值
{
string strusername = session["un"].tostring();
string strpassword = session["pwd"].tostring();
string strpwd = session["conpwd"].tostring();
response.write("用户名为" + strusername + "<br/>" + "密码为" + strpassword + "<br/>" + "确认密码为" + strpwd);
session.remove("un");
session.remove("pwd");
session.remove("conpwd");
}
public void cookietransfer()//接收cookie传值,来自于webform_1页面的值
{
string strusername = request.cookies["un"].value.tostring();
string strpassword = request.cookies["pwd"].value.tostring();
string strpwd = request.cookies["conpwd"].value.tostring();
response.write("用户名为" + strusername + "<br/>" + "密码为" + strpassword + "<br/>" + "确认密码为" + strpwd);
}
public void applicationtransfer()//接收application传值,来自于webform_1页面的值
{
application.lock();
string strusername = application["un"].tostring();
string strpassword = application["pwd"].tostring();
string strpwd = application["conpwd"].tostring();
application.unlock();
if (strpassword != strpwd)
{
response.write("您确认的密码错误,请重新输入!<br/>");
server.transfer("webform_1.aspx");
}
response.write("用户名为" + strusername + "<br/>" + "密码为" + strpassword + "<br/>" + "确认密码为" + strpwd);
}
public void transfer()//transfer传值,来自webform_1.aspx页面的值
{
webform_1 wf1;
wf1 = (webform_1)context.handler;
string strusername = wf1.un;
string strpassword = wf1.pwd;
string strpwd = wf1.conpwd;
response.write("用户名为" + strusername + "<br/>" + "密码为" + strpassword + "<br/>" + "确认密码为" + strpwd);
}
}
}
本人水平有限,还请各位朋友多多指教!
复制代码 代码如下:
<%@ page language="c#" autoeventwireup="true" codebehind="webform_1.aspx.cs" inherits="页面传值.webform_1" %>
<!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">
<div>
<asp:table id="tablelogin" runat='server'>
<asp:tablerow>
<asp:tablecell><label>用户名:</label></asp:tablecell>
<asp:tablecell><asp:textbox id="username" runat="server" width="150px"></asp:textbox></asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell><label>密码:</label></asp:tablecell>
<asp:tablecell><asp:textbox id="password" runat="server" width="150px"></asp:textbox></asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell><label>验证密码:</label></asp:tablecell>
<asp:tablecell><asp:textbox id="confimpwd" runat="server" width="150px"></asp:textbox></asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell><asp:button id="confirm" runat="server" text="确认" width="50px" onclick="confirm_click" /></asp:tablecell>
</asp:tablerow>
</asp:table>
</div>
</form>
</body>
</html>
webform_2.aspx页面如下:
复制代码 代码如下:
<%@ reference page="~/webform_1.aspx" %>
<%@ page language="c#" autoeventwireup="true" codebehind="webform_2.aspx.cs" inherits="页面传值.webform_2" %>
<!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">
<div>
</div>
</form>
</body>
</html>
webform_1.aspx.cs文件如下:
复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
namespace 页面传值
{
public partial class webform_1 : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
}
public string un//得到用户名
{
get
{
return username.text;
}
}
public string pwd//得到密码
{
get
{
return password.text;
}
}
public string conpwd//得到确认密码
{
get
{
return confimpwd.text;
}
}
/// <summary>
/// 向webform_2.aspx页面传值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void confirm_click(object sender, eventargs e)
{
//1:querystring页面传值
//string url = "webform_2.aspx?un=" + username.text + "&userpassword=" + password.text + "&conpwd=" + confimpwd.text;
//response.redirect(url);
//2:session传值
//session["un"] = username.text;
//session["pwd"] = password.text;
//session["conpwd"] = confimpwd.text;
//server.transfer("webform_2.aspx");
//3:使用cookie对象传值
//httpcookie cookie_name = new httpcookie("un");
//cookie_name.value = username.text;
//httpcookie cookie_pwd = new httpcookie("pwd");
//cookie_pwd.value = password.text;
//httpcookie cookie_conpwd = new httpcookie("conpwd");
//cookie_conpwd.value = confimpwd.text;
//response.appendcookie(cookie_name);
//response.appendcookie(cookie_pwd);
//response.appendcookie(cookie_conpwd);
//server.transfer("webform_2.aspx");
//4:使用application对象传值,类似session传值,作用范围全局所有用户
//application["un"] = username.text;
//application["pwd"] = password.text;
//application["conpwd"] = confimpwd.text;
//response.redirect("webform_2.aspx");
server.transfer("webform_2.aspx");
}
}
}
webform_2.aspx.cs文件如下:
复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
namespace 页面传值
{
public partial class webform_2 : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
//querytransfer();
//sessiontransfer();
//cookietransfer();
//applicationtransfer();
transfer();
}
public void querytransfer()//接收querystring传值,来自于webform_1页面的值
{
string strusername = request.querystring["un"].tostring();
string strpassword = request.querystring["userpassword"].tostring();
string strpwd = request.querystring["conpwd"].tostring();
response.write("用户名为" + strusername + "<br/>" + "密码为" + strpassword + "<br/>" + "确认密码为" + strpwd);
}
public void sessiontransfer()//接收session传值,来自于webform_1页面的值
{
string strusername = session["un"].tostring();
string strpassword = session["pwd"].tostring();
string strpwd = session["conpwd"].tostring();
response.write("用户名为" + strusername + "<br/>" + "密码为" + strpassword + "<br/>" + "确认密码为" + strpwd);
session.remove("un");
session.remove("pwd");
session.remove("conpwd");
}
public void cookietransfer()//接收cookie传值,来自于webform_1页面的值
{
string strusername = request.cookies["un"].value.tostring();
string strpassword = request.cookies["pwd"].value.tostring();
string strpwd = request.cookies["conpwd"].value.tostring();
response.write("用户名为" + strusername + "<br/>" + "密码为" + strpassword + "<br/>" + "确认密码为" + strpwd);
}
public void applicationtransfer()//接收application传值,来自于webform_1页面的值
{
application.lock();
string strusername = application["un"].tostring();
string strpassword = application["pwd"].tostring();
string strpwd = application["conpwd"].tostring();
application.unlock();
if (strpassword != strpwd)
{
response.write("您确认的密码错误,请重新输入!<br/>");
server.transfer("webform_1.aspx");
}
response.write("用户名为" + strusername + "<br/>" + "密码为" + strpassword + "<br/>" + "确认密码为" + strpwd);
}
public void transfer()//transfer传值,来自webform_1.aspx页面的值
{
webform_1 wf1;
wf1 = (webform_1)context.handler;
string strusername = wf1.un;
string strpassword = wf1.pwd;
string strpwd = wf1.conpwd;
response.write("用户名为" + strusername + "<br/>" + "密码为" + strpassword + "<br/>" + "确认密码为" + strpwd);
}
}
}
本人水平有限,还请各位朋友多多指教!
推荐阅读
-
asp.net页面传值测试实例代码(前后台)
-
asp.net后台关闭当前页面并传值的两种方法
-
ASP.NET页面之间传值的方式之Application实例详解
-
【Asp.net】使用Ajax和Jquery在前台向后台传参数并返回值的实例
-
ASP.NET页面之间传值的方式之Application实例详解
-
ASP.NET通过更改Url进行页面传值的实现代码
-
asp.net利用Ajax和Jquery在前台向后台传参数并返回值的实例
-
asp.net利用Ajax和Jquery在前台向后台传参数并返回值的实例
-
asp.net页面传值测试实例代码(前后台)
-
【Asp.net】使用Ajax和Jquery在前台向后台传参数并返回值的实例