I have some trouble understanding this one so here it is.
I\'m trying to set a cookie and display the value on the page using ASP.NET + C#.
he
The first time, the request has no cookies (yet); it will only have them the second time around, after the response has set them. So your code has to deal with the possibility that Request.Cookies
just may not have a "fontSize"
entry, and provide the proper default when that is the case. For example:
HttpCookie cookie = Request.Cookies.Get("fontSize");
// Check if cookie exists in the current request.
if (cookie == null)
{
Response.Write( "Defaulting to 'small'.");
}
else
{
Response.Write( Request.Cookies["fontSize"].Value);
)
The lblChangeToSmall_Click
event is fired after the Page_Load
event. Therefore the cookie write won't be available on the Request until the subsequent postback.
It will be avaialable on the client immediately though.