Jaquarh
Jaquarh

Reputation: 6673

C# / .NET / ASPX : Adding Cookies

In my Page_Load function, I have this:

 var output = new StringBuilder();
        output.Append("<div class='search-recent'> <ul>");
        var str = Response.Cookies["UserSettings"].Value;
        {
            try
            {
                string[] tokens = str.Split(':');
                foreach (String searchHist in tokens)
                {
                    output.Append("<li>" + searchHist + "</li>");
                }
            }
            catch (Exception ex)
            {
                output.Append("<li>" + str + "</li>");
            }
            finally
            {
                output.Append("</div>");
                recentSearch.Text = output.ToString();
            }
        }

In my button function I have this:

if (Response.Cookies["UserSettings"].Value != null)
        {
            var oldCookieVal = Response.Cookies["UserSettings"].Value;
            HttpCookie cookie = new HttpCookie("UserSettings");
            cookie.Value = oldCookieVal + ":" + sinput;
            cookie.Expires = DateTime.Now.AddHours(3);
            Response.SetCookie(cookie);
        }
        else
        {
            HttpCookie cookie = new HttpCookie("UserSettings");
            cookie.Value = sinput;
            cookie.Expires = DateTime.Now.AddHours(3);
            Response.SetCookie(cookie);
        }

For some reason, the Cookie is always overwritten even when one exists before. Is there anyway to stop it from overwriting? Thank-you.

Reference to research: http://asp.net-tutorials.com/state/cookies/

Edit: It appears that the .Value is not returning anything, what could I use to fix this?

Big Edit: I replaced the .Value to ToString(); and it now does not overwrite. However, the cookie value is System.Web.HttpCookie when it is sent to the front end... Any suggestions?

Front end view: https://gyazo.com/9c08160c6bca6cf5b335c0fc3861d0cb (It is all test data)

Upvotes: 4

Views: 2590

Answers (2)

Jaquarh
Jaquarh

Reputation: 6673

Ok, so after a lot of trial and error I found that the .Value is only used when you're setting or getting the value.

When you want to CHECK the value exists, you remove the .Value. When you want to SET or GET the value, you use .Value

Example -

Setting the cookie (not setting or getting during the check)

string oldCookieVal = Request.Cookies["UserSettings"].Value;
        string sinput = searchInput.Text;
        if (Response.Cookies["UserSettings"] != null)
        {
            HttpCookie cookie = new HttpCookie("UserSettings");
            cookie.Value = oldCookieVal + ":" + sinput;
            cookie.Expires = DateTime.Now.AddHours(3);
            Response.SetCookie(cookie);
        }
        else
        {
            HttpCookie cookie = new HttpCookie("UserSettings");
            cookie.Value = sinput;
            cookie.Expires = DateTime.Now.AddHours(3);
            Response.SetCookie(cookie);
        }

Example -

Getting the Value and using it

 var output = new StringBuilder();
        string userSettings = Request.Cookies["UserSettings"].Value;
        output.Append("<div class='search-recent'> <ul>");
        {
            try
            {
                string[] tokens = userSettings.Split(':');
                foreach (String searchHist in tokens)
                {
                    output.Append("<li>" + searchHist + "</li>");
                }
            }
            catch (Exception ex)
            {
                output.Append("<li>" + userSettings + "</li>");
            }
            finally
            {
                output.Append("</div>");
                recentSearch.Text = output.ToString();
            }

Remember to use Literal's (controls) when you're working with transferring data from back-end to front-end

Upvotes: 0

John Riehl
John Riehl

Reputation: 1330

When you want to get the value of the cookie as it is in the client, you need to access Request.Cookies. To set the value, use Response.Cookies.

Upvotes: 6

Related Questions