locked
Using the user's preferences in all pages RRS feed

  • Question

  • User-879727125 posted

    I am creating an ASP.Net web site without MVC. My web site is very simple and has no users (logins) and no database. The only way that pages are accessed is when the user navigates using a link (or a postback is done to apply this customization). I want to allow the user to make a small customization for the web site. The current customization consists of a single string about 10 characters in size; not much.

    Selections will be persisted as cookies unless cookies are blocked. When the selection changes, a postback is required to apply it. When the user changes their selection, the selection can be saved as a cookie then a postback done so that the selection can be acted upon by the server. The selection is in the Form collection for the form that changes the preference, but it needs to be accessible to all pages.

    Each page could get the selection from the cookie but that won't work if cookies are blocked, correct? I could use session state except if cookies are blocked then the URL is used to send the session values, correct? I prefer not to do that. Probably a hidden field would be good, assuming the server can automatically include the data in each page. Initially, when the first page is shown, the web site needs to get the customization from the user's cookie if present, right? Then a postback done to apply the change and from then on the hidden field could be used. Then if the selection is changed, another postback is done.

    I have a couple of books about ASP.Net that I am reading. I am not asking for details of how to do this, if I can get general concepts then I will read whatever I need to read to do it.

    Wednesday, March 30, 2016 10:38 PM

All replies

  • User614698185 posted

    Hi Sam Hobbs,

    Cookies are small pieces of text that are passed between browser and web server with every request. As a consequence, their values are available to any page within the site. Cookies are commonly used to store user preferences which help the site remember which features to turn on or off.

    You create or set a cookie like this:

    Response.Cookies["myCookie"].Value = "some cookie value";

    All the time that the user's session continues, you can read the value of the cookie:

    var cookieValue = Request.Cookies["myCookie"].Value;

    Session State provides a mechanism that enables you to tie together requests from the same user for a limited period - the duration of a session. As such, you can store user-related values and retrieve them at any stage during a session. By default, a session begins when the user first visits the site, and lasts for 20 minutes after the last request. Session variables can be any data type from a string to a collection. When the item is stored in session, it is stored as an object data type, so it will need to be cast back to its original data type if there is no implicit cast between object and the original type.

    For more information, please see:

    http://www.mikesdotnetting.com/article/192/transferring-data-between-asp-net-web-pages

    http://www.codeguru.com/columns/dotnet/passing-data-between-pages-in-asp.net.htm

    Best Regards,

    Candice Zhou

    Thursday, March 31, 2016 3:11 AM
  • User-879727125 posted

    Thank you, Candice. I know all that.

    I asked:

    Each page could get the selection from the cookie but that won't work if cookies are blocked, correct?

    Things get complicated when cookies are blocked, correct? Correct me if I am wrong about that, but I think that is what I have read.

    I also asked:

    if cookies are blocked then the URL is used to send the session values, correct?

    I don't want to use session values if they are sent in the URL, even if the user made the choice to block cookies.

    What I am trying to do seems very simple until I try to figure out how to do it.

    Thursday, March 31, 2016 4:17 AM
  • User614698185 posted

    Hi Sam Hobbs,

    Blocking cookies might prevent some webpages from displaying correctly. When you make these sorts of changes, the cookies that are already stored on your computer will not be affected. So, you can try block browser cookies in your computer.

    Best Regards,

    Candice Zhou

    Tuesday, April 5, 2016 10:17 AM