Introduction
This article helps to list the most used ways to pass or transfer data among multiple web pages or an entire web site.
The important part of the website development is how to manage data transfer across web pages. As you all know, the Hypertext Transfer Protocol (HTTP) is the transport protocol or the application layer above TCP/IP that is responsible for each server and client request; it is stateless. It does not keep any data information for the next page response-request; the connection is closed from the server. So to overcome this problem there are some techniques that help to maintain the data across each request.
Getting Started
There are various ways to pass data information across pages. Let's start.
1. Cookies
A small piece of information or message sent by the web server to the web browser during a web request,
that is stored on the user's system in the form of a text file. This is the method used most often to store data.
How to use Cookie:
HttpCookie UserCookie = new HttpCookie("User");
UserCookie.Value = txtName.Text;
UserCookie.Expires = DateTime.Now.AddHours(1);
Response.Cookies.Add(UserCookie);
How to retrieve a Cookie value:
lblMsg.Text = Request.Cookies["User"].Value;
2. QueryString
Another way or method to pass data among web pages through URL's is:
http://www.c-sharpcorner.com/Notification/Notification.aspx?Mode=out
The section beyond the part marked in red is where the query string starts, using a question mark (?) as separator.
Using "&" we can pass multiple query strings.
The following is a sample use of a Query String:
Response.Redirect(String.Format("RetrieveData.aspx?user={0}&id={1}", txtName.Text, 1));
The following is a sample of how to retrieve a Query String value:
if (Request.QueryString["user"] != null)
{
lbl2.Text = Request.QueryString["user"];
}
3. Sessions
The most acceptable and secure method is by using Session variables.
The following is a sample use of a Session:
Session["user"] = txtName.Text;
Session["id"] = 1;
List<string> colors = new List<string>();
colors.Add("red");
colors.Add("green");
colors.Add("blue");
Session["colors"] = colors;
colors = null;
Response.Redirect("RetrieveData.aspx");
The following sample of how to retrieve a Session value:
if (Session["user"] != null)
{
lbl3.Text = Session["user"].ToString();
}
if (Session["colors"] != null)
{
List<string> col = new List<string>();
col = (List<string>) Session["colors"];
foreach (var item in col
{
lbl3.Text += " " + item + ", ";
}
col = null;
}
4. Cross-Page Posting
Another technique used to post from one page to another page is by setting the PostBackUrl property to the target page.
The PreviousPage property in the target page contains a reference to the source page.
The following is a sample of how to use Cross-Page Posting:
<asp:Button ID="btnPage2" Text="Call Page2" runat="server" width="132px" PostBackUrl="~/RetrieveData.aspx" />
The following is a sample of how to use a Cross-Page Posting value:
if (Page.PreviousPage != null)
{
DropDownList ddl = (DropDownList)Page.PreviousPage.FindControl("ddl1");
if (ddl != null)
{
lbl4.Text = ddl.SelectedItem.Text;
lbl4.ForeColor = System.Drawing.Color.FromName(lbl4.Text);
}
}
5. Server.Transfer
Using this technique, form values of one page can be accessed in another page by enabling the second parameter to true.
Also, the same for the Server.Execute method.
The following is a sample of how to use Server.Transfer:
protected void btnPage2_Click(object sender, EventArgs e) {
Server.Transfer("RetrieveData.aspx", true); }
The following is a sample of how to use a Server.Transfer value:
if (Request.Form["ddl1"] != null)
{
lbl5.Text = Request.Form["ddl1"];
}
More methods exist, such as Submit Form, Context.Handler, Static variables, Saving data to XML, Text file to a Database and later retrieve it another form and so on.
Conclusion
Hope you all like it, post your comments also.
Thank you!
Various Ways to Pass Data Among Web Forms
This article shows various ways to pass data among Web Forms.


Datta KharadPosted Nov 26, 2018, 8:17 PM EST
Nice Article and useful
Elahe HajimohamadiPosted Oct 29, 2018, 5:29 AM EDT
Dear Sir, At first Thank you so much for your good website . I want to design a website with asp postfix in C sharp .Net. My program has many buttons ,my program with Javascript code recognize that which button has clicked. now i must to send this information to asp.cs and my collage program that completely wrote with c sharp/windows App. i searched many in Internet but i couldn't find answer. I appreciate you if you help and guide me. Best regards
Sr KarthigaPosted Jul 7, 2016, 2:50 AM EDT
Nice explanation
Sr KarthigaPosted Jul 7, 2016, 2:50 AM EDT
Good one
iSpidersPosted Dec 10, 2013, 9:44 PM EST
Very Nice Article
Kannan NPosted Nov 18, 2013, 5:39 AM EST
I like your article... and very very usefull to me ..
Suthish NairTOP 100Posted Sep 12, 2013, 11:53 PM EDT
@gakenh01, Thanks for pointing out... Even your list are no completed :).. Anyway will update more content into the article...
HyunHo CheaPosted Sep 12, 2013, 4:35 PM EDT
It is very awesome! You are good man.
Gakenh01Posted Sep 12, 2013, 7:59 AM EDT
That's about 1/3 of the ways, here are some others: HTTP Post, HTTP Get, Public Properties, PreviousPage Control Info, HttpContext Items Collection, Cache
Mukesh Kumar TiwariPosted Sep 12, 2013, 12:59 AM EDT
very good article..very simple and useful..
anad aramandlaPosted Jul 1, 2011, 1:38 AM EDT
function getVendorallotFiles($file_date,$account_id=NULL) { $account_id_condition = ($account_id)?' AND avf.account_id = '.$account_id:NULL; $query="SELECT avt.file_id , cca.account_name , avf.file_name , avf.file_size, avf.play_time, vvc.company_name from vendor.vendor_fileallot vvf JOIN vendor.vendor_contacts vvc ON vvf.vendor_id = vvc.vendor_id JOIN admin.voice_filetrans avt ON avt.reference_id=vvf.allot_id JOIN admin.voice_filedetails avf USING(file_id) JOIN client.client_accounts cca ON cca.account_id = avf.account_id WHERE vvf.allot_date = '".$file_date."' ".$account_id_condition." AND avt.thead_id IN (1,2,4) ORDER BY cca.account_name , avf.file_name "; $arr = $this->returnQueryArray($query); return $arr; pls send me the sp for that
Datta KharadPosted May 28, 2011, 8:57 PM EDT
It is very useful article.
Datta KharadPosted May 28, 2011, 8:53 PM EDT
It is very useful article.
shane wozPosted May 26, 2011, 8:28 PM EDT
very good helpfull artical for beginners...
surender bhyanPosted May 25, 2011, 8:27 PM EDT
Thanks a lot suthish.. You did it in very simple way, thanks again :-)
Gobi GPosted Mar 1, 2011, 11:45 PM EST
Good Article...
Rohatash KumarTOP 100Posted Mar 1, 2011, 1:06 AM EST
It's a really good article. Keep it up!
Blocked AccountPosted Feb 28, 2011, 6:55 PM EST
Suthish, Very informative article. Keep posting!
Krishna GaradPosted Feb 27, 2011, 6:43 PM EST
Thank's suthish for sharing it. I was unaware of Server.Transfer and Cross Page posting thank's again.
SapnaTOP 100Posted Feb 27, 2011, 6:07 PM EST
Nice article...