Skip to content Skip to sidebar Skip to footer

How Can I Write And Read Bool Values Into/from Cookies?

I want to save the state of checkboxes in an html form on submit to cookies, and then enable/disable other parts of my site based on those cookie values. I've started out with code

Solution 1:

Cookies are strings. You'll need to convert the cookie value to the type you want. The boolean values are being saved as true or false because that's the string representation of a boolean.

var selectTwitterCookie = Request.Cookies["selectTwitter"];
bool selectTwitter = false;

if(selectTwitterCookie != null)
{
    bool.TryParse(selectTwitterCookie, out selectTwitter);
}

Alternatively, you could use Convert.ToBoolean(selectTwitterCookie).

Post a Comment for "How Can I Write And Read Bool Values Into/from Cookies?"