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

Source: Internet
Author: User
Asp.net|server asp.net server.transfer () is a good way to transfer values between two pages, from page A to Transfer to page B, You can get an instance of a class on page A on page b by Context.Handler, and call each member object of a in B.

The following example establishes WebForm1 and WebForm2, using the Server.Transfer () method to demonstrate Reading WebForm1 text boxes in WebForm2, reading properties, passing values through the context, invoking WebForm1 methods, and so on:

A TextBox1 and a Button1 are placed on the WEBFORM1, and the procedure is as follows:

public class WebForm1:System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load (object sender, System.EventArgs e)
{
CONTEXT.ITEMS.ADD ("Context", "context from Form1");
}
public string Time
{
Get{return DateTime.Now.ToString ();}
}
public string Testfun ()
{
Return "Function of WebForm1 called";
}
Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
InitializeComponent ();
Base. OnInit (e);
}

private void InitializeComponent ()
{
This. Button1.Click + = new System.EventHandler (this. Button1_Click);
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion

private void Button1_Click (object sender, System.EventArgs e)
{
Server.Transfer ("Webform2.aspx", true);
}


Place a Literal1 control on the WebForm2, as follows:

public class WebForm2:System.Web.UI.Page
{
protected System.Web.UI.WebControls.Literal Literal1;

private void Page_Load (object sender, System.EventArgs e)
{
String strtxt= "";
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;
}

Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
InitializeComponent ();
Base. OnInit (e);
}

private void InitializeComponent ()
{
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion
}

The added note is that the second parameter of the transfer method indicates whether the page's form and querrystring values are preserved, and you can try to set it to false, and the TextBox1 value will not be read in WebForm2.




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.