Several Methods for passing parameters between pages

Source: Internet
Author: User

Several Methods for passing parameters between Asp.net pages
Method 1: pass through URL link
Send. aspx:
Protected void button#click (object sender, EventArgs e)
{
Request. Redirect ("Default2.aspx? Username = honge ");
}
Receive. aspx:
String username = Request. QueryString ["username"];
In this way, the parameter value is obtained.

Method 2: post
Send. aspx
<Form id = "form1" runat = "server" action = "receive. aspx" method = post>
<Div>
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "button"/>
<Asp: textbox id = "username" runat = "server"> </ASP: textbox>
</Div>
</Form>
Receive. aspxstring username = ruquest. Form ["receive"];

Method 3: Use session
Send. aspx:
Protected void button#click (Object sender, eventargs E)
{
Session ["username"] = "honge ";
Request. Redirect ("Default2.aspx ");
}
Receive. aspx:
String username = Session ["username"];
In this way, the parameter value is obtained.

Method 4: Use Application
Send. aspx:
Protected void button#click (object sender, EventArgs e)
{
Application ["username"] = "honge ";
Request. Redirect ("Default2.aspx ");
}
Receive. aspx:
String username = Application ["username"];
In this way, the parameter value is obtained.

Method 5: Use Server. Transfer
Send. aspx:
Public string Name
{
Get
{
Return "honge ";
}
}
Protected void button#click (object sender, EventArgs e)
{
Server. Transfer ("Default2.aspx ");
}

Receive. aspx:
Send d = Context. Handler as send;
If (d! = Null)
{
Response. Write (d. Name); to obtain the parameter value.
}

In asp.net 2.0, you can use the following method:
PreviousPage
PreviousPage d = Context. Handler as PreviousPage;
If (d! = Null)
{
Response. Write (d. Name); to obtain the parameter value.
}

It can also be used as follows:
Send. aspx:
<Asp: Button ID = "btnSubmit" runat = "server" PostBackUrl = "~ /Reveive. aspx "Text =" Submit "/>

Receive. aspx:
<% @ PreviousPageType VirtualPath = "~ /Default. aspx "%>
String name = PreviousPage. Name;
In this way, the parameter value is obtained.

If MasterPage is used in your page, the PreviousPage passed by Server. Transfer is invalid. I don't know why.
If MasterPage is used, it is best to use Session or Context. Items ["username.
 
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, but the passed value is less secure.
This method is a good solution when the requirement for completeness is 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 the saved URL above
The following code snippet demonstrates how to implement this method:
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:
Private void page_load (Object sender, system. eventargs E)
{
Label1.text = request. querystring ["name"];
Label2.text = request. querystring ["email"];
}

Use session Variables
Using session variables is another way to pass values between pages. In this example, we store the values in the control in the session variables,
Use it on another page to transfer values between different pages. However, you must note that too many session variables are stored.
Data consumes a lot of server resources and should be careful when using sessions. 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. Clear the value explicitly when determining that this session is not required.
  
The following code snippet demonstrates how to implement this method:
Source Page code:
Private void button#click (Object sender, system. eventargs E)
{
File: // textbox1 and textbox2 are webform
File: // controls session ["name"] = textbox1.text;
Session ["email"] = TextBox2.Text;
Server. Transfer ("anotherwebform. aspx ");
}
Target Page code:
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 ");
}
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 in another
The page uses Object Attributes to access exposed values. Of course, to use this method, you need to write additional code to create some attributes so that you can
But the benefits of this method are also obvious. In general, this method is concise and
Is 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 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:
Add the following code to the page
Public string Name
{
Get
{
Return textbox1.text;
}
}
 
Public String email
{
Get
{
Return textbox2.text;
}
}
Then call the server. Transfer Method
Private void button#click (Object sender, system. eventargs E)
{
Server. Transfer ("anotherwebform. aspx ");
}
  
Target Page code:
Private void page_load (Object sender, system. eventargs E)
{
File: // create instance of source web form
Webform1 WF1;
File: // get reference to current handler instance
WF1 = (webform1) Context. Handler;
Label1.Text = wf1.Name;
Label2.Text = wf1.EMail;
}
Summary
This article describes how to use different methods to Transfer values between ASP. NET pages. The three methods are QueryString, Session, and Server. Transfer,
We should repeat the similarities and differences between several methods.

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.