Asp. NET using the Server.Transfer () method to pass value instances between pages

Source: Internet
Author: User

The following code is tested in VS2008 by

<%@ Page language="C #"autoeventwireup="true"codefile="WebForm1.aspx.cs"inherits="WebForm1"%><! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">"http://www.w3.org/1999/xhtml">"Server"> <title></title>"Form1"runat="Server"> <div> <asp:textbox id="TextBox1"runat="Server"></asp:textbox><br/> <asp:button id="Button1"runat="Server"text="Button"onclick="Button1_Click"/> </div> </form></body>View Code
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls; Public Partial classwebform1:system.web.ui.page{ Public stringTime {Get{returnDateTime.Now.ToString ();} }     Public stringTestfun () {return "Function of WebForm1 called"; }    protected voidPage_Load (Objectsender, EventArgs e) {CONTEXT.ITEMS.ADD ("Context","Context from Form1"); }    protected voidButton1_Click (Objectsender, EventArgs e) {        //This . TextBox2.Text =request ["TextBox1"]. ToString ();Server.Transfer ("webform2.aspx",true);//when the second parameter is false, the contents of TextBox1 cannot be obtained in webform2.aspx             }}
View Code

Webform2.aspx Source

<%@ Page language="C #"autoeventwireup="true"codefile="WebForm2.aspx.cs"inherits="WebForm2"%><%@ Reference page="~/webform1.aspx"%><%--Notice here--%><! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">"http://www.w3.org/1999/xhtml">"Server"> <title></title>"Form1"runat="Server"> <div> <asp:literal id="Literal1"runat="Server"></asp:Literal> </div> </form></body>View Code
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls; Public Partial classwebform2:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) {        stringStrtxt =""; WebForm1 Oform= (WEBFORM1) This.        Context.Handler; Strtxt+="Value of Textbox:"+ request.form["TextBox1"] +"<br>"; Strtxt+="Time Property :"+ Oform.time +"<br>"; Strtxt+="Context String:"+ context.items["Context"]. ToString () +"<br>"; Strtxt+ = Oform.testfun () +"<br>"; Literal1.text=Strtxt; }}
View Code

Excerpted from: http://blog.csdn.net/vchao13/article/details/5004502

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.