In this asp.net tutorial, we will discuss how to manage cookies in client machine in asp.net using JavaScript.
Manage cookies in client machine in Asp.NET
Here I have a dialog box that is appearing in the page load. But when users click on the Acknowledge button, it will not appear next time till the end of the cookies expire date.
So here I have a simple HTML code to create a Dialog box that will appear in the client computer when visiting the site.
<body>
<form id="form1" runat="server">
<div id="dialog" title="Critial Alert">
<p>Your computer might restart at any time and IT is not responsible in case of any data loss..</p>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" style="background-color: forestgreen; color: white;" onclick="CheckAcknowledge();">Acknowledge </button>
<button type="button" class="btn btn-default" data-dismiss="modal" style="background-color: red; color: white;" onclick="hide();">Remind me later </button>
</div>
</div>
</form>
</body>
In the above screenshot, when users click on Acknowledge, It will hide from the page for a particular user. So I set the cookies in button click but before that, I am checking, if cookies already exist, then I hide the dialog, if it doesn’t exist then it will show to the user.
$(function () {
var AlertID = getCookie("notification-id");
if (AlertID != "") {
document.getElementById('dialog').style.display = "none";
}
else {
$("#dialog").dialog();
}
});
Below is the code to get the cookies in asp.net.
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
In above code, “notification-id” is my cookies name which i am storing in client browser.
Next when client click on Acknowledge button, I set the cookies in client browser with expire date.
function CheckAcknowledge() {
setCookie("notification-id", "acknowledged", 30);
$('#dialog').dialog('close');
}
Below is the method to set the cookies in Asp.Net.
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
Below is the code to modify cookies in Asp.Net.
function UpdateCookie() {
var now = new Date();
now.setMonth(now.getMonth() - 1);
cookievalue = "1";
document.cookie = "notification-id=" + cookievalue;
document.cookie = "expires=" + now.toUTCString() + ";"
document.write("Setting Cookies : " + "notification-id=" + cookievalue);
}
When I click on the Acknowledge button, you can see the below screenshot where cookies is already set in the browser.
You can see, when I refresh the page next time, it is verifying the cookies, if it already exists then the dialog box will hide for the user.
When cookies is not exist in the client browser then, it will appear like below:
You may like following asp.net tutorials:
- Download Asp.Net Books PDF Free
- How to create a folder if not exist in C#.Net
- Create a console application in visual studio code
- Generate One Time Password (OTP) in Asp.Net using C#.Net
- How to create a SOAP API request with Username Token in .Net
- How to create a GUID in C#.Net
In this asp.net tutorial, we discussed how to manage cookies in client machine in asp.net using JavaScript. Specially, we checked how to set, get and modify cookies in asp.net.
Rajkiran is currently working as a SharePoint Consultant in India . Rajkiran having 7+ years of experience in Microsoft Technologies such as SharePoint 2019/2016/2013/2010, MOSS 2007,WSS 3.0, Migration, Asp.Net, C#.Net, Sql Server, Ajax, jQuery etc.He is C#Corner MVP (2 Times).