user3604527
user3604527

Reputation: 21

C# how to retain values after redirecting

So what i want to know is that how do i retain the values after redirecting, where after clicking the back button brings me back to the first page. For example, if i store some values in page 1 then i click submit, which brings me to page 2. But in page 2 i want to click back. How do i retain the values that i have submitted in page 1? Also, what must i write in the btn_Click field? This is my code? What should I change

protected void btnBack_Click(object sender, EventArgs e)
    {
        Server.Transfer("AddStaff.aspx", true);
        Response.Redirect("AddStaff.aspx?" +strValues);
    }

Upvotes: 2

Views: 1763

Answers (1)

Shanaka Rathnayaka
Shanaka Rathnayaka

Reputation: 2682

there are several ways you can retain the value.

  1. Using Cookie
  2. Using Session
  3. Using Query String
  4. Using database

For example let's look at how to set the Cookie

 HttpCookie cookie = new HttpCookie("ValueToSave", "StackOverFlow");
 Response.Cookies.Add(cookie);
 Response.Redirect("~/WebForm2.aspx");

To Access the Cookie you can do as follows on Page_Load

if (Request.Cookies["ValueToStore"] != null)
{
     string tempCookie = Request.Cookies["ValueToStore"].Value;
}

Using session you can achieve it as follows
Save value to Session on Button Click

Session["ValueToStore"] = "StackOverFlow Session";

Retriving the value on Page Load

 if (Session["ValueToStore"] != null)
 {
     string val2 = Session["ValueToStore"].ToString();
 }

Upvotes: 1

Related Questions