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"];
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. aspx
String 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"];
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"];
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.
}
For example, in asp.net 2.0, you can use the PreviousPage
PreviousPage d = Context. Handler as PreviousPage;
If (d! = Null)
{
Response. Write (d. Name); to obtain the parameter value.
}
You can also use
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 in the address bar of the browser (Insecure ),
Objects cannot be passed, but this method is a good solution if the passed value 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:
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 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 is important to note that storing too much data in Session variables will consume a large amount of server resources. You should be careful when using session. Of course, we should also use some cleanup actions to remove unnecessary sessions.
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.
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 extremely 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. Use
The entire process of 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;
}