ASP.NET uses server.transfer () method on page

xiaoxiao2021-03-06  15

ASP.NET Server.Transfer () is a good way to pass the values ​​between the two pages. When the A page Transfer to the B page, you can get an instance of a class of a page at the B page via Context.Handler, thus Call the individual members of A in B.

The following example establishes WebForm1 and WebForm2, demonstrates the text box for reading WebForm1 through the server.transfer () method, read properties, and calls WebForm1 method, etc.

Place 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";} # region Web form Designer generated code 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 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"] "
"; strodxt = "Time Property:" = ""; strtxt = " Context string: " context.items [" context "]. TOSTRING () "
"; strtxt = = testfun () "
"; literal1.text = strtxt;} #Region web form Designer Generated Code Override Protected Void OnNit (Eventargs E) {InitializationComponent (); Base.onit (E);} Private Void InitializationComponent () {this.Load = New System.EventHandler (this.page_load);

} #Endregion}

Supplementary description is the second parameter of the Transfer method indicates whether the value of the FORM and QUERRYSTRING of the page is retained. You can try to set it to false, then you can't read the TEXTBOX1 value in WebForm2.

转载请注明原文地址:https://www.9cbs.com/read-48070.html

New Post(0)