Showing posts with label application state. Show all posts
Showing posts with label application state. Show all posts

Dec 21, 2009

asp.net cookie example: how to set value in a cookie


<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) {
        if (!this.IsPostBack) {
            HttpCookie testCokkie = new HttpCookie("ExampleCookie");
            testCokkie.Expires = DateTime.Now.AddDays(5);
            Response.Cookies.Add(testCokkie);
        }
    }

    protected void Button1_Click(object sender, System.EventArgs e) {
        HttpCookie ExampleCookie = Request.Cookies["ExampleCookie"];
        ExampleCookie["Name"] = TextBox1.Text.ToString();
        ExampleCookie["City"] = TextBox2.Text.ToString();
        Response.Cookies.Add(ExampleCookie);
    }

    protected void Button2_Click(object sender, System.EventArgs e) {
        HttpCookie exampleCookie = Request.Cookies["ExampleCookie"];
        if (exampleCookie != null) {
            string name = exampleCookie["Name"];
            string city = exampleCookie["City"];

            label1.Text = "Cookie found and read<br />";
            label1.Text += "Name: " + name + "<br />City: " + city;
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>asp.net cookie example: how to set value in a cookie</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">asp.net cookie example: set value</h2>
        <asp:Label 
            ID="label1" 
            runat="server" 
            Font-Size="Large" 
            ForeColor="SeaGreen"
            Font-Italic="true"
            >
        </asp:Label>
        <br /><br />
        <asp:Label ID="Label2" runat="server" Text="Name">
        </asp:Label>
        <asp:TextBox ID="TextBox1" runat="server" BackColor="LightGreen">
        </asp:TextBox>
        <br />
        <asp:Label ID="Label3" runat="server" Text="City">
        </asp:Label>
        <asp:TextBox ID="TextBox2" runat="server" BackColor="LightGreen">
        </asp:TextBox>
        <br />
        <asp:Button 
            ID="Button1" 
            runat="server" 
            Text="Set cookie value" 
            OnClick="Button1_Click"
            Font-Bold="true"
            ForeColor="Crimson"
            />
        <asp:Button 
            ID="Button2" 
            runat="server" 
            Text="Read cookie" 
            OnClick="Button2_Click"
            Font-Bold="true"
            ForeColor="DarkGreen"
            />
    </div>
    </form>
</body>
</html>













asp.net cookie example: how to set expires date time

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) {
        HttpCookie cookie = new HttpCookie("ColorCookie");
        cookie["Color"] = "Crimson";
        cookie["ColorValue"] = "#DC143C";
        cookie.Expires = DateTime.Now.AddYears(1);
        Response.Cookies.Add(cookie);
        Label1.Text = "Cookie created successfully and set expire after 1 year";

        HttpCookie myCookie = Request.Cookies["ColorCookie"];
        if (myCookie != null)
        {
            string color = myCookie["Color"];
            string colorValue = myCookie["ColorValue"];
            Label1.Text += "<br /><br />Cookie[ColorCookie] Found and read<br/>";
            Label1.Text += "Color: " + color;
            Label1.Text += "<br />Value: " + colorValue;
        }
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>asp.net cookie example: how to set expires date time</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Teal">asp.net Cookie example: set cookie expires</h2>
        <asp:Label 
            ID="Label1" 
            runat="server" 
            Font-Size="Large" 
            ForeColor="Crimson"
            Font-Italic="true"
            Font-Bold="true"
            >
        </asp:Label>
    </div>
    </form>
</body>
</html>
   
   
   
   

   

asp.net cookie example: how to read a cookie

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) {
        if (!this.IsPostBack) 
        {
            HttpCookie infoCookie = new HttpCookie("Info");
            infoCookie["Country"] = "USA";
            infoCookie["City"] = "NewYork";
            infoCookie["Name"] = "Jenny";
            infoCookie.Expires = DateTime.Now.AddDays(7);
            Response.Cookies.Add(infoCookie);
        }
        
    }

    protected void Button1_Click(object sender, System.EventArgs e) {
        HttpCookie cookie = Request.Cookies["Info"];

        if (cookie != null)
        {
            string country = cookie["Country"];
            string city = cookie["City"];
            string name = cookie["Name"];
            Label1.Text = "Cookie[Info] Found and read<br/>";
            Label1.Text += "Name: " + name;
            Label1.Text += "<br />Country: " + country;
            Label1.Text += "<br />City: " + city;
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>asp.net cookie example: how to read a cookie</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Teal">asp.net Cookie example: Read a cookie</h2>
        <asp:Label 
            ID="Label1" 
            runat="server" 
            Font-Size="Large" 
            ForeColor="DarkGreen">
        </asp:Label>
        <br /><br />
        <asp:Button 
            ID="Button1" 
            runat="server" 
            Text="Read Cookie" 
            OnClick="Button1_Click"
            Font-Bold="true"
            ForeColor="HotPink"
             />
    </div>
    </form>
</body>

</html>
 
 
 

 









asp.net cookie example: how to create a cookie

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) {
        HttpCookie userCookie = new HttpCookie("UserInfo");
        userCookie["Country"] = "Italy";
        userCookie["City"] = "Rome";
        userCookie["Name"] = "Jones";
        userCookie.Expires = DateTime.Now.AddDays(3);
        Response.Cookies.Add(userCookie);
        Label1.Text = "Cookie create successful!<br><br/>";
        
        HttpCookie cookie = Request.Cookies["UserInfo"];
        
        if (cookie != null) 
        {
            string country = cookie["Country"];
            string city = cookie["City"];
            string name = cookie["Name"];
            Label2.Text = "Cookie Found and read<br/>";
            Label2.Text += "Name: " + name;
            Label2.Text += "<br />Country: " + country;
            Label2.Text += "<br />City: " + city;
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>asp.net cookie example: how to create a cookie</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Teal">asp.net Cookie example: Create a cookie</h2>
        <asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="SeaGreen">
        </asp:Label>
        <asp:Label ID="Label2" runat="server" Font-Size="Large" ForeColor="Crimson">
        </asp:Label>
    </div>
    </form>
</body>
</html>
   
   
   
   

   

asp.net application state example: how to lock and unlock application state in asp.net

ApplicationStateLockUnLock.aspx
 
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e) {
        Application.Lock();
        
        int clickCounter = 0;
        if(Application["ClickCounter"] !=null)
        {
            clickCounter = (int)Application["ClickCounter"];
        }
        clickCounter++;
        Application["ClickCounter"] = clickCounter;
        Application.UnLock();

        Label1.Text = "Button Clicked: " + clickCounter.ToString() + " times";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>asp.net application state example: how to lock and unlock application state in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">asp.net application state example: Lock and UnLock</h2>
        <asp:Label 
            ID="Label1" 
            runat="server" 
            Font-Size="Large" 
            ForeColor="DodgerBlue"
            >
        </asp:Label>
        <br /><br />
        <asp:Button 
            ID="Button1" 
            runat="server" 
            Text="Show Button Click Status"
            OnClick="Button1_Click"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>


</html>