keyboard_arrow_up

How To Pass Values Between Two Web Pages In ASP.NET

How To Pass Values Between Two Web Pages In ASP.NET



ASP.NET is an open-source server-side web application framework designed for web development to develop dynamic web pages. Developed by Microsoft to allow programmers to build dynamic web sites, web applications and web services.

The successor of ASP.NET is ASP.NET Core. It is a re-integration of ASP.NET as a web framework, along with other frameworks.

ASP.NET web forms give excellent event driven programming model to developers. It simplifies the overall design of your application but has some problems of its own. For example, in conventional ASP values can be easily passed from one ASP page to another ASP page using POST. The same thing is impossible in ASP.NET if you want to stick to web form model .There are some ways that can be used to overcome this situation. This article denotes various possibilities to do the same.

Specifically we will deal how to pass values using querystring, how to utilise session variables to do the same and finally how to use Server.Transfer method.

Querystring

Querystring is a day old process to pass values across pages. The main advantage of this method is very simple. What limits is that the values are visible in the browser address bar and you can not pass objects that way. This method is best suited when you want to pass less number of values that need not be secured from others. In order to put this method you need to follow these steps:
Develop the web form with controls
Give some button that posts the form again
In the event of clicking the button design a string that holds URL for another
Add control values to the URL as querystring parameters
Response.Redirect to another form with this URL
Here is the code:

Source Web Form

private void Button1_Click
(object sender, System.EventArgs e)
{
string url;
url=”anotherwebform.aspx?name=” +
TextBox1.Text + “&email=” +
TextBox2.Text;
Response.Redirect(url);
}
Destination Web Form
private void Page_Load
(object sender, System.EventArgs e)
{
Label1.Text=Request.QueryString["name"];
Label2.Text=Request.QueryString["email"];
}

Using Session variables

Another way to pass values across pages. Here you store control values in session variables and reach out for them in another web form. But storing too much data in a session can be an overhead on the server. It also needs some kind of cleaning action from your side so that unwanted session variables are deleted The sequence of steps are as follows:
Develop the web form with controls
Give some link button that posts the form back
Add session variables and set them to control values
Response.Redirect to another form
In that form ingress Session variables and delete them if necessary
Here the code shows in action:

Source Web Form

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”);
}
Destination Web Form
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

A very complex but sophisticated method of passing values across pages. Here you expose the values you want to find in other pages as properties of the page class. This method needs to code properties that you can access in another web form. Overall this method is much clearer and object oriented than earlier methods. This is how the entire process works:
Develop the web form with controls
Develop property Get procedures
Give some link button that posts the form back
In the button click event handler use Server.Transfer method that will transfer execution to the specific form
In the second form you can get a reference to the first form by using Context.Handler property.
The code to perform this is complex and is shown below:
Source Web Form
Add properties to the web form:

public string Name
{
get
{
return TextBox1.Text;
}
}

public string EMail
{
get
{
return TextBox2.Text;
}
}
Now, call Server.Transfer.
private void Button1_Click
(object sender, System.EventArgs e)
{
Server.Transfer(“anotherwebform.aspx”);
}
Destination Web Form
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;
}

Summary

This article shows various ways to pass data between two ASP.NET web forms. The three methods namely Querystring, Session and Server.Transfer provide ways to pass our values across pages.
Hope this article is useful!

We constantly make an effort to update the latest in ASP .NET

If you are considering to take .Net training then our CRB Tech .Net Training center could be very helpful in fulfilling your aspirations. Learn ASP.Net with us and enhance yourself in your career.

Stay connected to this page of CRB Tech .NET reviews for more technical up-gradation and other resources.

Related Articles:

Here Are Few Tips on Asp.Net Life Cycle

Importance of Asp.Net Development