There are three methods for transferring values between ASP. NET pages: QueryString, Session, and Server. Transfer. By transferring values between pages, you can span different pages and implement complex functions.
ASP. NET:
Pass through URL link address
- send.aspx:
- protected void Button1_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.
The second method for transferring values between ASP. NET pages:
Post.
- send.aspx
-
- <form id="form1" runat="server" action="receive.aspx" method=post>
- <div>
- <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
- <asp:TextBox ID="username" runat="server"></asp:TextBox>
- </div>
- </form>
- receive.aspx
- string username = Ruquest.Form["receive"];
The third method for transferring values between ASP. NET pages:
Through session
- send.aspx:
- protected void Button1_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 Button1_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 StringName
- {
- Get{
- Return "Honge";
- }
- }
- Protected VoidButton#click (ObjectSender, EventArgs e)
- {
- Server. Transfer ("Default2.aspx");
- }
-
- Receive. aspx:
-
- Send d = Context. HandlerAsSend;
- If(D! =Null)
- {
- Response. Write (d. Name); to obtain the parameter value.
- }
If you can use this method in asp.net 2.0: PreviousPage
- PreviousPage d = Context. HandlerAsPreviousPage;
- 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, Server. the PreviousPage passed by Transfer is invalid. I don't know why. so 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, the disadvantage is that the passed value is insecure in the address bar of the browser. At the same time, objects cannot be transmitted. However, when the passed value is small and the security requirements are not high, this method is a good solution. The steps for using this method are as follows:
1. Create a web form using the control)
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 Button1_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 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. 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 Button1_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, 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. 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 Button1_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 implement value Transfer between ASP. NET pages. The three methods are QueryString, Session, and Server. Transfer. We should understand the similarities and differences between these methods. I hope this article will be helpful to you until you can use it in your code!
- In-depth research on Repeater controls: Maximum Flexibility
- Getting started with DataList controls
- Exploring the operation mechanism of the DataGrid Web Control
- Discuss the similarity between ASP. NET data Web controls
- Transformation from traditional ASP to ASP. NET: Understanding controls