Monday, 17 March 2008

Using Cookies C# ASP.Net

c# Cookies

Part 1- Using cookies

The easy way...

How to store a single value in a cookie

Response.Cookies["name"].Value = "Sean Tiernan";

Multiple values

This example shows how to write a cookie with multiple key value pairs in the cookie UID

Response.Cookies["UID"]["firstname"] = "Sean";
Response.Cookies["UID"]["surname"] = "Tiernan";


How to read information from a cookie



// Check if cookie exists

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


{


    // Read cookie information - firstname into a string called firstname

  
firstName = Server.HtmlEncode(Request.Cookies["UID"]["firstname"]);


// Assign to a textbox

    tbFirstName.Text = firstName;



// Read surname into a string called surname

    surname = Server.HtmlEncode(Request.Cookies["UID"]["surname"]);


// Assign to a text box

    tbSurname.Text = surname;



}




No comments: