.

Saturday, 14 June 2014

How to Create and Read Cookies In ASP.NET Using C#

6/14/2014 - By Pranav Singh 0

Cookie play very important role in our web application. As we all know that cookies used for storing small information.  So in this article I will show you how you can create an d read the created value of the cookie.


So for this article first we will create a new asp.net application in this add a new page and in page add some button control and labels. After adding all the controls your page will look as shown below.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Cookie.aspx.cs" Inherits="ProjectDemo_Asp.et.Cookie" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to Create and Read Cookies In ASP.NET Using C#</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Create Cookie" OnClick="Button1_Click" />
        &nbsp;<asp:Button ID="Button2" runat="server" Text="Read Cookie Value" OnClick="Button2_Click" />
        <br />
        <asp:Label ID="Label1" runat="server" Text="" Style="color: #FF0066"></asp:Label>
        <br />
        <asp:Label ID="Label2" runat="server" Text="" Style="color: #0000FF"></asp:Label>
    </div>
    </form>
</body>
</html>

Now generate the button click event and add the below code as shown below.

C#.Net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectDemo_Asp.et
{
    public partial class Cookie : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// Assign value to cookie
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Cookies["UserCookieValue"].Value = "Aspdotnet-pools.com";
            Label1.Text = "Cookie Assign Value : Aspdotnet-pools.com";
        }
        /// <summary>
        /// Retrive value to cookie
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button2_Click(object sender, EventArgs e)
        {
            if (Request.Cookies["UserCookieValue"] != null)
            {
                Label2.Text = "Cookie Value : " + Request.Cookies["UserCookieValue"].Value;
            }
            else
            {
                Label2.Text = "Sorry no cookie found.";
            }
        }
    }
}

VB.Net
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace ProjectDemo_Asp.et
    Partial Public Class Cookie
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

        End Sub
        ''' <summary>
        ''' Assign value to cookie
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            Response.Cookies("UserCookieValue").Value = "Aspdotnet-pools.com"
            Label1.Text = "Cookie Assign Value : Aspdotnet-pools.com"
        End Sub
        ''' <summary>
        ''' Retrive value to cookie
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)
            If Request.Cookies("UserCookieValue") <> Nothing Then
                Label2.Text = "Cookie Value : " + Request.Cookies("UserCookieValue").Value
            Else
                Label2.Text = "Sorry no cookie found."
            End If
        End Sub
    End Class
End Namespace

Now view the page in browser. And click on button retrieve cookie value. You will get message that “sorry no cookie found.”



Now check the final output.



 DOWNLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks

Tips and Tricks

About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

Please contact Us

Email Id
: contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.
© 2014
back to top