Asp.net page value passing test instance code (front and back end)

Source: Internet
Author: User

The content of WebForm_1.aspx is as follows:
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "WebForm_1.aspx.cs" Inherits = "Page transfer value. 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> User name: </label> </asp: TableCell>
<Asp: TableCell> <asp: TextBox ID = "UserName" runat = "server" Width = "150px"> </asp: TextBox> </asp: TableCell>
</Asp: TableRow>
<Asp: TableRow>
<Asp: TableCell> <label> password: </label> </asp: TableCell>
<Asp: TableCell> <asp: TextBox ID = "PassWord" runat = "server" Width = "150px"> </asp: TextBox> </asp: TableCell>
</Asp: TableRow>
<Asp: TableRow>
<Asp: TableCell> <label> Verification password: </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 = "OK" Width = "50px" OnClick = "Confirm_Click"/> </asp: tableCell>
</Asp: TableRow>
</Asp: Table>
</Div>
</Form>
</Body>
</Html>

The WebForm_2.aspx page is as follows:
Copy codeThe Code is as follows:
<% @ Reference Page = "~ /WebForm_1.aspx "%>
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "WebForm_2.aspx.cs" Inherits = "Page transfer value. 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>

The WebForm_1.aspx.cs file is as follows:
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Pass value on the namespace page
{
Public partial class WebForm_1: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
}
Public string un // get the username
{
Get
{
Return UserName. Text;
}
}
Public string pwd // get the password
{
Get
{
Return PassWord. Text;
}
}
Public string conpwd // confirm the password
{
Get
{
Return ConfimPWD. Text;
}
}
/// <Summary>
/// Pass the value to the WebForm_2.aspx page
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protected void Confirm_Click (object sender, EventArgs e)
{
// 1: QueryString page Value
// String url = "WebForm_2.aspx? Un = "+ UserName. Text +" & userpassword = "+ PassWord. Text +" & conPwd = "+ ConfimPWD. Text;
// Response. Redirect (url );
// 2: Session value transfer
// Session ["un"] = UserName. Text;
// Session ["pwd"] = PassWord. Text;
// Session ["conpwd"] = ConfimPWD. Text;
// Server. Transfer ("WebForm_2.aspx ");
// 3: Use a cookie object to pass the value
// 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: Use the application object to pass the value, similar to the session to pass the value, the scope of the action is global to all users
// Application ["un"] = UserName. Text;
// Application ["pwd"] = PassWord. Text;
// Application ["conpwd"] = ConfimPWD. Text;
// Response. Redirect ("WebForm_2.aspx ");
Server. Transfer ("WebForm_2.aspx ");
}
}
}

The WebForm_2.aspx.cs file is as follows:
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Pass value on the namespace page
{
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 () // receives the value passed by QueryString from the value on the WebForm_1 page
{
String strUserName = Request. QueryString ["un"]. ToString ();
String strPassword = Request. QueryString ["userpassword"]. ToString ();
String strPWD = Request. QueryString ["conPwd"]. ToString ();
Response. write ("username is" + strUserName + "<br/>" + "password is" + strPassword + "<br/>" + "Confirm Password is" + strPWD );
}
Public void SessionTransfer () // receives the value from the WebForm_1 page.
{
String strUserName = Session ["un"]. ToString ();
String strPassword = Session ["pwd"]. ToString ();
String strPWD = Session ["conpwd"]. ToString ();
Response. write ("username is" + strUserName + "<br/>" + "password is" + strPassword + "<br/>" + "Confirm Password is" + strPWD );
Session. Remove ("un ");
Session. Remove ("pwd ");
Session. Remove ("conpwd ");
}
Public void CookieTransfer () // receives the cookie value from the value on the WebForm_1 page.
{
String strUserName = Request. Cookies ["un"]. Value. ToString ();
String strPassword = Request. Cookies ["pwd"]. Value. ToString ();
String strPWD = Request. Cookies ["conpwd"]. Value. ToString ();
Response. write ("username is" + strUserName + "<br/>" + "password is" + strPassword + "<br/>" + "Confirm Password is" + strPWD );
}
Public void ApplicationTransfer () // receives the value from the WebForm_1 page.
{
Application. Lock ();
String strUserName = Application ["un"]. ToString ();
String strPassword = Application ["pwd"]. ToString ();
String strPWD = Application ["conpwd"]. ToString ();
Application. UnLock ();
If (strPassword! = StrPWD)
{
Response. Write ("the password you confirmed is incorrect. Please enter it again! <Br/> ");
Server. Transfer ("WebForm_1.aspx ");
}
Response. write ("username is" + strUserName + "<br/>" + "password is" + strPassword + "<br/>" + "Confirm Password is" + strPWD );
}
Public void Transfer () // Transfer value, from the value on the WebForm_1.aspx page
{
WebForm_1 wf1;
Wf1 = (WebForm_1) Context. Handler;
String strUserName = wf1.un;
String strPassword = wf1.pwd;
String strPWD = wf1.conpwd;
Response. write ("username is" + strUserName + "<br/>" + "password is" + strPassword + "<br/>" + "Confirm Password is" + strPWD );
}
}
}

My skills are limited. Please give me more advice!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.