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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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

    - Developing a Mini ASP.NET AJAX Server Centri...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT