熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

asp.net頁面傳值測試實例代碼

2022-06-13   來源: .NET編程 

  WebForm_aspx內容如下:

復制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm_aspxcs" Inherits="頁面傳值WebForm_" %>
<!DOCTYPE html PUBLIC "//WC//DTD XHTML Transitional//EN" "
<html xmlns="
<head runat="server">
<title></title>
</head>
<body>
<form id="form" 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="px"></asp:TextBox></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell><label>密碼</label></asp:TableCell>
<asp:TableCell><asp:TextBox ID="PassWord" runat="server" Width="px"></asp:TextBox></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell><label>驗證密碼</label></asp:TableCell>
<asp:TableCell><asp:TextBox ID="ConfimPWD" runat="server" Width="px"></asp:TextBox></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell><asp:Button ID="Confirm" runat="server" Text="確認" Width="px" OnClick="Confirm_Click" /></asp:TableCell>
</asp:TableRow>
</asp:Table>
</div>
</form>
</body>
</html>

  
WebForm_aspx頁面如下

復制代碼 代碼如下:
<%@ Reference Page="~/WebForm_aspx" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm_aspxcs" Inherits="頁面傳值WebForm_" %>
<!DOCTYPE html PUBLIC "//WC//DTD XHTML Transitional//EN" "
<html xmlns="
<head runat="server">
<title></title>
</head>
<body>
<form id="form" runat="server">
<div>
</div>
</form>
</body>
</html>

  
WebForm_aspxcs文件如下

復制代碼 代碼如下:
using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemWeb;
using SystemWebUI;
using SystemWebUIWebControls;
namespace 頁面傳值
{
public partial class WebForm_ : SystemWebUIPage
{
protected void Page_Load(object sender EventArgs e)
{
}
public string un//得到用戶名
{
get
{
return UserNameText;
}
}
public string pwd//得到密碼
{
get
{
return PassWordText;
}
}
public string conpwd//得到確認密碼
{
get
{
return ConfimPWDText;
}
}
/// <summary>
/// 向WebForm_aspx頁面傳值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Confirm_Click(object sender EventArgs e)
{
//QueryString頁面傳值
//string url = "WebForm_aspx?un=" + UserNameText + "&userpassword=" + PassWordText + "&conPwd=" + ConfimPWDText;
//ResponseRedirect(url);
//Session傳值
//Session["un"] = UserNameText;
//Session["pwd"] = PassWordText;
//Session["conpwd"] = ConfimPWDText;
//ServerTransfer("WebForm_aspx");
//使用cookie對象傳值
//HttpCookie cookie_name = new HttpCookie("un");
//cookie_nameValue = UserNameText;
//HttpCookie cookie_pwd = new HttpCookie("pwd");
//cookie_pwdValue = PassWordText;
//HttpCookie cookie_conpwd = new HttpCookie("conpwd");
//cookie_conpwdValue = ConfimPWDText;
//ResponseAppendCookie(cookie_name);
//ResponseAppendCookie(cookie_pwd);
//ResponseAppendCookie(cookie_conpwd);
//ServerTransfer("WebForm_aspx");
//使用application對象傳值類似session傳值作用范圍全局所有用戶
//Application["un"] = UserNameText;
//Application["pwd"] = PassWordText;
//Application["conpwd"] = ConfimPWDText;
//ResponseRedirect("WebForm_aspx");
ServerTransfer("WebForm_aspx");
}
}
}

  
WebForm_aspxcs文件如下

復制代碼 代碼如下:
using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemWeb;
using SystemWebUI;
using SystemWebUIWebControls;
namespace 頁面傳值
{
public partial class WebForm_ : SystemWebUIPage
{
protected void Page_Load(object sender EventArgs e)
{
//QueryTransfer();
//SessionTransfer();
//CookieTransfer();
//ApplicationTransfer();
Transfer();
}
public void QueryTransfer()//接收QueryString傳值來自於WebForm_頁面的值
{
string strUserName = RequestQueryString["un"]ToString();
string strPassword = RequestQueryString["userpassword"]ToString();
string strPWD = RequestQueryString["conPwd"]ToString();
ResponseWrite("用戶名為" + strUserName + "<br/>" + "密碼為" + strPassword + "<br/>" + "確認密碼為" + strPWD);
}
public void SessionTransfer()//接收session傳值來自於WebForm_頁面的值
{
string strUserName = Session["un"]ToString();
string strPassword = Session["pwd"]ToString();
string strPWD = Session["conpwd"]ToString();
ResponseWrite("用戶名為" + strUserName + "<br/>" + "密碼為" + strPassword + "<br/>" + "確認密碼為" + strPWD);
SessionRemove("un");
SessionRemove("pwd");
SessionRemove("conpwd");
}
public void CookieTransfer()//接收cookie傳值來自於WebForm_頁面的值
{
string strUserName = RequestCookies["un"]ValueToString();
string strPassword = RequestCookies["pwd"]ValueToString();
string strPWD = RequestCookies["conpwd"]ValueToString();
ResponseWrite("用戶名為" + strUserName + "<br/>" + "密碼為" + strPassword + "<br/>" + "確認密碼為" + strPWD);
}
public void ApplicationTransfer()//接收Application傳值來自於WebForm_頁面的值
{
ApplicationLock();
string strUserName = Application["un"]ToString();
string strPassword = Application["pwd"]ToString();
string strPWD = Application["conpwd"]ToString();
ApplicationUnLock();
if (strPassword != strPWD)
{
ResponseWrite("您確認的密碼錯誤請重新輸入!<br/>");
ServerTransfer("WebForm_aspx");
}
ResponseWrite("用戶名為" + strUserName + "<br/>" + "密碼為" + strPassword + "<br/>" + "確認密碼為" + strPWD);
}
public void Transfer()//Transfer傳值來自WebForm_aspx頁面的值
{
WebForm_ wf;
wf = (WebForm_)ContextHandler;
string strUserName = wfun;
string strPassword = wfpwd;
string strPWD = wfconpwd;
ResponseWrite("用戶名為" + strUserName + "<br/>" + "密碼為" + strPassword + "<br/>" + "確認密碼為" + strPWD);
}
}
}

  
本人水平有限還請各位朋友多多指教!


From:http://tw.wingwit.com/Article/program/net/201311/14111.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.