0

Follow my previous help in stackoverflow on discussion: Show a div in asp.net on condition

I have an ASP.NET page wiich include a webform in HTML has javascript code set cookie. (I know the javascript is setting the cookies because I can see them in google chrome with the combination of ctrl+shift+I ).

I wrote in the code behind this solution in order to show/not show the form based on the value cooke set or not:

  protected void Page_Load(object sender, EventArgs e)
{

    HttpCookie myCookie = new HttpCookie("tempcookieforclose");
    myCookie = Request.Cookies["tempcookieforclose"];

    // Read the cookie information and display it.
    if (myCookie != null)
        webform.Visible = false;
    else
        webform.Visible = true;

That does not work. As well is not working this exemple that show if cookie name are set in the output:

   HttpCookie myCookie = new HttpCookie("MyTestCookie");
    myCookie = Request.Cookies["MyTestCookie"];

    // Read the cookie information and display it.
    if (myCookie != null)
    Response.Write("<p>"+ myCookie.Name + "<p>"+ myCookie.Value);
       else
     Response.Write("not found");

(I changed MyTestCookie with the name of "mycookie")

as well I post the function the sets cookie for my webform:

        function tempcookie() {
        days = 1; // number of days to keep the cookie
        myDate = new Date();
        myDate.setTime(myDate.getTime() + (days * 24 * 60 * 60 * 1000));
        document.cookie = 'cookieName=cookieclose; expires=' + myDate.toGMTString();


        function permacookie() {
        days = 30; // number of days to keep the cookie
        myDate = new Date();
        myDate.setTime(myDate.getTime() + (days * 24 * 60 * 60 * 1000));
        document.cookie = 'cookieName=cookiesignup; expires=' + myDate.toGMTString();
         }
3
  • 1
    The server side runs before the client side. So you'd have to refresh the page in order for the server side to see the cookies set by the client. Commented Sep 26, 2014 at 17:52
  • Yes, that's why the server check if the cookie are set or not and decide to show/not show the webform. The webform set the cookie. Means that the first time I ever been in the page the webform shows up because the cookie is not set. Through clicking "close" in the webform the cookie gets set and therefore the next time you reload the page the webform should not show up but apparently does not work with my code :) Commented Oct 3, 2014 at 8:12
  • it was right what I have written in the post. Problem was I was using the value of the cookie and not the name of the cookie as a variable. :) Commented Oct 3, 2014 at 15:10

1 Answer 1

1

I did some modification in your code. I hope it will help you.

HttpCookie myCookie = new HttpCookie("tempcookieforclose");
myCookie.Values.Add("date", "1");
Response.Cookies.Add(myCookie);
// Read the cookie information and display it.
myCookie = Request.Cookies["tempcookieforclose"];
if (myCookie != null)
    Response.Write("In If");
else
    Response.Write("In Else");

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.