Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 12

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 cookie example: how to use cookie in asp.net

<%@ 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 favoriteColor = Request.Cookies["FavoriteColor"];
if (favoriteColor == null)
{
HttpCookie userCookie = new HttpCookie("FavoriteColor");
userCookie["Name"] = "Jones";
userCookie["Color"] = "Crimson";
userCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(userCookie);
Label1.Text = "Cookie created at: " +
DateTime.Now.ToString()+ "<br /><br />";

}
else
{
string name = favoriteColor["Name"];
string color = favoriteColor["Color"];
Label1.Text += "Cookie found and read<br />";
Label1.Text += "Hi " + name + " your favorite Color: " +
color;
}

}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>asp.net cookie example: how to use cookie in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Navy">asp.net cookie: how to use</h2>
<asp:Label ID="Label1" runat="server" Font-Size="Large"
ForeColor="SeaGreen">
</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 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 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 Redirect example: how to redirect a page to another page in asp.net

<%@ 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)
{
string url = "Image.aspx?";
url += "ID=" + ListBox1.SelectedValue.ToString() + "&";
url += "Name=" + Server.UrlEncode(ListBox1.SelectedItem.Text);
Response.Redirect(url);

}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net Redirect example: how to redirect a page to another
page in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Red">asp.net Redirect example</h2>
<asp:Label
ID="Label1"
runat="server"
Text="Select an Item for view Image"
Font-Bold="true"
ForeColor="SeaGreen"
>
</asp:Label>
<br />
<asp:ListBox
ID="ListBox1"
runat="server"
ForeColor="AliceBlue"
BackColor="Crimson"
>
<asp:ListItem Value="1">Avatar</asp:ListItem>
<asp:ListItem Value="2">Flower</asp:ListItem>
<asp:ListItem Value="3">Bird</asp:ListItem>
<asp:ListItem Value="4">Elephant</asp:ListItem>
<asp:ListItem Value="5">Fish</asp:ListItem>
<asp:ListItem Value="6">Forest</asp:ListItem>
</asp:ListBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1"
runat="server"
ControlToValidate="ListBox1"
Text="*"
>
</asp:RequiredFieldValidator>
<br />
<asp:Button
ID="Button1"
runat="server"
OnClick="Button1_Click"
Text="Show Image"
Font-Bold="true"
ForeColor="HotPink"
/>
</div>
</form>
</body>
</html>

asp.net session example: how to use session in asp.net

<%@ 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) {
Session["Name"] = TextBox1.Text.ToString();
Session["Color"] = TextBox2.Text.ToString();
Label1.Text = "Session saved!";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>asp.net session example: how to use session in
asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Red">asp.net session example</h2>
<asp:Label ID="Label1" runat="server" ForeColor="Crimson">
</asp:Label>
<br /><br />
<asp:Label ID="Label2" runat="server" Text="Name">
</asp:Label>
<asp:TextBox ID="TextBox1" runat="server">
</asp:TextBox>
<br />
<asp:Label ID="Label3" runat="server" Text="Favorite Color">
</asp:Label>
<asp:TextBox ID="TextBox2" runat="server">
</asp:TextBox>
<br />
<asp:Button
ID="Button1"
runat="server"
Font-Bold="true"
ForeColor="DodgerBlue"
Text="Save Into Session"
OnClick="Button1_Click"
/>
<br /><br />
<asp:HyperLink
ID="HyperLink1"
runat="server"
Text="Go for session test"
NavigateUrl="~/SessionTest.aspx">
</asp:HyperLink>
</div>
</form>
</body>
</html>

<%@ 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 Page_Load(object sender, System.EventArgs e) {
label1.Text = "Hi " + Session["Name"];
label1.Text += "!<br />Your favorite color is " +
Session["Color"];
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Navy">asp.net session example</h2>
<asp:Label
ID="label1"
runat="server"
Font-Size="Larger"
ForeColor="SeaGreen"
>
</asp:Label>
<br /><br />
<asp:HyperLink
ID="HyperLink1"
runat="server"
Text="Go session page"
NavigateUrl="~/Session.aspx">
</asp:HyperLink>

</div>
</form>
</body>
</html>

asp.net UrlEncode method example: how to encode an url in asp.net

UrlEncode.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)
{
string url = "UrlEncodeTest.aspx?";
url += "ID=" + ListBox1.SelectedValue.ToString()+ "&";
url += "Name=" + Server.UrlEncode(ListBox1.SelectedItem.Text);
Response.Redirect(url);

}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>asp.net UrlEncode method example: how to encode an url in
asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Red">asp.net UrlEncode() method example</h2>
<asp:Label
ID="Label1"
runat="server"
Text="Select an employee for view profile"
Font-Bold="true"
ForeColor="Green"
>
</asp:Label>
<br />
<asp:ListBox
ID="ListBox1"
runat="server"
ForeColor="AliceBlue"
BackColor="DodgerBlue"
>
<asp:ListItem Value="1">Jenny Jones</asp:ListItem>
<asp:ListItem Value="2">John Smith</asp:ListItem>
<asp:ListItem Value="3">Ben Forta</asp:ListItem>
<asp:ListItem Value="4">Raymond Camden</asp:ListItem>
<asp:ListItem Value="5">Little John</asp:ListItem>
</asp:ListBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1"
runat="server"
ControlToValidate="ListBox1"
Text="*"
>
</asp:RequiredFieldValidator>
<br />
<asp:Button
ID="Button1"
runat="server"
OnClick="Button1_Click"
Text="Show Profile"
Font-Bold="true"
ForeColor="Green"
/>
</div>
</form>
</body>
</html>

UrlEncodeTest.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 Page_Load(object sender, System.EventArgs e) {
Label1.Text = "Hi This is " + Request.QueryString["Name"];
Label1.Text += "<br />My employee ID is: " +
Request.QueryString["ID"];
string imageSource = "~/Images/" + Request.QueryString["ID"] +
".jpg";
Image1.ImageUrl = imageSource;
Image1.BorderWidth = 2;
Image1.BorderColor = System.Drawing.Color.SaddleBrown;
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Red">asp.net UrlEncode example</h2>
<asp:Label ID="Label1" runat="server" Font-Size="Large"
ForeColor="DodgerBlue">
</asp:Label>
<br /><br />
<asp:Image ID="Image1" runat="server" />
</div>
</form>
</body>
</html>

asp.net application state example: how to use application state in asp.net

ApplicationState.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) {
int counter = 0;
if(Application["ButtonClickCounter"] !=null)
{
counter = (int)Application["ButtonClickCounter"];
}
counter++;
Application["ButtonClickCounter"] = counter;
Label1.Text = "Button Clicked: " + counter.ToString() + "
times";
}
</script>

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

You might also like