Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 17703 |  Welcome, Guest!   Register  Login
 Home > Blogs > ASP.NET > State Management in ASP.NET part-1 (Cookies) ...
Johnbhatt

State Management in ASP.NET part-1 (Cookies)

 Blog author: Johnbhatt | Posted on: 7/6/2012 | Category: ASP.NET Blogs | Views: 903 | Status: [Member] | Points: 75 | Alert Moderator   
Ads

Hi,

I am Back with State Management Series.


In this post we are going to Talk about Cookies. After Reading this, you must be able to do following things:

  • Create Cookie
  • Access Cookie
  • Can answer Some Basic Q & A about Cookie

What is Need of Cookie?

Cookies are Required to Store Temporary data in Users system which may be Required during Session or after Session.

How Many Type of Cookie are there?

Cookies are generally of Two Type. Session wide or Persistent. Sessional cookies Expire as soon as User Closed Session means Browser. Persistent cookies expire after Defined Time. This can be Defined at the Time of Creation.

Who Make Cookies?

Cookie is always made by Application (Server) in Users/ Clients Browser and stored in Hard Disk of User. These are used to store Users Customized Appearance, other Values , Login Time etc for future Retrieval so that website can server him better.

Limitations of Cookie?

  • Cookies are open Invitations to Hackers.
  • Cookies can only store Little and Only String Type Data.

Now Lets Learn with Coding. First We will Create a Cookie named myCookie and Solve Earlier Problem with the Help of Cookie.

Now Code is Like Below:

 protected void Page_Load(object sender, EventArgs e)
    {

    }
    int x;    // Made a Variable with Datatype Int for Future Use
    HttpCookie myCookie;   // Defined a Cookie named myCookie.
    protected void Button1_Click(object sender, EventArgs e)
    { 
        x = Convert.ToInt32(TextBox1.Text);  // Converted textbox Value which we earlier Did.
        myCookie = new HttpCookie("txtValue");  // Assigned Variable myCookie.
        myCookie.Value = Convert.ToString(x);   // Value assigned to myCookie.
        Response.Cookies.Add(myCookie); //This Simple Made a Cookie that will Expire in Session End.
        myCookie.Expires = DateTime.Now.AddMinutes(15);  // We made is Persistent and stored it for Next 15 minutes.
      
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
       
        HttpCookie storedCook = Request.Cookies["txtValue"];  // Requested from Server for Cookie
        if (storedCook != null)  
            x = Convert.ToInt32(storedCook.Value);  // Converted to Integer again.
        TextBox1.Text = Convert.ToString(x * x);    // Now Printed Value in Textbox.
    }
Now Check....

Cool na, I'll be waiting for Feedback.






John Bhatt
Glad to Know, Free to Share.....
http://www.johnbhatt.com
Found interesting? Add this to:


About John Bhatt

Experience:4 year(s)
Home page:http://www.johnbhatt.com
Member since:Thursday, June 21, 2012
Level:Starter
Status: [Member]
Biography:John Bhatt is an IT Professional having interest in Web technology. He is Web Designer, Developer, Software Developer, Blogger and Technology Geek. Currently he writes his Blogs at Blog of P.Yar.B and various other Sites. He is Main author and founder of Download Center. Contact Him at : Facebook | Twitter | Website.
>> Write Response - Respond to this post and get points

More Blogs

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/19/2013 4:58:19 AM