Home

Thursday, 14 July 2016

Working with Cookies using C# Asp.net

Step 1- Create UI where user give input name is WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Cokkies.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        Name<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
&nbsp;Email
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <br />
        <asp:Label ID="Label2" runat="server" Text="msg"></asp:Label>    
    </div>
    </form>
</body>
</html>
Step 2-Write below code in WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Cokkies
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Browser.Cookies)
                {
                    if (Request.QueryString["Cookiecheck"] == null)
                    {
                        HttpCookie cookies = new HttpCookie("Test", "1");
                        Response.Cookies.Add(cookies);
                        Response.Redirect("~/WebForm1.aspx?Cookiecheck=1");
                    }

                    else
                    {
                        HttpCookie cookie = Response.Cookies["Test"];
                        if (cookie == null)
                        {

                            Label2.Text = "We Detected your browser cookie are disable please enable cookie";
                        }
                    }
                }
                else
                {
                    Response.Write("Not Support");
                }
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            HttpCookie _cookie = new HttpCookie("UserInfo");
            _cookie["Name"] = TextBox1.Text;
            _cookie["Email"] = TextBox2.Text;
            //_cookie.Expires.AddDays(10);
            Response.Cookies.Add(_cookie);
            Response.Redirect("~/WebForm2.aspx");
        }
    }
}
Step 3-Create another aspx page where user seen given value in WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Cokkies.WebForm2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        Name=
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Email =<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>    
    </div>
    </form>
</body>
</html>
Step 3-Add below code in WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Cokkies
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie _cookie = Request.Cookies["UserInfo"];
            if (_cookie != null)
            {
                Label1.Text = _cookie["Name"];
                Label2.Text = _cookie["Email"];
            }
        }
    }

No comments:

Post a Comment