Passing data between pages in asp.net

postback

Image credit: http://www.beansoftware.com

Passing data between pages in asp.net

Passing data between pages in asp.net is one of the most important aspects of web development.
As web pages are stateless in nature (All the information is lost when we browse to another page or close the browser), We have to look for the ways to pass the data from one page to another or to maintain the data even after post back or even till the browser is open and the cookies are not deleted.
Let’s discuss what options are available to do specific tasks.

1. Cross page post backs
One of the best features introduced with asp.net 2.0 is cross-page posting or simply posting the data to the different page(s). That feature was only available in the asp version but not in .net before the arrival of framework 2.0.
By the term post back, we mean the way to send the data back to the page itself. But cross-page posting enables us to post the data to another page as well.
Let’s see an example

PostingPage.aspx

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”PostingPage.aspx.cs” Inherits=”PostingPage” %>

<!DOCTYPE html>

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title>
<title>
<head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:Label ID=”lblPost” runat=”server” Text=”Hello World!”></asp:Label>
</br>
<asp:Button ID=”btnPost” runat=”server” PostBackUrl=”~/PostedPage.aspx” Text=”Post the Text” > < /asp:Button >
</form>
</body>
</html>

PostedPage.aspx

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”PostedPage.aspx.cs” Inherits=”PostedPage” %>

<!DOCTYPE html>

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title><
<head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:Label ID=”lblPosted” runat=”server” Text=”Posted Text is:”></asp:Label>

</form>
</body>
</html>

PostedPage.aspx.cs

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class PostedPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{

Label lblPosting = (Label)PreviousPage.FindControl(“lblPost”);
lblPosted.Text = lblPosting.Text + ” posted from PostingPage”;

}
}
}

2. QueryString

QueryString is preferred to transfer small amount of data and it is the best practice to avoid confidential data as the data appears in browser’s address bar. Let’s see how it works

QueryStringSender.aspx

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”QueryStringSender.aspx.cs” Inherits=”QueryStringSender” %<

<!DOCTYPE html >

< html xmlns=”http://www.w3.org/1999/xhtml” >
< head runat=”server” >
< title > < /title >
< /head >
< body >
< form id=”form1″ runat=”server” >
< div >
< asp:Button ID=”Button1″ runat=”server” Text=”Send QueryString” OnClick=”Button1_Click” />
< /div >
< /form >
< /body >
< /html >

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class QueryStringSender : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{

int ID=1;
Response.Redirect(“QueryStringReceiver.aspx?ID=” + ID.ToString());
}
}

QueryStringReceiver.aspx

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”QueryStringReceiver.aspx.cs” Inherits=”QueryStringReceiver” %>

<!DOCTYPE html>

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>

<asp:Label ID=”Label1″ runat=”server”></asp:Label>

</div>
</form>
</body>
</html>

QueryStringReceiver.aspx.cs

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class QueryStringReceiver : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString[“ID”] != null)
{

Label1.Text =”ID:” + Request.QueryString[“ID”];
}
}
}

3. The ViewState

Have you ever wondered why the values in the controls such as TextBox remain unchanged even when the page is refreshed ? Each control is provided with the ViewState repository that holds the value by default so that the control retains its value.
This ViewState is effective only if we do not browse away from our page.The article at msdn gives brief and easiest usage of the ViewState in our program. Point to be noted that this ViewState can also be encrypted to protect the data

4. The Session

Session management is the core technique in web development . You can’t be a good web developer if you don’t practice how to effectively manage the session. With the help of session management, we can retain anything we want (such as value of the variable ) as long as our browser is open. This session is alive at every page unless we terminate it manually. Consider the session as the interaction of the user with the browser when he opens it.

Simplest examples are:

Assigning a string to a Session at Page 1:

string str=”Session Management is fun”;
Session[“str”]=str;

Getting the value from that session in Page2:

if(Session[“str”]!=null)
string strGet=Session[“str”].ToString(); // casting is must for session

To terminate Session:

Session.Abandon();

Session.Remove(“str”);

Session[“str”] = null;

Session.Clear();

5. Cookies

Cookies are the files that are stored on the client PC that is surfing a specific site. The cookies contain important information about the current user interaction with the site so that the site owners get the idea about user experience with their site etc.These cookies can be stored in memory or on the hard disk(persistent cookies) . It is a best practice to delete all the cookies after surfing the internet.

1. Creating a cookie by HttpCookie Object and assigning properties:

HttpCookie myDetails= new HttpCookie(“myDetails”);
myDetails.Value = “I am a programmer”
myDetails.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(myDetails);

Getting a cookie value:

string myRecord = Request.Cookies[“myDetails”].Value;

2. Creating a cookie by Response object:

Response.Cookies[“myDetails”].Value =”I am a programmer” ;
Response.Cookies[“myDetails”].Expires = DateTime.Now.AddDays(1);

Retrieving cookie value

string myDetails = Request.Cookies[“myDetails”].Value;

2. Creating a cookie by Response object with multiple values:

Response.Cookies[“myDetails”][“Profession”] = “Engineering”;

2.Retrieving multiple cookie values:

string myDetails = Request.Cookies[“myDetails”][“Profession”];

How to expire a cookie?

if (Request.Cookies[“myDetails”] != null)
{
HttpCookie myDetails = new HttpCookie(“myDetails”);
myDetails.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(myCookie);
}

Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.
Syed Adeel Ahmed on FacebookSyed Adeel Ahmed on GoogleSyed Adeel Ahmed on InstagramSyed Adeel Ahmed on Twitter

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.