ASP.NET

1. ASP.NET Introduction 2. Comp of .Net Framework 3.5 3. Sys.Req for VS 2008 4. ASP.NET Envrmnt Setup 5. VS2010 Ultimate Sys.Req 6. Installing VS 2010 7. VS 2012 Sys.Req 8. Installing VS Exp 2012 9. Start the VS 2008 10. Application Life Cycle 11. Page Life Cycle 12. Page Life Cycle Events 13. ASP.NET Example 14. Event Handling 15. Default Events 16. Server Side 17. Request Object 18. Response Object 19. Server Controls 20. Server Controls Properties 21. Server Controls Methods 22. HTML Server Controls 23. Client Side 24. ASP.NET Basic Controls 25. TextBox Control 26. CheckBox Controls 27. RadioButton Controls 28. ListBox Control 29. HyperLink Control 30. Image Control 31. BulletedList Control 32. ASP.NET Directives 33. Implements Directive 34. Master Type Directive 35. Page Directive 36. PreviousPage Type 37. Managing State 38. Control State 39. Cookies 40. Query Strings 41. Server-Side State 42. Session State 43. Validation Controls 44. Required FieldValidator 45. Range Validator 46. Compare Validator 47. RegularExpressionValidator 48. Custom Validator 49. Validation Summary 50. Data Source Control 51. AdRotator Control 52. Calendar Control 53. Calendar Control Example 54. Panel Control 55. Panel Control Example 56. Multi Views Control 57. MultiView & View controls 58. MultiView Control Example 59. FileUpload Control 60. FileUpload Control Eg 61. AJAX Control 62. UpdatePanel control 63. UpdateProgress Control 64. Custom Controls 65. Custom Control Eg 66. Personalization 67. Create Simple Profile 68. ADO.NET 69. ADO.NET Objects 70. DataTable 71. DataRow 72. DataColumn 73. Object Example 74. Error Handling 75. Tracing Errors 76. Debugger 77. Security 78. Authentication 79. Authorization 80. LINQ 81. LINQ Query Operators 82. LINQ Example 83. Caching 84. Data Caching 85. Output Caching 86. Object Caching 87. Web Services 88. Create Web Services Eg 89. Web & Machine.config 90. Settings Schema 91. ASP.NET Deployment 92. XCopy Deployment 93. Web Setup Project 94. ASP Interview Part 1 95. ASP Interview Part 2 96. ASP Interview Part 3 97. ASP Interview Part 4 98. ASP Interview Part 5 99. ASP Interview Part 6
Pr.Pg Next Pg

Cookies tutorials

  • A cookie is a small piece of text stored on user's computer. Usually, information is stored as name-value pairs. Cookies are used by websites to keep track of visitors. Every time a user visits a website, cookies are retrieved from user machine and help identify the user. 

  • Advantages of using cookies are:

    1. Configurable expiration rules: The cookie can expire when the browser session ends, or it can exist indefinitely on the client computer, subject to the expiration rules on the client.

    2. No server resources are required: The cookie is stored on the client and read by the server after a post.

    3. Simplicity: The cookie is a lightweight, text-based structure with simple key-value pairs.

    4. Data persistence: Although the durability of the cookie on a client computer is subject to cookie expiration processes on the client and user intervention, cookies are generally the most durable form of data persistence on the client.

  • Disadvantages of using cookies are:
    1. Size limitations: Most browsers place a 4096-byte limit on the size of a cookie, although support for 8192-byte cookies is becoming more common in newer browser and client-device versions.

    2. User-configured refusal: Some users disable their browser or client device's ability to receive cookies, thereby limiting this functionality.

    3. Potential security risks: Cookies are subject to tampering. Users can manipulate cookies on their computer, which can potentially cause a security risk or cause the application that is dependent on the cookie to fail.

 

Example:

  • Here we have creating and retriving the cookies using HttpCookie class. We have used one textbox for cookie name, a button and alevel that will display the cookie name. 

Cookies1Example.aspx.cs Code:

 

Following example shows how to create a cookies and read the cookies in ASP.NET

Sam Sir

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

public partial class Cookies1Example : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

HttpCookie UserCookies = new HttpCookie("UserCookies");

UserCookies.Value = txtusername.Text;

UserCookies.Expires = DateTime.Now.AddHours(1);

Response.Cookies.Add(UserCookies);

}

 

protected void btncreatecookies_Click(object sender, EventArgs e)

{

string username = Request.Cookies["UserCookies"].Value;

lblcookiesname.Text = username;

}

}

 

 

Cookies1Example.aspx Code:


 

Following example shows how to create a cookies and read the cookies in ASP.NET

Sam Sir

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Cookies1Example.aspx.cs" Inherits="Cookies1Example" %>

 

<!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>cbtSAM ASP.NET Cookies Example</title>

</head>

<body>

<form id="form1" runat="server">

<div>

UserName :

<asp:TextBox ID="txtusername" runat="server"></asp:TextBox>

<br />

<br />

<asp:Button ID="btncreatecookies" runat="server"

onclick="btncreatecookies_Click" Text="Create Cookies" />

<br />

<asp:Label ID="lblcookiesname" runat="server" Text=""></asp:Label>

</div>

</form>

</body>

</html>

 

Output

 

After running the project it will display such as.

asp.net cookies example

Here we have created a cookies UserName is watson,below output shows the created cookie name.

asp.net cookies example


Example 2: Here is an example where we save a users choice of background color:

CookiesExample.aspx.cs Code

 

 

Following example shows the use of ASP.NET Cookies:

Sam Sir

 

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

 

public partial class CookiesExample : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (Request.Cookies["BackgroundColor"] != null)

{

ColorSelector.SelectedValue = Request.Cookies["BackgroundColor"].Value;

bdytag.Style["background-color"] = ColorSelector.SelectedValue;

}

}

protected void ColorSelector_SelectedIndexChanged(object sender, EventArgs e)

{

bdytag.Style["background-color"] = ColorSelector.SelectedValue;

HttpCookie cookie = new HttpCookie("BackgroundColor");

cookie.Value = ColorSelector.SelectedValue;

cookie.Expires = DateTime.Now.AddHours(1);

Response.SetCookie(cookie);

}

}

 

 

  • The page simply contains a DropDownList control, which automatically posts back each time a new item is selected. It has 3 different colors, besides the default one, which is simply white. Once a new item is selected, the ColorSelector_IndexChanged method is fired, from our CodeBehind file:

CookiesExample.aspx Code

 

 

Following example shows the use of ASP.NET Cookies:

Sam Sir

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CookiesExample.aspx.cs" Inherits="CookiesExample" %>

 

<!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>cbtSAM ASP.NET Cookies Example</title>

</head>

<body runat="server" id="bdytag">

<form id="form1" runat="server">

<div>

<asp:DropDownList runat="server" id="ColorSelector" autopostback="true"

onselectedindexchanged="ColorSelector_SelectedIndexChanged" >

<asp:ListItem value="White" selected="True">Select color..</asp:ListItem>

<asp:ListItem value="Red">Red</asp:ListItem>

<asp:ListItem value="Green">Green</asp:ListItem>

<asp:ListItem value="Blue">Blue</asp:ListItem>

</asp:DropDownList>

</div>

</form>

</body>

</html>

Output

 

 

asp.net cookies example

 

Pr.Pg border                                              Next Pg