Methods for transferring values between ASP. NET pages (1)

Source: Internet
Author: User

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 (such as textbox)
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

For example:
Source Page Code :
Private void button#click (Object sender, system. eventargs E)
{
String URL;
Url = "anotherwebform. aspx? Name= "+Textbox1.text + "& Email = "+ Textbox2.text ;
Response. Redirect (URL );
}
Target Page code: (display location: such as label)
Private void page_load (Object sender, system. eventargs E)
{
Label1.text = Request. querystring [" Name "];
Label2.text = Request. querystring ["Email "];
}

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 the session variable.
4. Use the response. Redirect method to redirect to another page
5. Extract the session value from another page. When determining that this session is not required, clear it explicitly.

example:
Source Page code:
private void button#click (Object sender, system. eventargs e)
{< br> // textbox1 and textbox2 are webform
// controls
session [" name "] = textbox1.text;
session [" email "] = textbox2.text;

// response. redirect ("anotherwebform. aspx ");
server. transfer ("anotherwebform. aspx ");
}< br> Target Page code:
private void page_load (Object sender, system. eventargs e)
{< br> label1.text = session ["name"]. tostring ();
label2.text = session ["email"]. tostring ();
session. remove ("name");
session. remove ("email");
}

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. You can use this method on another pageObject AttributesTo access the exposed value. Of course, to use this method, 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 event processing button.ProgramTo transfer the server. 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 access the value of the control on the previous page.
The following code comprehensively implements the above steps:
Source Page code:

  Public   String Name
{
Get { Return Textbox1.text ;}
}

 

Public StringEmail
{
Get{ReturnTextbox2.text ;}
}

 

 

Then call the server. Transfer () method

Private   Void Button#click ( Object Sender, system. eventargs E)
{
Server. Transfer ( " Anotherwebform. aspx " );
}

 

 

Target Page code:

In anotherwebform. aspx, add <% @ reference page = "~ /Gcsetting. aspx "%>

Add the following in anotherwebform. aspx.

Anotherwebform. 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;
}

 

 

Another transferserver. Transfer () method

Do not perform get operations by name or email.

Private VoidButton#click (ObjectSender, system. eventargs E)
{
Server. Transfer ("Anotherwebform. aspx? Name ="+ Name +" & Email = "+ email);
}

// Use server. the address in the address bar of the transfer () method remains unchanged, and the transmitted parameters are not displayed in plaintext. response is used. in the Redirect () method, the address bar displays the information on the target page and the plaintext is displayed.

 

Another method

Protected   Void Page_load ( Object Sender, eventargs E)
{
String Name = Request. querystring [ " Name " ];
String Password = Request. querystring [ " Email " ];
This . Label1.text =   " User name: "   + Name + " " +   " Email: "   + Email;
}

 

 

 

 

Related Article

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.