Cookie only displayed on refresh?

前端 未结 2 1867
深忆病人
深忆病人 2021-01-26 12:45

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

相关标签:
2条回答
  • 2021-01-26 13:31

    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);
    )
    
    0 讨论(0)
  • 2021-01-26 13:37

    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.

    0 讨论(0)
提交回复
热议问题