Skip to main content

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

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

This is a Simple Contact From done in C# .Net and also can be used in Umbraco.  Paste the code in to Visual Web Developer for a better view. The important bits are coment in the second file.  One of the Commented out lines highlights how to get the mailto address from an Umbraco field. The code Below includes the myContactForm.ascx.cs codebehind file and the myContactForm.ascx file. 1) The myContactForm.ascx file:  <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ContactFormforPublications.ascx.cs" Inherits="usercontrols_ContactFormPublications" %> <div class="contact-form" id="contactForm" runat="server">     <h3>Request a Publication</h3>     <div class='int'>         <form runat="server">         <asp:Panel DefaultButton="contactFormSubmit" runat="server">         <fieldset>             <div class='

Umbraco 7 - C# Razor - Passing a parameter variable from a Template to a Partial View.

Passing a parameter variable from a Template to a Partial View Example - Passing a Page Id to a Partial View. In the example below we are passing 3 page Ids (1090. 1091, 1092) to a partial view.  The Partial View will display the details for each of these nodes on a home page.  See final output below. 1) In your Template: @Html.Partial("HomePageLinksToLandingPages", new ViewDataDictionary{{ "pageId", 1090}}) @Html.Partial("HomePageLinksToLandingPages", new ViewDataDictionary{{ "pageId", 1091}}) @Html.Partial("HomePageLinksToLandingPages", new ViewDataDictionary{{ "pageId", 1092}}) 2) The Partial View: @inherits Umbraco.Web.Mvc.UmbracoTemplatePage @{ var thisPageId = 1090; var stringPage = @ViewData["pageId"]; thisPageId = Convert.ToInt32(stringPage); } <div class="col-md-4"> <arti