Transfer value between web form pages

zhaozj2021-02-16  94

Many times, you will need to position from one page to another page, pass the value from one page to another page. For example, a page may prompt the user to provide name and password. When the user submits the form, you need to call another page to authenticate the user.

There are a variety of methods for sharing information between pages:

Use the query string that adds information to the URL and passes the URL to the next page. This method exists to make the disadvantage of information. For more information, see

WebClient.QueryString property

with

HttpRequest.QueryString properties

. Use session status storage information, all pages in the user's current session can be accessed globally. However, this method takes up server memory, and the information is only stored before the session expires, which has more system overhead than to the next page. For more information, see Web Form Status Management. Use the custom page properties and redirect from the initial page to the target page, and you can still read the value on the old page. This strategy is described in this topic.

The web form page is a class in the application, so you can create properties to handle any classes. However, because the web form page is actually existed during the execution page, their life cycle is very short. Therefore, the use of the web form page is quite limited because they exist only during the process of processing the page. However, if you use specific technologies to pass the control from one page to another, you can still access the properties on the previous page.

Create a shared value on the source page

In the code, one or more read-only properties on the standard syntax statement page of the property. Returns the attribute value to be passed to the next page. The following example shows how to declare the properties called Property1 and set it to the value of text box on the page: 'Visual Basic Public Readonly Property Property1 () AS String Get Return TEXTBOX1.TEXT END GET End Property // C # Public String Property1 {get {return;}} By calling Server objects (

Httpserverutility

Category

TRANSFER

Method calls the next page and passes the URL of the page to which the information is passed to. The following example shows how to call a page called WebForm2 from the event handler (in the same item): 'Visual Basic Private Sub Button1_Click (Byval E AS System.Event, Byval E AS System.EventArgs) Handles Button1.Click Server.Transfer ("Webform2.aspx") End Sub // C # Private Void Button1_Click (Object Sender, System.EventArgs E) {Server.Transfer ("Webform2.aspx");}

To get the attribute value of the first page from the calling page, create an instance variable of the source page class. You can then assign HTTP objects to it (

IHTTPHANDALER

An example of a class), that is, the object that receives the initial request.

Read the attribute value in the source page in the called page

Create a global instance variable, which is typed as a class of the source page. The following example shows how to declare the type of the type indicated by the page named WebForm1: 'Visual Basic Public SourcePage As Webform1 // C # public WebForm1 SourcePage; In the Page_Load handler, from the Context.handler object (Ihttphandler

The source page is obtained in the interface and then assigned to the variable created in step 1. This handler object must be converted to the type of the source page class.

Note that this logic should only be run at the page for the first time (ie, call this page for the first time).

Get the attribute value from the source page and use these attribute values ​​in a manner that uses any object properties.

Note If you want to use attribute values ​​in any page processing phase other than the first page initialization phase, you must save these attribute values ​​(for example, saved in view status). For more information, see

Web Forms Status Management Introduction.

Complete Page_Load handler may look like the following: 'Visual Basic Private Sub Page_Load (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then sourcepage = CType (Context.Handler , WebForm1) Label1.Text = sourcepage.Property1 End If End Sub // C # private void Page_Load (object sender, System.EventArgs e) {if (IsPostBack) {WebForm1 sourcepage = (WebForm1) Context.Handler;! Label1.Text = Sourcepage.property1;}}

See

Web Form Status Management

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

New Post(0)