Several Methods for passing page values

Source: Internet
Author: User

1. Querystring

Using querysting to pass values between pages is already a very old mechanism. The main advantage of this method is that it is very simple to implement, however, its disadvantage is that the passed value is displayed on the address bar of the browser (Insecure), and the object cannot be passed, however, this method is a good solution when the number of transmitted values is small and the security requirements are not high. The steps for using this method are as follows: 1. Use the control to create a web form (Form)

2. Create buttons and link buttons that can return the form

3. Create a character variable to save the URL in the button or link button click event.

4. Add the querystring parameter to the saved URL.

5. Use response. Redirect to redirect to the saved URL above

The following code snippet demonstrates how to implement this method:

Some code in the Source Page webform1.aspx. CS:

Private void button#click (Object sender, system. eventargs E)

{

String URL;

Url = "webform2.aspx? Name = "+ textbox1.text +" & Email = "+

Textbox2.text;

Response. Redirect (URL );

}

Some code in webform2.aspx. CS on the target page:

Private void page_load (Object sender, system. eventargs E)

{

Label1.text = request. querystring ["name"];

Label2.text = request. querystring ["email"];

}

 

2. Use session Variables

Using the session variable is another way to pass values between pages. In this example, we store the value in the control in the session variable and then use it on another page, to transfer values between different pages. However, it should be noted that storing too much data in the session variable will consume a lot of server resources, so you should be careful when using the session. Of course, we should also use some cleanup actions to remove unnecessary sessions to reduce unnecessary resource consumption. The general steps for transferring values using session variables are as follows:

1. Add necessary controls to the page.

2. Create buttons and link buttons that can return the form

3. In the Click Event of a button or link button, add the control value to session variable 4 and use response. redirect (or server. the transfer method redirects to another page 5, extracts the session value from another page, and explicitly clears the session value when determining that this session is not required.

The following code snippet demonstrates how to implement this method:

Some code in the Source Page webform1.aspx. CS:

Private void button#click (Object sender, system. eventargs E)

{

// Textbox1 and textbox2 are webform

// Controls

Session ["name"] = textbox1.text;

Session ["email"] = textbox2.text;

Server. Transfer ("webform2.aspx ");

}

Some code in webform2.aspx. CS on the target page:

Private void page_load (Object sender, system. eventargs E)

{

Label1.text = session ["name"]. tostring ();

Label2.text = session ["email"]. tostring ();

Session. Remove ("name ");

Session. Remove ("email ");

}

3. Use server. Transfer

This method is a little more complex than the method described above, but it is particularly useful in inter-page value transfer, using this method, you can access the exposed value in the form of Object Attributes on another page. Of course, this method is used, you need to write additional code to create some attributes so that you can access it on another page. However, the benefits of this method are also obvious. In general, this method is concise and object-oriented. The entire process of using this method is as follows: 1. Add necessary controls to the page.

2. Create the get attribute process of the returned value

3. Create buttons and link buttons that can return the form

4. click the button to call server in the event handler. transfer the transfer method to the specified page 5. On the second page, we can use context. the handler property is used to obtain the reference of the previous page instance object. With it, you can use the value of the control to access the previous page. The following code comprehensively implements the code of the above step process:

Some code in the Source Page webform1.aspx. CS:

Add the following code to the page

Public string name

{

Get

{

Return textbox1.text;

}

}

Public String email

{

Get

{

Return textbox2.text;

}

}

Page 2/6

Then call the server. Transfer Method

Private void button#click (Object sender, system. eventargs E)

{

Server. Transfer ("webform2.aspx ");

}

Target Page code:

In webform2.aspx, be sure to add

<% @ Reference page = "~ /Webform1.aspx "%> or

<% @ Previouspagetype virtualpath = "~ /Webform1.aspx "%>

Add the following in webform2.aspx. CS.

 

Private void page_load (Object sender, system. eventargs E)

{

// Create instance of source web form

Webform1 WF1;

// Get reference to current handler instance

WF1 = (webform1) Context. Handler;

Label1.text = wf1.name;

Label2.text = wf1.email;

}

If an error occurs during debugging

C: \ windows \ Microsoft. NET \ framework \ v2.0.50727 \ temporary ASP. NET files, delete the folder of the newly created website name. (disable the Visual Studio development environment and then delete it)

4. Run the @ previouspagetype command.

This command is. NET 2.0, used to process ASP. NET 2.0 provides the cross-page transfer function. used to determine the page on which the cross-page transfer process starts. it contains two attributes: typename: the name of the derived class for sending back.

Virtualpath: Set the address of the page to be sent during delivery.

Example:

The Source Page webform1.aspx contains a textbox with the ID txtname.

Set an attribute in webform1.aspx. CS:

Public textbox name

Page 3/6

Get {return this.txt name;} // returns a control object

}

Add the following at the top of the design file (webform2.aspx) on the target page:

<% @ Previouspagetype virtualpath = "~ /Page1.aspx "%>,

Then you can reference the attributes defined in webform1.aspx.

In webform2.aspx. CS, You can reference the following format (assume that webform2.aspx has a label with the ID of lblname ):

Lblname. Text = "hello" + previouspage. Name. Text + "<br/> ";

5. Use the postbackurl attribute of some controls

Example: The Source Page is still webform1.aspx and the target page is webform2.aspx.

Some code in webform1.aspx:

<Asp: button id = "btnpostback" runat = "server"

TEXT = "pbbutton"> </ASP: button>

<Asp: textbox id = "txtname" runat = "server"> </ASP: textbox>

<Asp: Calendar id = "calendar1" runat = "server"> </ASP: Calendar>

Some code in webform2.aspx. CS:

Protected void page_load (Object sender, system. eventargs E)

{

Textbox txtname;

Calendar calendar1;

Txtname = (textbox) previouspage. findcontrol ("txtname ");

Calendar1 = (calendar) previouspage. findcontrol ("calendar1 ");

Label. Text = "hello," + txtname. Text + calendar1.selecteddate. tow.datestring ();

Page 4/6

This method has a problem: what if someone requested webform2.aspx before clicking the button, that is, before webform1.aspx is not processed? This requires

Before processing the code in webform2.aspx, add a judgment. Use the iscrosspagepostback attribute, which is similar to the ispostback attribute. It allows you to check whether the request is from webform1.aspx:

Protected void page_load (Object sender, system. eventargs E)

{

If (previouspage. iscrosspagepostback)

{

Textbox txtname;

Calendar calendar1;

Txtname = (textbox) previouspage. findcontrol ("txtname ");

Calendar1 = (calendar) previouspage. findcontrol ("calendar1 ");

Label. Text = "hello," + txtname. Text + calendar1.selecteddate. tow.datestring ();

}

Else

{

Response. Redirect ("webform1.aspx ");

}

}

6. Use cookie object variables

This is also a common method. Like session, it is for every user, but there is a fundamental difference, that is, cookies are stored on the client, session is stored on the server. In addition, the use of cookies should be used in combination with the ASP. NET built-in Object Request. A. aspx C # code

Private void button#click (Object sender, system. eventargs E)

Page 5/6

Httpcookie cookie_name = new httpcookie ("name ");

Cookie_name.value = label1.text;

Reponse. appendcookie (cookie_name );

Server. Transfer ("B. aspx ");

}

B. C # code in aspx

Private void page_load (Object sender, eventargs E)

{

String name;

Name = request. Cookie ["name"]. value. tostring ();

}

7. Use the application object variable

The scope of the Application object is global, that is, it is valid for all users. The common methods are lock and unlock.

A. aspx C # code

Private void button#click (Object sender, system. eventargs E)

{

Application ["name"] = label1.text;

Server. Transfer ("B. aspx ");

}

B. C # code in aspx

Private void page_load (Object sender, eventargs E)

{

String name;

Application. Lock ();

Name = application ["name"]. tostring ();

Application. Unlock ();

}

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.