Summary of the methods for passing values between. net pages in asp tutorial
It mainly sets the PostBackUrl attribute value and the name of the page to jump ("~ /Default2.aspx "), and then ("~ /Default2.aspx ") use the PreviousPage. FindControl () method to find the control on the current page.
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 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"];
}
Code:
Default. aspx:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.111cn.net">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> first page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox>
<Br/>
<Asp: Button ID = "Button1" runat = "server" Text = "Jump" PostBackUrl = "~ /Default2.aspx "/>
</Div>
</Form>
</Body>
</Html>
CS file on the second page
Default2.aspx. cs:
Protected void Page_Load (object sender, EventArgs e)
{
TextBox tex = (TextBox) PreviousPage. FindControl ("TextBox1 ");
Response. Write (tex. Text );
}
Post
Method: common method. Use form to submit.
Features: The most common method. A common technique is to submit hidden data in a hidden domain by form.
Applicable data: massive data, including file upload.
Applicability: same as the Get method
Usage: submit after the action target is specified in the form client, and use server in the servo end of the asp.net tutorial. transfer (url) submission; Request is used in the servo end. get Form ["FormFieldID.
1. Form submission,
<Form action = "target. aspx" method = "post" name = "form1">
<Input name = "param1" value = "1111"/>
<Input name = "param2" value = "2222"/>
</Form>
....
Form1.submit ();
....
This method is implemented in ASP. NET is invalid because ASP. . NET forms are always submitted to their own pages. If you want to submit other pages, special processing is required.
2. <A href = "target. aspx? Param1 = 1111 & param2 = 2222 "> Link transfer </A>
Receiving page: string str = Request ["param1"]
3. Session sharing
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 (or Server. Transfer) 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:
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 ");
}
Sending page: Session ("param1") = "1111 ";
String str = Session ("param1"). ToString ();
4. Application sharing
Sending page: Application ("param1") = "1111 ";
By page: string str = Application ("param1"). ToString ();
This method is not often used. Because the Application is shared within an Application domain, all users can change and set its value. Therefore, only the counters and other places that require global variables are used.
Use Application object variables
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 ();
}