Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Method steps of transferring parameters between pages in ASP.Net

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the method steps of passing parameters between pages in ASP.Net". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian to study and learn "the method steps of passing parameters between pages in ASP.Net" together.

Using QueryString

QuerySting is an old mechanism for passing values between pages. The main advantage of this method is that it is very simple to implement. However, the disadvantage is that the values passed are displayed in the browser address bar (unsafe) and the objects cannot be passed. However, this method is still a good solution when the value passed is small and the security requirements are not high.

The steps to use this method are as follows:

Creating web forms using controls (forms)

Create buttons and link buttons that return forms

Create a character variable that holds the URL in the click event of a button or link button

Add the QueryString parameter to the saved URL

Redirect to the URL saved above using Response.Redirect 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"]; }

Using the Session Variable

Using Session variables is another way to pass values between pages. In this case, we store the value in the control in Session variables and then use it in another page to achieve value transfer between pages. However, it should be noted that storing too much data in the Session variable will consume more server resources, and we should be cautious when using sessions. Of course, we should also use some cleanup actions to remove some unnecessary sessions to reduce unnecessary resource consumption.

The general procedure for passing values using Session variables is as follows:

Add necessary controls to the page

Create buttons and link buttons that return forms

Add the control's value to the session variable in the button or link button click event

Redirect to another page using the Response.Redirect method

Extract the session value on another page, and explicitly clear it when you are sure you don't need to use it. The following code snippet demonstrates how to do this:

Source Page Code:

private void Button1_Click (object sender, System.EventArgs e) { //textbox1 and textbox2 are webform 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"); }

Using Server.Transfer

This method is slightly more complex than the one described above, but it is particularly useful in inter-page value passing, where you can access exposed values as object properties on another page. Of course, with this method, you need to write extra code to create properties that can be accessed on another page, but the benefits are obvious. Overall, using this approach is both concise and object-oriented.

The whole process of using this method is as follows:

Add necessary controls to the page

Create Get property procedure for return value

Create buttons and link buttons that return forms

Call the Server.Transfer method in the button click event handler to transfer to the specified page

In the second page, we can use the Context.Handler property to get a reference to the previous page instance object, through which we can use the value of the control to access the previous page. The following code synthesizes the code for implementing 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) { //create instance of source web form WebForm1 wf1; //get reference to current handler instance wf1=(WebForm1)Context.Handler; Label1.Text=wf1.Name; Label2.Text=wf1.EMail;} Thank you for reading, the above is the "ASP.Net between the pages of the method steps" of the content, after learning this article, I believe that we have a deeper understanding of the ASP.Net between the pages of the method steps, the specific use of the need for everyone to practice verification. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report