Asp.net three basic parameter passing methods (web forms)

Source: Internet
Author: User

ASP. NET web forms provides developers with an excellent event-driven development mode. However, this simple applicationProgramThe development mode brings us some minor problems. For example,
In traditional ASP applications, you can easily transfer one or more values from one page to another through the POST method, and use the same method in ASP. net
Trouble. Here, we can solve this problem in other ways.
ASP. NET provides three methods for us,
You can use querystring to transmit the corresponding values.
The other is to transmit the corresponding value through the session variable.
Also, it is implemented through the server. Transfer Method. The following sections describe one by one:

I. querystring
Querystring is a simple value transfer method. Its disadvantage is that it displays the value to be transferred in the address bar of the browser and cannot pass objects in this method. If you want to upload

It is best to use this method when delivering a security that is not so important or a simple value. The following is a small example to complete the data transfer process. The procedure is as follows:
1. Create a web form
2. Place a button1 in the new web form, and place two textbox1 and textbox2
3. Create a click event for the button
CodeAs follows:
Private void button#click
(Object sender, system. eventargs E)
{
String URL;
Url = "webform2.aspx? Name = "+
Textbox1.text + "& Email =" +
Textbox2.text;
Response. Redirect (URL );
}
4. Create a new target page named webform2
5. Place label1 and label2 in webform2.
Add the following code to page_load of webform2:
Private void page_load
(Object sender, system. eventargs E)
{
Label1.text = request. querystring ["name"];
Label2.text = request. querystring ["email"];
}
Run the command to view the passed result.

Ii. Use session Variables

Using session variables to pass values is the most common method. In this method, values can be transmitted not only to the next page, but also to multiple pages until the session Variables

after the value is removed, the variable disappears. For example:
1. Create a web form
2. Place a button1 in the new web form, and place two textbox1 in it, textbox2
3. Create a click event for the button
code:
private void button#click
(Object sender, system. eventargs e)
{< br> session ["name"] = textbox1.text;
session ["email"] = textbox2.text;
response. redirect ("webform2.aspx");
}< br> 4. Create a target page named webform2
5. place two label1 files in webform2, label2
Add the following code to page_load of webform2:
privat E 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");
}< br> run, you can see the passed result.

Iii. Use server. Transfer
Although this method is a bit complicated, it is also a way to pass values on the page.
For example:
1. Create a web form
2. Place a button1 in the new web form, and place two textbox1 and textbox2
3. Create a click event for the button
The Code is as follows:
Private void button#click
(Object sender, system. eventargs E)
{
Server. Transfer ("webform2.aspx ");
}
4. The textbox1 is returned during the creation process. The value code of the textbox2 control is as follows:
Public string name
{
Get
{
Return textbox1.text;
}
}

Public String email
{
Get
{
Return textbox2.text;
}
}
5. Create a new target page named webform2
6. Place label1 and label2 in webform2.
Add the following code to page_load of webform2:
Private void page_load
(Object sender, system. eventargs E)
{
// Create an instance of the original form
Webform1 WF1;
// Obtain the instantiated handle
WF1 = (webform1) Context. Handler;
Label1.text = wf1.name;
Label2.text = wf1.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.