ASP.NET
  Home arrow ASP.NET arrow Page 6 - Databases and Cookies
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Databases and Cookies
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 7
    2004-11-03

    Table of Contents:
  • Databases and Cookies
  • Key Points About Cookies
  • Looking at your cookies
  • Examples of Using Cookies
  • Try It Out – Setting and Reading a Cookie with ASP Alone
  • How it Works – Setting and Reading a Cookie with ASP Alone
  • Using Cookies with ADO and a Database
  • Try It Out – Setting a Cookie Using ASP–ADO
  • How it Works – Setting a Cookie Using ASP–ADO
  • Resetting a Cookie
  • Try It Out – Resetting a Cookie
  • How It Works – Resetting a Cookie
  • Summary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Databases and Cookies - How it Works – Setting and Reading a Cookie with ASP Alone


    (Page 6 of 13 )

    The first page is a simple HTML form. Remember that when using radio buttons on a form, one VALUE will be assigned to the NAME and then sent to the response page.

    <FORM ACTION="2726-11-Cook-TIO-01-CookieSetter.asp" METHOD=get >
    <INPUT TYPE="radio" NAME="PrefFontSize" VALUE=small> Small
    <INPUT TYPE="radio" NAME="PrefFontSize" VALUE=medium CHECKED> Medium
    <INPUT TYPE="radio" NAME="PrefFontSize" VALUE=large> Large<BR><BR>
    <INPUT TYPE=submit>
    </FORM>
    </BODY></HTML>

    The next page is the response page, as named in the ACTION attribute above. This page will be writing the cookie so we must be sure to buffer the page before starting the usual <HEAD> information:

    <%@ Language=VBScript%>
    <%Response.Buffer=true%>
    <HTML><HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    <TITLE>2726 chapt11 Cookies TIO#01 Create Cookie that holds the Preferences</TITLE>
    </HEAD><BODY>

    Now we pick the data of the cookie out of the Request object and assign it to a variable. I like to report back to the user what she has selected.

    <%
      varPrefFontSize = Request.Querystring("PrefFontSize")
      Response.Write "Your cookie holds a font size preference of " &_
        varPrefFontSize & "<BR>"

    Once safely ensconced in a variable, we can use the data as the text for a cookie. Since cookies by default expire when the browser closes, we need to explicitly set an expiration:

      Response.Cookies("PrefFontSize")=varPrefFontSize 
      Response.Cookies("PrefFontSize").Expires = date+365
    %>

    Now that the cookie is set we can give the user a hyperlink to the home page to try out the new convenience.

    <A HREF="2726-11-Cook-TIO-01-HomePageUsingSizePreference.asp">
    Now that the font size is set, click here to go to the home page.</A>

    The home page that uses the cookie has two steps. After the housekeeping, here is the game plan laid out.

    <%@ Language=VBScript %>
    <HTML><HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    <TITLE>2726 chapt11 Cookies TIO#01 HomePage Using the Font Size Preferences</TITLE>
    </HEAD><BODY>
    <%
    '--------------------
    ' the plan to use the font size cookie preference on this page
    ' get cookie
    ' use sailor preference in Select Case to set styles

    To pick up the cookie we query the Request object and use that result to fill a variable. You can see the artifact of development where I checked the cookie on the browser screen. I looked at the results while building the page, then at the time of deployment added an apostrophe to the beginning of the line which turned the command into a comment and thus prevented execution of the Response.Write. I like to leave these in the code because later, when there is another round of problems and troubleshooting, these tools are available by just deleting the leading apostrophe.

    '--------------------
    ' get cookie
    '
    varPrefFontSize = Request.Cookies("PrefFontSize")
    ' next line for testing
    'Response.Write "<HR>The cookie is " & varPrefFontSize & "<HR>"

    Now we run a Select Case against the variable that contains the cookie contents. Note that we must write the <STYLE> and </STYLE>  tags regardless of the preference, so those are outside of the Select Case. Within the Select Case only one of the style definitions will be written.

    '--------------------
    ' use sailor preference in Select Case to set styles
    '
      Response.Write "<STYLE>"
      Select Case varPrefFontSize
        case "large"
          Response.Write "P {font-size: 26 pt}"
        case "medium"
          Response.Write "P {font-size: 16 pt}"
        case "small"
          Response.Write "P {font-size: 6 pt}"
      End Select
      Response.Write "</STYLE>"
    %>

    The remainder of the page contains simple HTML, which uses <p><p> styles that have been modified within the above code:

    '--------------------
    ' use sailor preference in Select Case to set styles
    '
    Response.Write "<STYLE>"
    Select Case varPrefFontSize
    case "large"
    Response.Write "P {font-size: 26 pt}"
    case "medium"
    Response.Write "P {font-size: 16 pt}"
    case "small"
    Response.Write "P {font-size: 6 pt}"
    End Select
    Response.Write "</STYLE>"
    %>

    This is from Beginning ASP Databases by Kauffman, Spencer, and Willis (Apress, ISBN 1590592492). Check it out at your favorite bookstore today.

    Buy this book now.

    More ASP.NET Articles
    More By Apress Publishing


       · i really need help adding the updated information to my database
     

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek