Use the Server. Transfer () method in ASP. NET to pass values between pages.

Source: Internet
Author: User
ASP. NET Server. transfer () is A good way to pass values between two pages. When Transfer is transferred from page A to page B, you can use Context on page B. handler obtains an instance of A class on the page and calls each member object of A in B.

In the following example, WebForm1 and WebForm2 are created. The Server. Transfer () method is used to demonstrate how to read the textbox, read attributes, pass values through Context, and call WebForm1 in WebForm2:

Put a TextBox1 and a Button1 on WebForm1. The program 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 by region Web Form Designer
Override protected void OnInit (EventArgs e)
{
InitializeComponent ();
Base. OnInit (e );
}

Private void InitializeComponent ()
{
This. Button1.Click + = new System. EventHandler (this. button#click );
This. Load + = new System. EventHandler (this. Page_Load );

}
# Endregion

Private void button#click (object sender, System. EventArgs e)
{
Server. Transfer ("WebForm2.aspx", true );
}

Place a Literal1 control on WebForm2. The program is 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 by region Web Form Designer
Override protected void OnInit (EventArgs e)
{
InitializeComponent ();
Base. OnInit (e );
}

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

}
# Endregion
}

The second parameter of the Transfer method indicates whether the Form and QuerryString values of the page are retained. You can set it to False, and the TextBox1 value cannot 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.