this example shows the ways to pass data from one page to another page. 1.Passing values through context:
//Page1.aspx stores value in context before transferring Context.Items("UserName") = txtName.Text; Server.Transfer("Page2.aspx"); //Page2.aspx retrieves the value from Page1’s context string sName; sName = Context.Items("UserName").ToString; Response.Write("Your name is " + sName);
2.passing value through session:-
//Page1.aspx stores value in Session on button click Session["name"]=TextBox1.Text; Session["email"]=TextBox2.Text; Response.Redirect("Page2.aspx");
Page2.aspx retrieves the value from sessin Page1’s load
Label1.Text=Session["name"].ToString(); Label2.Text=Session["email"].ToString();
3.passing value through Querysting:- Page1.aspx on button click
private void Button1_Click (object sender, System.EventArgs e) { string url; url="Page2.aspx?name=" + TextBox1.Text + "&email=" + TextBox2.Text; Response.Redirect(url); }
Page2.aspx retrieves the value on Page1’s load
private void Page_Load (object sender, System.EventArgs e) { Label1.Text=Request.QueryString["name"]; Label2.Text=Request.QueryString["email"]; }
4.passing value through Cookies:- Page1.aspx on button click
protected void Button1_Click(object sender, EventArgs e) { HttpCookie cookie = new HttpCookie("UserName"); cookie.Value = TextBox1.Text; cookie.Expires = DateTime.Now.AddDays(1); Response.Cookies.Add(cookie); Response.Redirect("Page2.aspx"); }
Page2.aspx retrieves the value on Page1’s load
protected void Page_Load(object sender, EventArgs e) { if (Request.Cookies["UserName"] != null) Response.Write(Request.Cookies["UserName"].Value); }
5.How to use PreviousPage:- paste this code on page1.aspx
add a new page "page2.aspx" and go to page2.aspx.cs and paste this code on : TextBox1.Text = Request.Form["TextBox1"].ToString(); TextBox1.Text = Request.Form["TextBox1"].ToString();
Or paste this code:
TextBox textBoxTemp = (TextBox) PreviousPage.FindControl("TextBox1"); TextBox1.Text = textBoxTemp.Text;
important :be sure click on the button after inserting text into the textbox.
6.You can also pass the data between pages with database. Thanks