简体   繁体   中英

How to go back to previous page and preserve the previous page data in c#

I am working on a Retail Website which is developed in Asp.net 3.0. I have a cart system so when user add some items to their cart they are being redirect to Basket page. And i have a button on Basket page for them to go back add some more items. i am not sure how to do that.

here is the case.

Home Page -> Mens Clothing Product List -> Jeans Page -> Basket pgae

Now when users go to Mens Clothing they see all the items, then they select one Jeans they want to buy and they are redirected to Jeans Page. Now they add items to their basket and they are redirected to Basket Page, Now i have a button on Basket page called "Keep Shopping", When they click (as of now they are redirected to Home page) BUT i need them to redirect to Mens Clothing Product List instead of Home Page.

CASE 1

I have googled it and found out that we can use URLReferrer and check it if it is null or not. BUT in my case it is always Basket Page URL as i am using Response.Redirect on Jeans Page. So i can not use URLReferrer.

CASE 2

I have also found out that using Javascript History we can go back to 1 or 2 page. BUT as i want to preserve the user data and CART i can not use that as well because its gonna use the browser cache and will not save any users data.

CASE 3

I was thinking of using the Context Or Session to store the current page URL on Jeans page and i can check it in Basket Page, if it not null then redirect to that page. BUT i want to go back one more step ie. Mens Clothing page not Jeans Page.

I am really not sure whether this will be a good idea or not. Please suggest.

Thanks...

Use Response.Redirect to go the required page and use session store the Basketdata.

For redirecting use:

protected void btn_click(object sender, EventArgs e)
{
    Response.Redirect("desired page.aspx");
}

For storing use dataset or datatable and store it in ssession.

    Session["items"] = ur_DataTable as DataTable;

     or

     Session["items"] = ur_DataSet as DataSet;

ur_Datatable or ur_DataSet will contain the product which you have added.

To render the another page and save its data to access in current page ,do as below

protected void button_Click(Object sender,EventArgs e )
{
   session["give your prefered Name to session"]=your value.its whatever
   response.redirect("your Page URL to Redirect from this page to that page");
}

Now access the value that you have stored in session on another page. Do as below bye default value return by session are of "Object" type, so you can convert in whatever data type using explicit conversion

Here I am converting object value in string,

protected void Page_Load(object sender, EventArgs e)
{
    string value=session["Use Same Name as u have given to it"].ToString();
}

One approach to simple state management in asp.net is to use a hidden field. Use a hidden field to store and pass a location to the basket page.

Now when users go to "Mens Clothing" they see all the items, then they select one "Jeans" they want to buy and they are redirected to "Jeans" Page. Now they add items to their basket and they are redirected to "Basket" Page.

Now I have a button on "Basket" page called "Keep Shopping". When they click I need to redirect them to Mens Clothing Product List instead of Home Page.

Now you say you want to redirect to the "Mens Clothing" page, but you have to determine why you want to redirect to that page. If it always is that page, then hardcode that:

Response.Redirect("Shop/Categories/Mens-Clothing");

Though when the page you want to return to depends on what item was added to the basket, you need to realize what page you want to redirect to and how the relation of that page is with regard to the item.

The Basket page knows what item is being added to the basket. If you for example want to redirect to the category page for that item, look up the category this item is in, and redirect to that page:

Response.Redirect("Shop/Categories/" + item.Category.Name);

Although this will redirect you to the "Jeans" page, because that is the choosen item's category.

Now it looks like you want to redirect to the parent category of the category the item is in. Then you user that:

Response.Redirect("Shop/Categories/" + item.Category.ParentCategory.Name);

Or whatever your data model looks like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM