Umbrcao - Macro - .Net - c# - Create a one time Pop Up in Umbraco using c# to display a message about cookies

Umbrcao - Macro - .Net - c# - Create a one time Pop Up in Umbraco using c# to display a message about cookies


Two create a One time pop up in umbraco to display a message, For example to warn that your site uses cookies, you need to.

1) Create a .Net user Control.
2) Add the .Net user Control to Umbraco Macros
3) Add the Macro to your Main Page Template, so that it displays on ever page.

Here are two methods for doing the one time pop up.  

The first method displays on every page until the user clicks "Click here to remove this message".  Clicking on "More information about cookies" will take them to the a page displaying your Privacy section, but will not remove the message.

The Second method displayed once and the user does not have to click anything. The message will appear on any page the user first lands on and then not be shown again.

Method 1 For Creating a One time Pop up warning about Cookies being used:

1) Create a user control called "WeUseCookies.ascs".

2) Add a Literal to the UserControl called "CookieMonster"

3) Here is the CodeBehind file:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//www.markdownie.me.uk

public partial class usercontrols_WeUseCookies : System.Web.UI.UserControl
{
     protected void Page_Load(object sender, EventArgs e)
     {
 
   //Hide the Message that warns about cookies
   CookieMonster.Visible = false;



   //Check to see if the user has clicked that they have read the message
    string v = Request.QueryString["accept"];
   if (v != null)  // If the user has excepted the message call writecookie function
  {  
     writeCookie();
   }



  //Check to see if the cookie exists
  HttpCookie checkForCookie = new HttpCookie("MyCookiesWarning");
  checkForCookie = Request.Cookies["MyCookiesWarning"];
  
  //If the cookie exists do nothing, else display the message.
   if (checkForCookie != null)
   {
     // Do nothing
     CookieMonster.Text = "";
   }
   else
   { 
    CookieMonster.Visible = true;
    CookieMonster.Text = "<div style=\"width:961px;border:0px;padding:10px;border-style:dashed;border-color:black;margin:auto;margin-bottom:10px;\"<p>This site uses cookies. By continuing to browse the site you agree to our use of cookies. <a href=\"?accept=true\">a) Click here to remove this message</a>. <a href=\"http://www.markdownie.me.uk\">b) More information about cookies</a>.</p>.</div>";

    }
}

   private void writeCookie()
   {
    HttpCookie MyCookie = new HttpCookie("MyCookiesWarning");
    DateTime now = DateTime.Now;
    MyCookie.Value = now.ToString();
     Response.Cookies.Add(MyCookie);
    }

}


Method 2 For Creating a One time Pop up warning about Cookies being used:

1) Create a user control called "WeUseCookies.ascs".

2) Add a Literal to the UserControl called "CookieMonster"

3) Here is the CodeBehind file:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//www.markdownie.me.uk

public partial class usercontrols_WeUseCookies : System.Web.UI.UserControl
{
     protected void Page_Load(object sender, EventArgs e)
    {
       // Hide the Literal and
        CookieMonster.Visible = false;

       // Check to see if the Cookie already exists
        HttpCookie checkForCookie = new HttpCookie("CookiesWarning");
        checkForCookie = Request.Cookies["CookiesWarning"];





      // If the cookie already exists, do nothing.  Else display a warning and and write the cookie.
      if (checkForCookie != null)
     {
        // Do nothing
       LabelCookieMonster.Text = "";
     }
    else
    {
    CookieMonster.Visible = true;
    CookieMonster.Text = "<div style=\"width:961px;border:0px;padding:10px;border-style:dashed;border-color:black;margin:auto;margin-bottom:10px;\"<p>This site uses cookies. By continuing to browse the site you agree to our use of cookies. <a href=\ "http://www.markdownie.me.uk\">Find out more here.</a><br /></p></div>";
   writeCookie(); 
   }
}




private void writeCookie()
 {
    HttpCookie myCookie = new HttpCookie("CookiesWarning");
    DateTime now = DateTime.Now;
    myCookie.Value = now.ToString();
   Response.Cookies.Add(myCookie);
  }
}


You may also want to set an expiry on your Cookie as the cookies in the code above get deleted everytime the browser closes. Just change the writeCookie function to:

private void writeCookie()
{
       HttpCookie myCookie = new HttpCookie("CookiesWarning");
        DateTime now = DateTime.Now;
        myCookie.Value = now.ToString();

         NESCookie.Expires = DateTime.Now.AddDays(50);
         Response.Cookies.Add(myCookie);
}



If you experience any issues with this code, please let me know....




Comments

Popular posts from this blog

Umbraco - Reset the Admin Password via SQL Server

Create a .NET Contact Form that Gets the Last Url Visited in C# Can also be Used in Umbraco

Umbraco Macro for IFrame when Embedding Video, Vimeo Universal Player, or other types of IFrame on to a Page XSLT version and Razor Version