ASP.NET
  Home arrow ASP.NET arrow Page 5 - 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 - Try It Out – Setting and Reading a Cookie with ASP Alone


    (Page 5 of 13 )

    Considering the dates of birth in our sailors list, it looks like we should accommodate folks that need glasses. Sailors trying to use laptops on yachts, where the bright sun makes reading a LCD screen difficult, exacerbate this problem. So we want to give sailors the ability to pick one of three types of displays: small, medium and large. Since this parameter will be utilized with every page, and is not sensitive, we can improve server speed by storing the setting in a cookie rather than in our People table for look–up.

    You will have to create three pages. The first will be a "Set Preference" form to ask the sailor for her or his preference in type size. A second page will be a "Cookie Setter" that stores the preference in a cookie and offers a button to open the home page. The third is the "Home Page Using Preference" that will read the cookie and use that data to set its type style.

    If you haven't worked with HTML styles before, I suggest you study the following few ideas:

    • Style attributes are set for levels of text, for example <p>, <H1>, etc.
    • Text for the remainder of the document will be formatted according to the style of its level

    • Styles are set within the tags <STYLE> and </STYLE>

    • Style tags are placed in the head section of the page

    • Style attributes are set as Level {AttributeName: value} such as the following:
      P {font–size: 14 pt}
    • Multiple attributes are separated by semi colons as follows:
      P {font–size: 14 pt; color: black}

      You can get a much better feel for all the features of styles from Chapters 1 and 2 of Frank Boumphrey's book: Professional Style Sheets (ISBN 1-861001-65-7). This book also carries you through XML, an improved formatting technique.

    The first page is a form that gets the font size preference from the user. The code for this page is available as 2726-11-Cook-TIO-01-SetPreferenceForm.htm:

    <HTML><HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    <TITLE>2726 chapt11 Cookies TIO 01 SetPreferences</TITLE>
    </HEAD><BODY>

    Please select the size font you prefer.
    <FORM ACTION="2726-11-Cook-TIO-01-CookieSetter.asp" METHOD=get id=form1 NAME=form1>
    <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 above code produces the following form page:

     

    Then we need the response page that actually sets the cookie. 2726-11-Cook-TIO-01-CookieSetter.asp contains the following code:

    <%@ 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>
    <%
      varPrefFontSize = Request.Querystring("PrefFontSize")
      Response.Write "Your cookie holds a font size preference of " & varPrefFontSize & _ "<BR>"
        Response.Cookies("PrefFontSize")=varPrefFontSize
        Response.Cookies("PrefFontSize").Expires = date+365
    %>
    <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>
    </BODY></HTML>

    The preceding cookie-setting code produces the screen below, if the visitor selected medium.

     

    Now that the cookie is set we can use it in the following code for a home page, saved in file 2726-11-Cook-TIO-01-HomePageUsingPreference.asp:

    <%@ 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 set sailor preference in view
    ' get cookie
    ' use sailor preference in Select Case to set styles
    '-------------------
    ' get cookie
    '
      varPrefFontSize = Request.Cookies("PrefFontSize")
    ' next line for testing
    ' Response.Write "<HR>The cookie is " & varPrefFontSize & "<HR>"

    '-------------------
    ' 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>"
    %>
    </HEAD>
    <BODY>
    <H2>Chapter 11 ASP-ADO and Cookies <BR><BR>
    TIO #1 - Setting Page Styles According to Preferences</H2>
    <P><P>Welcome to the Sailor Home Page.<BR><BR>
    Your page is displayed as per your viewing preference</P>
    </BODY></HTML>

    The above home page code produces the screen below, again assuming that the medium size text was selected. 

     

    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

    - 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...
    - Building an In-Text Advertising System Under...
    - Developing a Mini ASP.NET AJAX Server Centri...





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