Cookies in asp.net - All about HttpCookies

Cookies in asp.net 


Asp.net cookies is a small piece of information stored on the client machine.The HttpCookie class is under System.Web namespace. It contains properties and methods necessary to work with individual cookies.Cookies are used by websites to keep track of visitors e.g. to keep user information like username etc. Length of the text stored in cookies is 4096(4kb) in general but could vary from browser to browser.

Cookies are small text files that a is transferred by the site you visit or internet service provider and saved to your computer's hard drive with the help of web browser. You need to allow your site to save the file to your system. The site/ service provider recognizes your web browser and capture, remembers and stores specific information.

With the help of cookies, only the website can remember your Wishlist items, products stored in your shopping cart, your recent activity list and preferences that ultimately help us improve our service and your browsing experience. With the help of cookies, we compile site traffic and site visitors’ interaction data.

Remember, third party sites are not provided your personal information stored with us, but we may use the information and contact external websites to improve the services we offer and assist you better.

Cookies can be enabled or disabled through your browser settings. In case you face any issues look for browser help menu and search how to modify cookies settings. Turning off cookies will refrain you from features that help improve site experience and services provided to you. You can still place orders over the telephone by contacting customer service.


Cookies in asp.net



Persistent and Non-persistent cookies in ASP.NET



There two type of cookies in ASP.NET

Persistent cookies: Asp.net cookies are stored on your computer hard disk.They stay on your hard disk and can be accessed by web servers until they are deleted or have expired.

public void PersistentCookies(string name, string value)
{
 HttpCookie cookie = new HttpCookie(name);
 cookie.Value = value;
 cookie.Expires = Convert.ToDateTime("12/12/2008");
 Response.Cookies.Add(cookie);
}



Non-persistent cookies: Asp.net cookies are saved only while your web browser is running.  They can be used by a web server only until you close your browser.They are not saved on your disk.


public void NonPersistentCookies(string name, string value)

{
    HttpCookie cookie = new HttpCookie(name);
    cookie.Value = value;
    Response.Cookies.Add(cookie);
}

Read More

How to call javascript function in anchor tag href

Call javascript function in anchor tag href


<html>
<head>
<title>Call javascript function in anchor tag  href:Examples</title>
</head>
<body>
<p><a href="javascript:void(window.open('http://xyz.com/'))">
Open a new window.</a></p>
</body>

</html>


Read More

Call javascript function in body onload event : body onload javascript

Call javascript function in body onload event 




<html>
<head>
<title> body onload javascript</title>
<script type="text/javascript">

    function demo() {

        document.write("onload");
    }

</script>
</head>
<body onload="demo();">
</body>
Read More

Difference between Const and ReadOnly in c#

Difference between Const and ReadOnly



Const in c#
  • You have to initialize const variables while declaration.
  • You cannot reassign a const variable.
  • Compiler evaluates the const variables.
  • The static modifier is not allowed in a constant declaration.
  • A const field of a reference type other than string can only be initialized with null.


Example for const in c#.net



using System;
using System.Text;

namespace ProgramCAll
{

    class ProgramCall
    {
        /*A const field of a reference type other than string
        can only be initialized with null*/
        private const StringBuilder myName = null;

        public static void Main()
        {
            //You have to initilize Const varabiles while declartion
            const int myEmpId = 173524;

            //Valid scenario
            const int myNumber = 10 + 16;

            const string myName = "John";

            // Reassigning a const variable is not allowed.
            //Comment out below code to run the program
            myEmpId = 23456;

            Console.WriteLine(myEmpId);

            Console.Read();
        }
    }
}


Readonly in c#
  • readonly fields can be initialized only while declaration or in the constructor.
  • Once  you initialize a readonly field, you cannot reassign it.
  • You can use static modifier for readonly fields
  • readonly modifier can be used with reference types
  • readonly modifier can be used only for instance or static fields, you cannot use readonly keyword for variables in the methods.


Example for readonly modifier in c#.net


using System;
using System.Text;

namespace ProgramCall
{
    class MyClass
    {
        //readonly can only be used with instance or static variables
        public readonly int mynumber = 10;
        public readonly int addnumber = 10 + 15;

        //readonly modifier can be used with static varaibles       
        public static string myEmployer = "ProgramCall.Com";

        //readonly can be used with reference types
        public readonly StringBuilder name = new StringBuilder("John");

        /*  readonly varaible can be intilized in constructor but
             cannot be declared in constructor    */
        public readonly string myName;

        //readonly fields can be initlized in constructor
        public MyClass(string name)
        {
            myName = name;

        }

        private void myMethod()
        {
            /* readonly modifier cannot be used in method level variables
           the below line throws a error. */
            //  readonly int num = 5;
        }

    }

    //main class
    class ProgramCall
    {

        public static void Main()
        {
            MyClass obj = new MyClass("Bill");

            Console.Read();
        }
    }
}


Difference between const and readonly
  • const fields has to be initialized while declaration only, while readonly fields can be initialized at declaration or in the constructor.
  • const variables can declared in methods ,while readonly fields cannot be declared in methods.
  • const fields cannot be used with static modifier, while readonly fields can be used with static modifier.
  • A const field is a compile-time constant, the readonly field can be used for run time constants.
Read More