Hello... This might be an easy question for you experienced .Net programmers, but, I am scratching my head over this one. Let me explain my situation.
We are trying to create some specialized product pages to walk a customer through building a custom swimming pool. So, for example, in Step 1, you would be choosing a pool size. Step 2, choose a Liner. Step 3, a pump/filter system... etc., etc....
In order to make this work like a kit, we have create a category for each pool, and then sunk the appropriate categories and products that match that pool type in that category. As such, I am able to build out the pages fairly easily to jump from option to option throughout the process.
Here is where I am having a problem...
If I add each item to the customer cart after each step, I can get this whole process to work no problem. My boss, however, would rather have nothing added to the cart, until after the last step is completed, and the customer chooses to add the entire thing to the cart. I am, of course, able to make an new instance of a Basket and populate that, but, I am uncertain how to keep that basket from re-initializing upon each server call.
Any ideas for me? I guess I'm unsure (in general) how to keep any variable current through a server call where the page is reloaded (or partially loaded).
Sorry if this is an obvious question. I would be most appreciative of any help anyone could give me with this!
Thanks,
Scott
Temporary cart
Re: Temporary cart
Make a singleton class:
So you get hold of the object via "TemporaryBasket mybasket = TemporaryBasket.Current;" and you can add in any extra properties and methods you might need in the class. Session is not the most stable place to store things, but should be adequate for your purpose.
Cheers,
Code: Select all
public class TemporaryBasket
{
public static TemporaryBasket Current
{
get
{
return (Session["TemporaryBasket"] ?? (Session["TemporaryBasket"] = new Basket())) as TemporaryBasket;
}
}
...
Cheers,
Nick Cole
http://www.ethofy.com
http://www.ethofy.com
Re: Temporary cart
You can place a basketid in the query string let say it TempBasketId. Then first try to load the basket using the TempBasketId from query string. In the first step the TempBasketId will be 0 so there will be no basket to load hence you will create a new one, for each next step you will have a valid TempBasketId and all you need to just load the basket and append the next basket item to it.
Re: Temporary cart
Thank you both for your speedy replies! I will get started trying it out right now.
Your posts were both very helpful!!!
Thanks again,
Scott
Your posts were both very helpful!!!
Thanks again,
Scott
Re: Temporary cart
Hey, I just thought I would post this solution in case it is helpful to anyone...
I've found that when trying to pass querystrings on elements of a page that are updated with AJAX, it's helpful to set the querystring, and not redirect manually. As such, .net won't let you set a querystring, as the collection is read-only by default. To get around this, use this code to temporarily set the collection to writeable.
After this, you should be able to add whatever you need to the querystring, and have it then available after the AJAX part of the page reloads.
This solution made it possible to keep the id of my temporary basket as well as the step in the process that the customer is on without reloading the entire page each time.
Thanks again for your help Mazhar and NickC.
Scott
I've found that when trying to pass querystrings on elements of a page that are updated with AJAX, it's helpful to set the querystring, and not redirect manually. As such, .net won't let you set a querystring, as the collection is read-only by default. To get around this, use this code to temporarily set the collection to writeable.
Code: Select all
NameValueCollection QS = Request.QueryString;
QS = (NameValueCollection)Request.GetType().GetField("_queryString", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(Request);
PropertyInfo readOnlyInfo = QS.GetType().GetProperty("IsReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
readOnlyInfo.SetValue(QS, false, null);
Code: Select all
QS["BasketId"] = _Basket.BasketId.ToString();
readOnlyInfo.SetValue(QS, true, null);
Thanks again for your help Mazhar and NickC.
Scott
Re: Temporary cart
Thanks Scott, great tip!