Outline - PowerPoint PPT Presentation

1 / 100
About This Presentation
Title:

Outline

Description:

Usually stores data in a relational database management systems (RDBMS) Middle Tier ... Hidden inputs that store any data that document author specifies such ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 101
Provided by: pres50
Category:
Tags: middle | outline

less

Transcript and Presenter's Notes

Title: Outline


1
Chapter 20 - ASP .Net, Web Forms and Web Controls
Outline 20.1 Introduction20.2 Simple HTTP
Transaction 20.3 System Architecture 20.4
Creating and Running a Simple Web Form Example
20.5 Web Controls 20.5.1  Text and Graphics
Controls 20.5.2  AdRotator Control 20.5.3  Valid
ation Controls20.6   Session Tracking 20.6.1  Co
okies 20.6.2  Session Tracking with
HttpSessionState20.7 Case Study Online Guest
book 20.8 Case Study Connecting to a Database
in ASP .NET 20.9 Tracing
2
20.1 Introduction
  • Web-Based Application Development
  • Creates Web content for Web browser clients
  • HyperText Markup Language (HTML)
  • Client-side scripting
  • Images and binary data
  • Web Forms (Web Form pages)
  • File extension .aspx
  • ASPX (Web Form files) contain written code, event
    handlers, utility methods and other supporting
    code

3
20.2 Simple HTTP Transaction
  • HyperText Transfer Protocol (HTTP)
  • Defines methods and headers which allows clients
    and servers exchange information in uniform way
  • Uniform Resource Locator (URL)
  • IP address indicating the location of a resource
  • All HTML documents have a corresponding URL
  • Domain Name Server (DNS)
  • A computer that maintains a database of hostnames
    and their corresponding IP addresses

4
20.3 System Architecture
  • Multi-tier Applications
  • Web-based applications (n-tier applications)
  • Tiers are logical groupings of functionality
  • Information Tier (data tier or bottom tier)
  • Maintains data pertaining to the applications
  • Usually stores data in a relational database
    management systems (RDBMS)
  • Middle Tier
  • Acts as an intermediary between data in the
    information tier and the application's clients

5
20.4 Creating and Running a Simple Web-Form
Example
  • Visual Component
  • Clickable buttons and other GUI components which
    users interact
  • Nonvisual Component
  • Hidden inputs that store any data that document
    author specifies such as e-mail address

6
WebTime.aspx
  • 1 lt-- Fig. 20.4 WebTime.aspx --gt
  • 2 lt-- A page that contains two labels --gt
  • 3
  • 4 lt_at_ Page Language"vb" AutoEventWireup"false
    "
  • 5 Codebehind"WebTime.aspx.vb"
    Inherits"WebTime.WebTimer" gt
  • 6
  • 7 lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    Transitional//EN"gt
  • 8 ltHTMLgt
  • 9 ltHEADgt
  • 10 lttitlegtWebTimelt/titlegt
  • 11 ltmeta name"GENERATOR"
  • 12 content"Microsoft Visual
    Studio.NET 7.0"gt
  • 13 ltmeta name"CODE_LANGUAGE"
    content"Visual Basic 7.0"gt
  • 14 ltmeta name"vs_defaultClientScript"
    content"JavaScript"gt
  • 15 ltmeta name"vs_targetSchema"
  • 16 content"http//schemas.microsoft.co
    m/intellisense/ie5"gt
  • 17 lt/HEADgt
  • 18 ltbody MS_POSITIONING"GridLayout"gt
  • 19 ltform id"Form1" method"post"
    runat"server"gt

7
WebForm1.aspx
  • 1 ' Fig. 20.5 WebForm1.aspx.vb
  • 2 ' The code-behind file for a page
  • 3 ' that displays the current time.
  • 4
  • 5 Imports System
  • 6 Imports System.Web
  • 7 Imports System.Web.UI
  • 8 Imports System.Web.UI.WebControls
  • 9
  • 10 Public Class WebTimer
  • 11 Inherits System.Web.UI.Page
  • 12
  • 13 Protected WithEvents displayLabel As _
  • 14 System.Web.UI.WebControls.Label
  • 15
  • 16 Protected WithEvents timeLabel As _
  • 17 System.Web.UI.WebControls.Label
  • 18
  • 19 'This call is required by the Web Form
    Designer.

8
WebForm1.aspx
  • 34 Private Sub Page_Load(ByVal sender As
    System.Object, _
  • 35 ByVal e As System.EventArgs) Handles
    MyBase.Load
  • 36 'Put user code to initialize the page
    here
  • 37 End Sub ' Page_Load
  • 38 End Class ' WebTimer

9
WbTime.html
  • 1 lt!-- Fig. 20.6 WebTime.html
    --gt
  • 2 lt!-- The HTML generated when WebTime is
    loaded. --gt
  • 3
  • 4 lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    Transitional//EN" gt
  • 5
  • 6 ltHTMLgt
  • 7 ltHEADgt
  • 8 lttitlegtWebTimelt/titlegt
  • 9 ltmeta name"GENERATOR"
  • 10 Content"Microsoft Visual Studio
    7.0"gt
  • 11 ltmeta name"CODE_LANGUAGE"
    Content"Visual Basic 7.0"gt
  • 12 ltmeta name"vs_defaultClientScript"
    content"JavaScript"gt
  • 13 ltmeta name"vs_targetSchema"
  • 14 content"http//schemas.microsoft.co
    m/intellisense/ie5"gt
  • 15 lt/HEADgt
  • 16
  • 17 ltbody MS_POSITIONING"GridLayout"gt
  • 18 ltform name"Form1" method"post"
  • 19 action"WebTime.aspx" id"Form1"gt

10
20.4 Creating and Running a Simple Web Form
Example
11
20.4 Creating and Running a Simple Web Form
Example
12
20.4 Creating and Running a Simple Web Form
Example
13
20.4 Creating and Running a Simple Web Form
Example
14
20.4 Creating and Running a Simple Web Form
Example
15
20.4 Creating and Running a Simple Web Form
Example
16
20.5 Web Controls
  • Text and Graphics Control
  • Label, Button, TextBox, Image RadioButtonList and
    DropDownList
  • AdRotator Control
  • Randomly selects an image to display and then
    generates a hyperlink to the Web page associated
    with that image
  • Validation Controls
  • Determines whether the data in another Web
    control are in the proper format
  • Validates user input

17
20.5 Web Controls
18
Controls.aspx
  • 1 lt-- Fig. 20.17 Conrols.aspx --gt
  • 2 lt-- Demonstrates web controls --gt
  • 3
  • 4 lt_at_ Page Language"vb" AutoEventWireup"false
    "
  • 5 Codebehind"Controls.aspx.vb"
  • 6 Inherits"Controls.WebForm1"
  • 7 enableViewStateFalse"
    EnabledSessionStateFalse" gt
  • 8
  • 9 lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    Transitional//EN"gt
  • 10 ltHTMLgt
  • 11 ltHEADgt
  • 12 lttitlegtWebForm1lt/titlegt
  • 13 ltmeta name"GENERATOR"
  • 14 content"Microsoft Visual
    Studio.NET 7.0"gt
  • 15 ltmeta name"CODE_LANGUAGE"
    content"Visual Basic 7.0"gt
  • 16 ltmeta name"vs_defaultClientScript"
    content"JavaScript"gt
  • 17 ltmeta name"vs_targetSchema"
  • 18 content"http//schemas.microsoft.co
    m/intellisense/ie5"gt
  • 19 lt/HEADgt

19
Controls.aspx
  • 37 ltaspImage id"UserLabel"
    style"Z-INDEX 103
  • 38 LEFT 42px POSITION absolute
    TOP 135px"
  • 39 runat"server" Width"439px"
    Height"28px"
  • 40 ImageUrl"images/user.png"gt
  • 41 lt/aspImagegt
  • 42
  • 43 ltaspLabel id"FillLabel"
    style"Z-INDEX 104
  • 44 LEFT 50px POSITION absolute
    TOP 189px"
  • 45 runat"server" Width"225px"
    ForeColor"Lime"
  • 46 Font-Size"Medium"gt
  • 47 Please fill out the fields
    below.
  • 48 lt/aspLabelgt
  • 49
  • 50 ltaspImage id"FirstImage"
    style"Z-INDEX 105
  • 51 LEFT 49px POSITION absolute
    TOP 224px"
  • 52 runat"server" Width"84px"
    Height"36px"
  • 53 ImageUrl"images/fname.png"gt
  • 54 lt/aspImagegt
  • 55

20
Controls.aspx
  • 67 ltaspTextBox id"EmailTextBox"
    style"Z-INDEX 108
  • 68 LEFT 147px POSITION absolute
    TOP 284px"
  • 69 runat"server" Width"112px"
    Height"18px"gt
  • 70 lt/aspTextBoxgt
  • 71
  • 71 ltaspImage id"LastImage"
    style"Z-INDEX 109
  • 73 LEFT 292px POSITION absolute
    TOP 227px"
  • 74 runat"server" Width"77px"
    Height"33px"
  • 75 ImageUrl"images/lname.png"gt
  • 76 lt/aspImagegt
  • 77
  • 78 ltaspImage id"PhoneImage"
    style"Z-INDEX 110
  • 79 LEFT 292px POSITION absolute
    TOP 273px"
  • 80 runat"server" Width"80px"
    Height"30px"
  • 81 ImageUrl"images/phone.png"gt
  • 82 lt/aspImagegt
  • 83
  • 84 ltaspTextBox id"LastTextBox"
    style"Z-INDEX 111
  • 85 LEFT 400px POSITION absolute
    TOP 232px"

21
Controls.aspx
  • 100 ltaspImage id"PublicationImage"
    style"Z-INDEX 114
  • 101 LEFT 50px POSITION absolute
    TOP 356px"
  • 102 runat"server" Width"435px"
    Height"27px"
  • 103 ImageUrl"images/downloads.png"gt
  • 104 lt/aspImagegt
  • 105
  • 106 ltaspLabel id"Booklabel"
    style"Z-INDEX 115
  • 107 LEFT 54px POSITION absolute
    TOP 411px"
  • 108 runat"server" Width"348px"
    Height"23px"
  • 109 ForeColor"Lime"
    Font-Size"Medium"gt
  • 110 Which book would you like
    information about?
  • 111 lt/aspLabelgt
  • 112
  • 113 ltaspDropDownList
    id"BookDropDownList"
  • 114 style"Z-INDEX 116 LEFT 60px
    POSITION
  • 115 absolute TOP 448px"
    runat"server"
  • 116 Width"326px" Height"29px"gt
  • 117
  • 118 ltaspListItem Value"XML How to
    Program 1e"gt

22
Controls.aspx
  • 135 ltaspListItem Value"C How to
    Program 3e"gt
  • 136 C How to Program 3e
  • 137 lt/aspListItemgt
  • 138 lt/aspDropDownListgt
  • 139
  • 140 ltaspHyperLink id"BooksHyperLink"
  • 141 style"Z-INDEX 117 LEFT 64px
    POSITION
  • 142 absolute TOP 486px"
    runat"server"
  • 143 Width"385px" Height"22px"
  • 144 NavigateUrl"http//www.deitel.co
    m"gt
  • 145 Click here to view more
    information about our books.
  • 146 lt/aspHyperLinkgt
  • 147
  • 148 ltaspImage id"OperatingImage"
    style"Z-INDEX 118
  • 149 LEFT 53px POSITION absolute
    TOP 543px"
  • 150 runat"server" Width"431px"
    Height"32px"
  • 151 ImageUrl"images/os.png"gt
  • 152 lt/aspImagegt
  • 153

23
Controls.aspx
  • 167 ltaspRadioButtonList
    id"OperatingRadioButtonList"
  • 168 style"Z-INDEX 125 LEFT 65px
    POSITION
  • 169 absolute TOP 624px"
    runat"server"
  • 170 Height"122px" Width"155px"gt
  • 171
  • 172 ltaspListItem Value"Windows
    NT"gt
  • 173 Windows NT
  • 174 lt/aspListItemgt
  • 175 ltaspListItem Value"Windows
    2000"gt
  • 176 Windows 2000
  • 177 lt/aspListItemgt
  • 178 ltaspListItem Value"Windows
    XP"gt
  • 179 Windows XP
  • 180 lt/aspListItemgt
  • 181 ltaspListItem Value"Linux"gt
  • 182 Linux
  • 183 lt/aspListItemgt
  • 184 ltaspListItem Value"Other"gt
  • 185 Other

24
20.5 Web Controls
25
CountryRotator.aspx
  • 1 lt-- Fig 20.18 CountryRotator.aspx
    --gt
  • 2 lt-- A Web Form that demonstrates class
    AdRotator. --gt
  • 3
  • 4 lt_at_ Page Language"vb" AutoEventWireup"false
    "
  • 5 Codebehind"CountryRotator.aspx.vb"
  • 6 Inherits"AdRotator.AdRotator"gt
  • 7
  • 8 lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    Transitional//EN"gt
  • 9 ltHTMLgt
  • 10 ltHEADgt
  • 11 lttitlegtWebForm1lt/titlegt
  • 12 ltmeta content"Microsoft Visual
    Studio.NET 7.0"
  • 13 name"GENERATOR"gt
  • 14 ltmeta content"Visual Basic 7.0"
    name"CODE_LANGUAGE"gt
  • 15 ltmeta content"JavaScript"
    name"vs_defaultClientScript"gt
  • 16 ltmeta name"vs_targetSchema"
  • 17 content"http//schemas.microsoft.co
    m/intellisense/ie5"gt
  • 18 lt/HEADgt
  • 19 ltbody background

26
CountryRotator.aspx
  • 30 ltaspadrotator id"countryRotator"
    style"Z-INDEX 102
  • 31 LEFT 36px POSITION absolute
    TOP 47px"
  • 32 runat"server" Height"72px"
    Width"108px"
  • 33 AdvertisementFile"AdRotatorInfor
    mation.xml"gt
  • 34 lt/aspadrotatorgt
  • 35
  • 36 lt/formgt
  • 37 lt/bodygt
  • 38 lt/HTMLgt
  •  
  • Fig. 20

27
Country.aspx
  • 1 ' Fig. 20.19 Country.aspx.vb
  • 2 ' The code-behind file for a page that
  • 3 ' demonstrates the AdRotator class.
  • 4
  • 5 Public Class AdRotator
  • 6 Inherits System.Web.UI.Page
  • 7
  • 8 Protected WithEvents displayLabel As _
  • 9 System.Web.UI.WebControls.Label
  • 10
  • 11 Protected WithEvents countryRotator As _
  • 12 System.Web.UI.WebControls.AdRotator
  • 13
  • 14 ' This call is required by the Web Form
    Designer.
  • 15 Private Sub InitializeComponent()
  • 16 End Sub
  • 17
  • 18 Private Sub Page_Init(ByVal sender As
    System.Object, _
  • 19 ByVal e As System.EventArgs) Handles
    MyBase.Init

28
20.5 Web Controls
29
AdRotatorInformation.xml
  • 1 lt?xml version"1.0" encoding"utf-8"?gt
  • 2
  • 3 lt!-- Fig. 20.20 AdRotatorInformation.xml
    --gt
  • 4 lt!-- XML file containing advertisement
    information. --gt
  • 5
  • 6 ltAdvertisementsgt
  • 7 ltAdgt
  • 8 ltImageUrlgtimages/us.pnglt/ImageUrlgt
  • 9 ltNavigateUrlgt
  • 10 http//www.odci.gov/cia/publications/
    factbook/geos/us.html
  • 11 lt/NavigateUrlgt
  • 12 ltAlternateTextgtUnited States
    Informationlt/AlternateTextgt
  • 13 ltImpressionsgt1lt/Impressionsgt
  • 14 lt/Adgt
  • 15
  • 16 ltAdgt
  • 17 ltImageUrlgtimages/france.pnglt/ImageUrlgt
  • 18 ltNavigateUrlgt
  • 19 http//www.odci.gov/cia/publications
    /factbook/geos/fr.html

30
AdRotatorInformation.xml
  • 34 ltAdgt
  • 35 ltImageUrlgtimages/italy.pnglt/ImageUrlgt
  • 36 ltNavigateUrlgt
  • 37 http//www.odci.gov/cia/publications
    /factbook/geos/it.html
  • 38 lt/NavigateUrlgt
  • 39 ltAlternateTextgtItaly
    Informationlt/AlternateTextgt
  • 40 ltImpressionsgt1lt/Impressionsgt
  • 41 lt/Adgt
  • 42
  • 43 ltAdgt
  • 44 ltImageUrlgtimages/spain.pnglt/ImageUrlgt
  • 45 ltNavigateUrlgt
  • 46 http//www.odci.gov/cia/publications/
    factbook/geos/sp.html
  • 47 lt/NavigateUrlgt
  • 48 ltAlternateTextgtSpain
    Informationlt/AlternateTextgt
  • 49 ltImpressionsgt1lt/Impressionsgt
  • 50 lt/Adgt
  • 51
  • 52 ltAdgt

31
AdRotatorInformation.xml
  • 70 ltAdgt
  • 71 ltImageUrlgtimages/senegal.pnglt/ImageUrlgt
  • 72 ltNavigateUrlgt
  • 73 http//www.odci.gov/cia/publications
    /factbook/geos/sg.html
  • 74 lt/NavigateUrlgt
  • 75 ltAlternateTextgtSenegal
    Informationlt/AlternateTextgt
  • 76 ltImpressionsgt1lt/Impressionsgt
  • 77 lt/Adgt
  • 78
  • 79 ltAdgt
  • 80 ltImageUrlgtimages/sweden.pnglt/ImageUrlgt
  • 81 ltNavigateUrlgt
  • 82 http//www.odci.gov/cia/publications
    /factbook/geos/sw.html
  • 83 lt/NavigateUrlgt
  • 84 ltAlternateTextgtSweden
    Informationlt/AlternateTextgt
  • 85 ltImpressionsgt1lt/Impressionsgt
  • 86 lt/Adgt
  • 87
  • 88 ltAdgt

32
Generator.aspx
  • 1 lt-- Fig. 20.21 Generator.aspx
    --gt
  • 2 lt-- A Web Form demonstrating the use of
    validators. --gt
  • 3
  • 4 lt_at_ Page Language"vb" AutoEventWireup"false
    "
  • 5 Codebehind"Generator.aspx.vb"
  • 6 Inherits"WordGenerator.Generator"7
    enableViewStateFalse" EnableSessionStateFa
    lse" gt8
  • 9 lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    Transitional//EN"gt
  • 10 ltHTMLgt
  • 11 ltHEADgt
  • 12 lttitlegtWebForm1lt/titlegt
  • 13 ltmeta name"GENERATOR"
  • 14 content"Microsoft Visual
    Studio.NET 7.0"gt
  • 15 ltmeta name"CODE_LANGUAGE"
    content"Visual Basic 7.0"gt
  • 16 ltmeta name"vs_defaultClientScript"
    content"JavaScript"gt
  • 17 ltmeta name"vs_targetSchema"
  • 18 content"http//schemas.microsoft.co
    m/intellisense/ie5"gt
  • 19 lt/HEADgt
  • 20 ltbody MS_POSITIONING"GridLayout"gt
  • 21 ltform id"Form1" method"post"
    runat"server"gt

33
Generator.aspx
  • 35 ltaspRegularExpressionValidator
    id"phoneNumberValidator"
  • 36 style"Z-INDEX 105 LEFT
    204px POSITION absolute
  • 37 TOP 44px" runat"server"
    ErrorMessage
  • 38 "The phone number must be in the
    form 555-4567."
  • 39 ControlToValidate"phoneTextBox"
  • 40 ValidationExpression"\d3-\d4
    "gt
  • 41 lt/aspRegularExpressionValidatorgt
  • 42
  • 43 ltaspRequiredFieldValidator
    id"phoneInputValidator"
  • 44 style"Z-INDEX 104 LEFT
    207px POSITION
  • 45 absolute TOP 81px"
    runat"server"
  • 46 ErrorMessage
  • 47 "Please enter a phone
    number."
  • 48 ControlToValidate"phoneTextBox"gt
  • 49 lt/aspRequiredFieldValidatorgt
  • 50
  • 51 ltaspButton id"submitButton"
    style"Z-INDEX 103
  • 52 LEFT 38px POSITION absolute
    TOP 77px"
  • 53 runat"server" Text"Submit"gt

34
Generator.aspx.vb
  • 1 ' Fig. 20.22 Generator.aspx.vb
  • 2 ' The code-behind file for a page that
  • 3 ' generates words when given a phone number.
  • 4
  • 5 Imports System.Web.UI.WebControls
  • 6
  • 7 Public Class Generator
  • 8 Inherits System.Web.UI.Page
  • 9
  • 10 Protected WithEvents phoneInputValidator
    As _
  • 11 RequiredFieldValidator
  • 12
  • 13 Protected WithEvents phoneNumberValidator
    As _
  • 14 RegularExpressionValidator
  • 15
  • 16 Protected WithEvents promptLabel As Label
  • 17 Protected WithEvents outputTextBox As
    TextBox
  • 18 Protected WithEvents submitButton As
    Button
  • 19 Protected WithEvents phoneTextBox As
    TextBox

35
Generator.aspx.vb
  • 36 ' generate words for first 3 digits
  • 37 outputTextBox.Text "Here are the
    words for the" _
  • 38 "first three digits" vbCrLf
  • 39
  • 40 ComputeWords(number.Substring(0,
    3), "")
  • 41 outputTextBox.Text vbCrLf
  • 42
  • 43 ' generate words for last 4 digits
  • 44 outputTextBox.Text "Here are the
    words for the" _
  • 45 "last 4 digits" vbCrLf
  • 46
  • 47 ComputeWords(number.Substring(3),
    "")
  • 48
  • 49 outputTextBox.Visible True
  • 50 End If
  • 51
  • 52 End Sub ' Page_Load
  • 53
  • 54 Private Sub ComputeWords(ByVal number As
    String, _

36
Generator.aspx.vb
  • 71 ' determine number, call ComputeWord
    recursively
  • 72 Select Case current
  • 73
  • 74 ' 0 can be q or z
  • 75 Case 0
  • 76 ComputeWords(number,
    temporaryWord "q")
  • 77 ComputeWords(number,
    temporaryWord "z")
  • 78
  • 79 ' 1 has no letter associated
    with it
  • 80 Case 1
  • 81 ComputeWords(number,
    temporaryWord "")
  • 82
  • 83 ' 2 can be a, b or c
  • 84 Case 2
  • 85 ComputeWords(number,
    temporaryWord "a")
  • 86 ComputeWords(number,
    temporaryWord "b")
  • 87 ComputeWords(number,
    temporaryWord "c")
  • 88
  • 89 ' 3 can be d, e or f

37
Generator.aspx.vb
  • 101 ' 5 can be j, k or l
  • 102 Case 5
  • 103 ComputeWords(number,
    temporaryWord "j")
  • 104 ComputeWords(number,
    temporaryWord "k")
  • 105 ComputeWords(number,
    temporaryWord "l")
  • 106
  • 107 ' 6 can be m, n or o
  • 108 Case 6
  • 109 ComputeWords(number,
    temporaryWord "m")
  • 110 ComputeWords(number,
    temporaryWord "n")
  • 111 ComputeWords(number,
    temporaryWord "o")
  • 112
  • 113 ' 7 can be p, r or s
  • 114 Case 7
  • 115 ComputeWords(number,
    temporaryWord "p")
  • 116 ComputeWords(number,
    temporaryWord "r")
  • 117 ComputeWords(number,
    temporaryWord "s")
  • 118
  • 119 ' 8 can be t, u or v

38
20.5 Web Controls
39
20.5 Web Controls
40
Generator.html
  • 1 lt!-- Fig. 20.23 Generator.html
    --gt
  • 2 lt!-- The HTML page that is sent to the
    client browser. --gt
  • 3
  • 4 lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    Transitional//EN" gt
  • 5 ltHTMLgt
  • 6 ltHEADgt
  • 7 lttitlegtWebForm1lt/titlegt
  • 8 ltmeta name"GENERATOR"
  • 9 content"Microsoft Visual Studio
    7.0"gt
  • 10 ltmeta name"CODE_LANGUAGE"
    content"Visual Basic 7.0" gt
  • 11 ltmeta name"vs_defaultClientScript"
  • 12 content"JavaScript"gt
  • 13 ltmeta name"vs_targetSchema"
  • 14 content"http//schemas.microsoft.co
    m/intellisense/ie5"gt
  • 15 lt/HEADgt
  • 16
  • 17 ltbody MS_POSITIONING"GridLayout"gt
  • 18
  • 19 ltform name"Form1" method"post"

41
Generator.html
  • 30 ltspan id"phoneNumberValidator"
  • 31 controltovalidate"phoneTextBox"
  • 32 errormessage
  • 33 "The phone number must be in
    the form 555-4567."
  • 34 evaluationfunction
  • 35 "RegularExpressionValidatorEva
    luateIsValid"
  • 36 validationexpression"\d3-\d4
    "
  • 37 style"colorRedZ-INDEX106LEFT
    217px
  • 38 POSITIONabsoluteTOP73pxvis
    ibilityhidden"gt
  • 39 The phone number must be in
    the form 555-4567.
  • 40 lt/spangt
  • 41
  • 42 ltinput name"phoneTextBox"
    type"text"
  • 43 id"phoneTextBox"
  • 44 style"Z-INDEX 102 LEFT 16px
  • 45 POSITION absolute TOP 52px"
    /gt
  • 46
  • 47 ltinput type"submit"
    name"submitButton"
  • 48 value"Submit"

42
Generator.html
  • 57 ltspan id"phoneInputValidator"
  • 58 controltovalidate"phoneTextBox"
  • 59 errormessage"Please enter a
    phone number."
  • 60 evaluationfunction
  • 61 "RequiredFieldValidatorEvaluat
    eIsValid"
  • 62 initialvalue""
    style"colorRedZ-INDEX105
  • 63 LEFT217pxPOSITIONabsoluteT
    OP47px
  • 64 visibilityhidden"gtPlease
    enter a phone number.
  • 65 lt/spangt
  • 66
  • 67 ltspan id"promptLabel"
    style"Z-INDEX 101
  • 68 LEFT 16px POSITION absolute
    TOP 23px"gt
  • 69 Please enter a phone number in
    the form 555-4567
  • 70 lt/spangt
  • 71
  • 72 ltscript language"javascript"gt
  • 73 lt!--
  • 74 var Page_Validators new Array(
  • 75 document.all"phoneNumberValid
    ator",

43
Generator.html
  • 89 if ( typeof(Page_ValidationVer
    ) "undefined" )
  • 90 alert(
  • 91 "Unable to find script
    library "
  • 92 "'/aspnet_client/system_
    web/'"
  • 93 "'1_0_3215_11/WebUIValid
    ation.js'. "
  • 94 "Try placing this file
    manually, or "
  • 95 "reinstall by running
    'aspnet_regiis -c'.")
  • 96 else if ( Page_ValidationVer
    ! "125" )
  • 97 alert(
  • 98 "This page uses an
    incorrect version "
  • 99 "of WebUIValidation.js.
    The page "
  • 100 "expects version 125. "
  • 101 "The script library is
    "
  • 102 Page_ValidationVer
    ".")
  • 103 else
  • 104 ValidatorOnLoad()
  • 105
  • 106
  • 107 function ValidatorOnSubmit()

44
20.6 Session Tracking
  • Cookies
  • Text file stored by a Web site on a individuals
    computer that allows the site to track the
    actions of the visitor
  • Records sites that the user visits and identifies
    shopping preferences
  • Cookies can store name-value pairs
  • Web Server can never access cookies created
    outside the domain associated with that server
  • HttpSessionState
  • HttpSessionState objects can store any type of
    objects (not just Strings) as attribute values

45
OptionsPage.aspx
  • 1 lt-- Fig 20.24 OptionsPage.aspx
    --gt
  • 2 lt-- allows clients to select a programming
    language --gt
  • 3 lt-- to get recommendations
    --gt
  • 4
  • 5 lt_at_ Page Language"vb" AutoEventWireup"false
    "
  • 6 Codebehind"OptionsPage.aspx.vb"
  • 7 Inherits"Cookies.Cookie"gt
  • 8
  • 9 lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    Transitional//EN"gt
  • 10 ltHTMLgt
  • 11 ltHEADgt
  • 12 lttitlegtCookieslt/titlegt
  • 13 ltmeta content"Microsoft Visual
    Studio.NET 7.0"
  • 14 name"GENERATOR"gt
  • 15 ltmeta content"Visual Basic 7.0"
    name"CODE_LANGUAGE"gt
  • 16 ltmeta content"JavaScript"
    name"vs_defaultClientScript"gt
  • 17 ltmeta name"vs_targetSchema"
  • 18 content"http//schemas.microsoft.co
    m/intellisense/ie5"gt
  • 19 lt/HEADgt

46
OptionsPage.aspx
  • 36 ltaspListItem Value"C"gtClt/asp
    ListItemgt
  • 37 ltaspListItem Value"C"gtClt/aspLi
    stItemgt
  • 38 ltaspListItem Value"C"gtClt/as
    pListItemgt
  • 39 ltaspListItem Value"Python"gtPyth
    onlt/aspListItemgt
  • 40 lt/aspradiobuttonlistgt
  • 41
  • 42 ltasphyperlink id"recommendationsLi
    nk"
  • 43 style"Z-INDEX 110 LEFT 42px
    POSITION
  • 44 absolute TOP 90px"
    runat"server"
  • 45 Visible"False" NavigateUrl
  • 46 "RecommendationPage.aspx"gt
  • 47 Click here to get book
    recommendations
  • 48 lt/asphyperlinkgt
  • 49
  • 50 ltasphyperlink id"languageLink"
    style"Z-INDEX
  • 51 109 LEFT 42px POSITION
    absolute
  • 52 TOP 55px" runat"server"
    Visible"False"
  • 53 NavigateUrl"OptionsPage.aspx"gt
  • 54 Click here to choose another
    language

47
OptionsPage.aspx
  • 1 ' Fig. 20.25 OptionsPage.aspx.vb
  • 2 ' Page that allows the user to choose a
    different language
  • 3
  • 4 Imports System.Web.UI.WebControls
  • 5
  • 6 Public Class Cookie
  • 7 Inherits System.Web.UI.Page
  • 8 Protected WithEvents languageLink As
    HyperLink
  • 9 Protected WithEvents recommendationsLink
    As HyperLink
  • 10 Protected WithEvents promptLabel As Label
  • 11 Protected WithEvents LanguageList As
    RadioButtonList
  • 12 Protected WithEvents welcomeLabel As
    Label
  • 13 Protected WithEvents submitButton As
    Button
  • 14 Private books New Hashtable()
  • 15
  • 16 ' Visual Studio .NET generated code
  • 17
  • 18 Private Sub Page_Init(ByVal sender As
    System.Object, _
  • 19 ByVal e As System.EventArgs) Handles
    MyBase.Init

48
OptionsPage.aspx
  • 31 Private Sub Page_Load(ByVal sender As
    System.Object, _
  • 32 ByVal e As System.EventArgs) Handles
    MyBase.Load
  • 33
  • 34 If IsPostBack Then
  • 35
  • 36 ' if postback is True, user has
    submitted information
  • 37 ' display welcome message and
    appropriate hyperlinks
  • 38 welcomeLabel.Visible True
  • 39 languageLink.Visible True
  • 40 recommendationsLink.Visible True
  • 41
  • 42 ' hide option information
  • 43 submitButton.Visible False
  • 44 promptLabel.Visible False
  • 45 LanguageList.Visible False
  • 46
  • 47 If (LanguageList.SelectedItem Is
    Nothing) False Then
  • 48 welcomeLabel.Text " " _
  • 49 LanguageList.SelectedItem.Text
    .ToString "."

49
OptionsPage.aspx
  • 62 ' if choice was made by user
  • 63 If (LanguageList.SelectedItem Is
    Nothing) False Then
  • 64 language LanguageList.SelectedItem
    .ToString()
  • 65 ISBN books(language).ToString()
  • 66
  • 67 ' create cookie, name/value pair is
  • 68 ' language chosen and ISBN number
    from Hashtable
  • 69 cookie New HttpCookie(language,
    ISBN)
  • 70
  • 71 ' add cookie to response,
  • 72 ' thus placing it on user's machine
  • 73 Response.Cookies.Add(cookie)
  • 74 End If
  • 75
  • 76 End Sub ' submitButton_Click
  • 77 End Class ' Cookie

50
20.6 Session Tracking
51
20.6 Session Tracking
52
RecomendationPage.aspx
  • 1 lt-- Fig 20.26 RecommendationPage.aspx
    --gt
  • 2 lt-- displays book recommendations using
    cookies --gt
  • 3
  • 4 lt_at_ Page Language"vb" AutoEventWireup"false
    "
  • 5 Codebehind"RecommendationPage.aspx.vb"
  • 6 Inherits"Cookies.Recommendations"gt
  • 7
  • 8 lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    Transitional//EN"gt
  • 9 ltHTMLgt
  • 10 ltHEADgt
  • 11 lttitlegtBook recommendationslt/titlegt
  • 12 ltmeta content"Microsoft Visual
    Studio.NET 7.0"
  • 13 name"GENERATOR"gt
  • 14 ltmeta content"Visual Basic 7.0"
    name"CODE_LANGUAGE"gt
  • 15 ltmeta content"JavaScript"
    name"vs_defaultClientScript"gt
  • 16 ltmeta name"vs_targetSchema"
  • 17 content"http//schemas.microsoft.co
    m/intellisense/ie5"gt
  • 18 lt/HEADgt
  • 19 ltbody MS_POSITIONING"GridLayout"gt

53
RecomedationsPage.aspx.vb
  • 1 ' Fig. 20.27 RecommendationsPage.aspx.vb
  • 2 ' Reading cookie data from the client
  • 3
  • 4 Imports System.Web.UI.WebControls
  • 5
  • 6 Public Class Recommendations
  • 7 Inherits Page
  • 8 Protected WithEvents recommendationsLabel
    As Label
  • 9 Protected WithEvents booksListBox As
    ListBox
  • 10
  • 11 ' Visual Studio .NET generated code
  • 12
  • 13 Private Sub Page_Init(ByVal sender As
    System.Object, _
  • 14 ByVal e As System.EventArgs) Handles
    MyBase.Init
  • 15
  • 16 InitializeComponent()
  • 17
  • 18 ' retrieve client's cookies
  • 19 Dim cookies As HttpCookieCollection

54
RecomedationsPage.aspx.vb
  • 35 ' if no cookies besides ID, no
    options were
  • 36 ' chosen. no recommendations made
  • 37 Else
  • 38 recommendationsLabel.Text "No
    Recommendations"
  • 39 booksListBox.Items.Clear()
  • 40 booksListBox.Visible False
  • 41 End If
  • 42
  • 43 End Sub ' Page_Init
  • 44
  • 45 Private Sub Page_Load(ByVal sender As
    System.Object, _
  • 46 ByVal e As System.EventArgs) Handles
    MyBase.Load
  • 47
  • 48 'Put user code to initialize the page
    here
  • 49 End Sub ' Page_Load
  • 50 End Class ' Recommendations

55
20.6 Session Tracking
56
OptionsPage.aspx
  • 1 lt-- Fig. 20.29 OptionsPage.aspx --gt
  • 2 lt-- displays a list of book options --gt
  • 3
  • 4 lt_at_ Page Language"vb" AutoEventWireup"false
    "
  • 5 Codebehind"OptionsPage.aspx.vb"
  • 6 Inherits"Sessions.Options2"gt
  • 7
  • 8 lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    Transitional//EN"gt
  • 9 ltHTMLgt
  • 10 ltHEADgt
  • 11 lttitlegtSession Trackinglt/titlegt
  • 12 ltmeta name"GENERATOR"
  • 13 content"Microsoft Visual
    Studio.NET 7.0"gt
  • 14 ltmeta name"CODE_LANGUAGE"
    content"Visual Basic 7.0"gt
  • 15 ltmeta name"vs_defaultClientScript"
    content"JavaScript"gt
  • 16 ltmeta name"vs_targetSchema"
  • 17 content"http//schemas.microsoft.co
    m/intellisense/ie5"gt
  • 18 lt/HEADgt
  • 19 ltbody MS_POSITIONING"GridLayout"gt

57
OptionsPage.aspx
  • 33 ltaspLabel id"idLabel"
    style"Z-INDEX 107
  • 34 LEFT 42px POSITION absolute
    TOP 66px"
  • 35 runat"server"gt
  • 36 lt/aspLabelgt
  • 37
  • 38 ltaspradiobuttonlist
    id"LanguageList" style"Z-INDEX 105
  • 39 LEFT 43px POSITION absolute
    TOP 69px" runat"server"gt
  • 40
  • 41 ltaspListItem Value"Visual
    Basic .NET"
  • 42 Selected"True"gtVisual Basic
    .NETlt/aspListItemgt
  • 43
  • 44 ltaspListItem Value"C"gtClt/asp
    ListItemgt
  • 45 ltaspListItem Value"C"gtClt/aspLi
    stItemgt
  • 46 ltaspListItem Value"C"gtClt/as
    pListItemgt
  • 47 ltaspListItem Value"Python"gtPyth
    onlt/aspListItemgt
  • 48 lt/aspradiobuttonlistgt
  • 49
  • 50 ltasphyperlink id"recommendationsLi
    nk" style"Z-INDEX 104
  • 51 LEFT 42px POSITION absolute
    TOP 172px"

58
OptionsPage.aspx
  • 64 ltasplabel id"welcomeLabel"
    style"Z-INDEX 102
  • 65 LEFT 42px POSITION absolute
    TOP 32px"
  • 66 runat"server" Visible"False"
    Font-Bold"True"
  • 67 Font-Size"Large"gtWelcome to
    sessions! You selected
  • 68 lt/asplabelgt
  • 69
  • 70 ltaspbutton id"submitButton"
    style"Z-INDEX 101
  • 71 LEFT 42px POSITION absolute
    TOP 207px"
  • 72 runat"server" Text"Submit"gt
  • 73 lt/aspbuttongt
  • 74
  • 75 lt/formgt
  • 76 lt/bodygt
  • 77 lt/HTMLgt

59
OptionsPge2.aspx.vb
  • 1 ' Fig. 20.30 OptionsPge2.aspx.vb
  • 2 ' A listing of programming languages,
  • 3 ' cookie is created based on choice made.
  • 4
  • 5 Imports System.Web.UI.WebControls
  • 6
  • 7 Public Class Options2
  • 8 Inherits System.Web.UI.Page
  • 9 Protected WithEvents languageLink As
    HyperLink
  • 10 Protected WithEvents recommendationsLink
    As HyperLink
  • 11 Protected WithEvents LanguageList As
    RadioButtonList
  • 12 Protected WithEvents idLabel As Label
  • 13 Protected WithEvents timeOutLabel As
    Label
  • 14 Protected WithEvents promptLabel As Label
  • 15 Protected WithEvents welcomeLabel As
    Label
  • 16 Protected WithEvents submitButton As
    Button
  • 17 Private books New Hashtable()
  • 18
  • 19 ' Visual Studio .NET generated code

60
OptionsPge2.aspx.vb
  • 34 Private Sub Page_Load(ByVal sender As
    System.Object, _
  • 35 ByVal e As System.EventArgs)
    Handles MyBase.Load
  • 36
  • 37 If IsPostBack Then
  • 38
  • 39 ' if postback is True, user has
    submitted information
  • 40 ' display welcome message and
    appropriate hyperlinks
  • 41 welcomeLabel.Visible True
  • 42 languageLink.Visible True
  • 43 recommendationsLink.Visible True
  • 44
  • 45 ' hide option information
  • 46 submitButton.Visible False
  • 47 promptLabel.Visible False
  • 48 LanguageList.Visible False
  • 49
  • 50 If (LanguageList.SelectedItem Is
    Nothing) False Then
  • 51 welcomeLabel.Text " " _
  • 52 LanguageList.SelectedItem.Text
    .ToString "."

61
OptionsPge2.aspx.vb
  • 67 Private Sub submitButton_Click(ByVal
    sender As System.Object, _
  • 68 ByVal e As System.EventArgs) Handles
    submitButton.Click
  • 69
  • 70 Dim language, ISBN As String
  • 71
  • 72 ' if choice was made by user
  • 73 If (LanguageList.SelectedItem Is
    Nothing) False Then
  • 74 language LanguageList.SelectedItem
    .ToString()
  • 75 ISBN books(language).ToString()
  • 76
  • 77 ' add name/value pair to Session
  • 78 Session.Add(language, ISBN)
  • 79 End If
  • 80
  • 81 End Sub ' submitButton_Click
  • 82 End Class ' Options2

62
20.6 Session Tracking
63
20.6 Session Tracking
64
RecomendationPage.aspx
  • 1 lt-- Fig. 20.32 RecommendationPage.aspx
    --gt
  • 2 lt-- displays book recommendations based on
    session --gt
  • 3 lt-- information
    --gt
  • 4
  • 5 lt_at_ Page Language"vb" AutoEventWireup"false
    "
  • 6 Codebehind"RecommendationPage.aspx.vb"
  • 7 Inherits"Sessions.Recommendations" gt
  • 8
  • 9 lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    Transitional//EN"gt
  • 10 ltHTMLgt
  • 11 ltHEADgt
  • 12 ltmeta content"Microsoft Visual
    Studio.NET 7.0"
  • 13 name"GENERATOR"gt
  • 14 ltmeta content"Visual Basic 7.0"
    name"CODE_LANGUAGE"gt
  • 15 ltmeta content"JavaScript"
    name"vs_defaultClientScript"gt
  • 16 ltmeta name"vs_targetSchema"
  • 17 content"http//schemas.microsoft.co
    m/intellisense/ie5"gt
  • 18 lt/HEADgt
  • 19 ltbody MS_POSITIONING"GridLayout"gt

65
RecommendationsPage.aspx.vb
  • 1 ' Fig. 20.33 RecommendationPage.aspx.vb
  • 2 ' Reading cookie data from the client
  • 3
  • 4 Imports System.Web.UI.WebControls
  • 5
  • 6 Public Class Recommendations
  • 7 Inherits Page
  • 8 Protected WithEvents recommendationLabel
    As Label
  • 9 Protected WithEvents booksListBox As
    ListBox
  • 10
  • 11 ' Visual Studio .NET generated code
  • 12
  • 13 Private Sub Page_Init(ByVal sender As
    System.Object, _
  • 14 ByVal e As System.EventArgs) Handles
    MyBase.Init
  • 15
  • 16 InitializeComponent()
  • 17
  • 18 Dim i As Integer
  • 19 Dim keyName As String

66
RecommendationsPage.aspx.vb
  • 31 ' use current key to display
  • 32 ' Session's name/value pairs
  • 33 booksListBox.Items.Add(keyName
    _
  • 34 " How to Program. ISBN "
    _
  • 35 Session(keyName))
  • 36 Next
  • 37 Else
  • 38 recommendationLabel.Text "No
    Recommendations"
  • 39 booksListBox.Visible False
  • 40 End If
  • 41 End Sub ' Page_Init
  • 42
  • 43 Private Sub Page_Load(ByVal sender As
    System.Object, _
  • 44 ByVal e As System.EventArgs) Handles
    MyBase.Load
  • 45
  • 46 'Put user code to initialize the page
    here
  • 47 End Sub ' Page_Load
  • 48 End Class ' Recommendations

67
20.7 Case Study Online Guest book
  • Guest Book
  • Data submitted on the guest-book form often are
    stored in a database located on the Web servers
    machine

68
20.7 Case Study Online Guest book
69
GuestbookPage.aspx
  • 1 lt-- Fig 20.35 GuestbookPage.aspx
    --gt
  • 2 lt-- controls and layout for guestbook
    application --gt
  • 3
  • 4 lt_at_ Page Language"vb" AutoEventWireup"false
    "
  • 5 Codebehind"GuestbookPage.aspx.vb"
  • 6 Inherits"Guestbook.Guestbook"gt
  • 7
  • 8 lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    Transitional//EN"gt
  • 9 ltHTMLgt
  • 10 ltHEADgt
  • 11 lttitlegtGuestBooklt/titlegt
  • 12 ltmeta content"Microsoft Visual
    Studio.NET 7.0"
  • 13 name"GENERATOR"gt
  • 14 ltmeta content"Visual Basic 7.0"
    name"CODE_LANGUAGE"gt
  • 15 ltmeta content"JavaScript"
    name"vs_defaultClientScript"gt
  • 16 ltmeta content
  • 17 "http//schemas.microsoft.com/intell
    isense/ie5"
  • 18 name"vs_targetSchema"gt
  • 19 lt/HEADgt

70
GuestbookPage.aspx
  • 35 ltaspButton id"submitButton"
    style"Z-INDEX 109
  • 36 LEFT 187px POSITION absolute
    TOP 319px"
  • 37 runat"server" Text"Submit"gt
  • 38 lt/aspButtongt
  • 39
  • 40 ltaspDataGrid id"DataGrid1"
    style"Z-INDEX 108
  • 41 LEFT 39px POSITION absolute
    TOP 372px"
  • 42 runat"server" Width"541px"
    Height"95px"
  • 43 HorizontalAlign"Left"
    BorderColor"E7E7FF"
  • 44 BorderWidth"1px"
    GridLines"None" CellPadding"3"
  • 45 PageSize"5"gt
  • 46
  • 47 ltSelectedItemStyle
    ForeColor"F7F7F7"
  • 48 BackColor"738A9C"gt
  • 49 lt/SelectedItemStylegt
  • 50 ltAlternatingItemStyle
    BackColor"F7F7F7"gt
  • 51 lt/AlternatingItemStylegt
  • 52 ltItemStyle HorizontalAlign"Left"
    Width"100px"
  • 53 ForeColor"4A3C8C"
    BackColor"E7E7FF"gt

71
GuestbookPage.aspx
  • 67 ltaspTextBox id"messageTextBox"
    style"Z-INDEX 107
  • 68 LEFT 135px POSITION absolute
    TOP 181px"
  • 69 runat"server" Width"449px"
    Height"113px"
  • 70 TextMode"MultiLine"gt
  • 71 lt/aspTextBoxgt
  • 72
  • 73 ltaspTextBox id"emailTextBox"
    style"Z-INDEX 106
  • 74 LEFT 135px POSITION absolute
    TOP 132px"
  • 75 runat"server" Width"449px"gt
  • 76 lt/aspTextBoxgt
  • 77
  • 78 ltaspTextBox id"nameTextBox"
    style"Z-INDEX 105
  • 79 LEFT 135px POSITION absolute
    TOP 85px"
  • 80 runat"server" Width"449px"gt
  • 81 lt/aspTextBoxgt
  • 82
  • 83 ltaspLabel id"messageLabel"
    style"Z-INDEX 104
  • 84 LEFT 39px POSITION absolute
    TOP 167px"
  • 85 runat"server" Width"51px"gtTell
    the world

72
GuestbookPage.aspx
  • 1 ' Fig. 20.36 GuestbookPage.aspx
  • 2 ' The code-behind file for the guest book
    page.
  • 3
  • 4 Imports System.Web.UI.WebControls
  • 5 Imports System.Data
  • 6 Imports System.IO
  • 7
  • 8 ' allows users to leave message
  • 9 Public Class Guestbook
  • 10 Inherits System.Web.UI.Page
  • 11
  • 12 Protected WithEvents promptLabel As Label
  • 13 Protected WithEvents nameLabel As Label
  • 14 Protected WithEvents emailLabel As Label
  • 15 Protected WithEvents messageLabel As
    Label
  • 16 Protected WithEvents dataGrid As DataGrid
  • 17 Protected WithEvents submitButton As
    Button
  • 18 Protected WithEvents messageTextBox As
    TextBox
  • 19 Protected WithEvents emailTextBox As
    TextBox

73
GuestbookPage.aspx
  • 34 ' clear text boxes user can enter new
    input
  • 35 Private Sub clearButton_Click(ByVal
    sender As System.Object, _
  • 36 ByVal e As System.EventArgs) Handles
    clearButton.Click
  • 37
  • 38 nameTextBox.Text ""
  • 39 emailTextBox.Text ""
  • 40 messageTextBox.Text ""
  • 41 End Sub ' clearButton_Click
  • 42
  • 43 Public Sub FillMessageTable()
  • 44 Dim table As New DataTable()
  • 45 Dim reader As StreamReader
  • 46 Dim separator As Char()
  • 47 Dim message As String
  • 48 Dim parts As String()
  • 49
  • 50 table dataView.Table
  • 51
  • 52 table.Columns.Add("Date")

74
GuestbookPage.aspx
  • 66 While message ltgt ""
  • 67
  • 68 ' split String into four parts
  • 69 parts message.Split(separator)
  • 70
  • 71 ' load data into table
  • 72 table.LoadDataRow(parts, True)
  • 73
  • 74 ' read one line from file
  • 75 message reader.ReadLine()
  • 76 End While
  • 77
  • 78 dataGrid.DataBind() ' update grid
  • 79
  • 80 reader.Close()
  • 81 End Sub ' FillMessageTable
  • 82
  • 83 ' add the user's entry to the guestbook
  • 84 Private Sub submitButton_Click(ByVal
    sender As System.Object, _

75
  • 100 ' clear textboxes and close stream
  • 101 nameTextBox.Text ""
  • 102 emailTextBox.Text ""
  • 103 messageTextBox.Text ""
  • 104 guestbook.Close()
  • 105
  • 106 FillMessageTable()
  • 107 End Sub ' submitButton_Click
  • 108 End Class ' Guestbook

76
20.7 Case Study Online Guest book
77
20.8 Case Study Connecting to a Database in ASP
.NET
  • ASP .NET
  • Login.aspx
  • Retrieves valid user name for this site through
    Login.mdb and all the author information is
    retrieved from Books.mdb database

78
Login.aspx
  • 1 lt-- Fig. 20.37 login.aspx
    --gt
  • 2 lt-- controls and formatting for login page
    --gt
  • 3
  • 4 lt_at_ Register TagPrefix"Header"
    TagName"ImageHeader"
  • 5 Src"ImageHeader.ascx" gt
  • 6
  • 7 lt_at_ Page Language"vb" AutoEventWireup"false
    "
  • 8 Codebehind"login.aspx.vb"
  • 9 Inherits"Database.Login"gt
  • 10
  • 11 lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    Transitional//EN"gt
  • 12 ltHTMLgt
  • 13 ltHEADgt
  • 14 lttitlegtlt/titlegt
  • 15 ltmeta content"Microsoft Visual
    Studio.NET 7.0"
  • 16 name"GENERATOR"gt
  • 17 ltmeta content"Visual Basic 7.0"
    name"CODE_LANGUAGE"gt
  • 18 ltmeta content"JavaScript"
    name"vs_defaultClientScript"gt
  • 19 ltmeta name"vs_targetSchema"

79
Login.aspx
  • 36 ltaspdropdownlist id"nameList"
    style"Z-INDEX 105
  • 37 LEFT 92px POSITION absolute
    TOP 185px"
  • 38 runat"server" Width"154px"gt
  • 39 lt/aspdropdownlistgt
  • 40
  • 41 ltasplabel id"passwordLabel"
    style"Z-INDEX 102
  • 42 LEFT 15px POSITION absolute
    TOP 220px"
  • 43 runat"server"gtPassword
  • 44 lt/asplabelgt
  • 45
  • 46 ltasptextbox id"passwordTextBox"
    style"Z-INDEX 103
  • 47 LEFT 92px POSITION absolute
    TOP 221px"
  • 48 runat"server"
    TextMode"Password"gt
  • 49 lt/asptextboxgt
  • 50
  • 51 ltaspcustomvalidator
    id"invalidPasswordValidator"
  • 52 style"Z-INDEX 107 LEFT
    262px POSITION
  • 53 absolute TOP 221px"
    runat"server"
  • 54 ControlToValidate"passwordTextBo
    x" Font-Bold"True"

80
Login.aspxImageHeader.ascx
  • 71 ltHeaderImageHeader
    id"ImageHeader1" runat"server"gt
  • 72 lt/HeaderImageHeadergt
  • 73
  • 74 lt/formgt
  • 75 lt/bodygt
  • 76 lt/HTMLgt

1 lt-- Fig. 20.38 ImageHeader.ascx
--gt 2 lt-- Listing for the header user
control --gt 3 4 lt_at_ Control Language"vb"
AutoEventWireup"false" 5
Codebehind"ImageHeader.ascx.vb" 6
Inherits"Database.ImageHeader" 7
TargetSchema"http//schemas.microsoft.com/intelli
sense/ie5" gt 8 9 ltaspImage id"Image1"
runat"server" 10 ImageUrl"http//localhost
/Database/bug2bug.png"gt 11 lt/aspImagegt
81
Login.aspx.vb
  • 1 ' Fig. 20.39 Login.aspx.vb
  • 2 ' The code-behind file for the page that
    logs the user in.
  • 3
  • 4 Imports System
  • 5 Imports System.Collections
  • 6 Imports System.ComponentModel
  • 7 Imports System.Data
  • 8 Imports System.Data.OleDb
  • 9 Imports System.Drawing
  • 10 Imports System.Web
  • 11 Imports System.Web.SessionState
  • 12 Imports System.Web.UI
  • 13 Imports System.Web.UI.WebControls
  • 14 Imports System.Web.UI.HtmlControls
  • 15 Imports System.Web.Security
  • 16
  • 17 Public Class Login
  • 18 Inherits System.Web.UI.Page
  • 19

82
Login.aspx.vb
  • 31 Protected WithEvents OleDbDataAdapter1 As
    OleDbDataAdapter
  • 32 Protected WithEvents OleDbSelectCommand1
    As OleDbCommand
  • 33 Protected WithEvents OleDbInsertCommand1
    As OleDbCommand
  • 34 Protected WithEvents OleDbUpdateCommand1
    As OleDbCommand
  • 35 Protected WithEvents OleDbDeleteCommand1
    As OleDbCommand
  • 36 Protected WithEvents OleDbConnection1 As
    OleDbConnection
  • 37 Protected WithEvents promptLabel As Label
  • 38 Protected dataReader As OleDbDataReader
  • 39
  • 40 ' Visual Studio .NET generated code
  • 41
  • 42 Private Sub Page_Init(ByVal sender As
    System.Object, _
  • 43 ByVal e As System.EventArgs) Handles
    MyBase.Init
  • 44
  • 45 InitializeComponent()
  • 46
  • 47 ' if page loads due to postback,
    process information
  • 48 ' otherwise, page is loading for first
    time, so
  • 49 ' do nothing

83
Login.aspx.vb
  • 67 ' catch error if database cannot be
    opened
  • 68 Catch exception As OleDbException
  • 69 Response.Write("Unable to open
    database!")
  • 70
  • 71 ' close database
  • 72 Finally
  • 73 ' close database connection
  • 74 OleDbConnection1.Close()
  • 75 End Try
  • 76 End If
  • 77 End Sub ' Page_Init
  • 78
  • 79 ' validate user name and password
  • 80 Private Sub invalidPasswordValidator_Serve
    rValidate( _
  • 81 ByVal source As Object, _
  • 82 ByVal args As ServerValidateEventArgs)
    _
  • 83 Handles invalidPasswordValidator.Server
    Validate
  • 84
  • 85 ' open database and check password

84
Login.aspx.vb
  • 101 ' if password user provided is
    correct create
  • 102 ' authentication ticket for user
    and redirect
  • 103 ' user to Authors.aspx otherwise
    set IsValid to false
  • 104 If args.Value dataReader.GetString
    (1) Then
  • 105 FormsAuthentication.SetAuthCookie
    ( _
  • 106 Request.Form("namelist"),
    False)
  • 107 Session.Add("name", _
  • 108 Request.Form("nameList").T
    oString())
  • 109 Response.Redirect("Authors.aspx")
  • 110 Else
  • 111 args.IsValid False
  • 112
  • 113 End If
  • 114
  • 115 ' display error if unable to open
    database
  • 116 Catch exception As OleDbException
  • 117 Response.Write("Unable to open
    database!")
  • 118
  • 119 ' close database

85
20.8 Case Study Connecting to a Database in ASP
.NET
86
20.8 Case Study Connecting to a Database in ASP
.NET
87
Authors.aspx
  • 1 lt-- Fig. 20.40 Authors.aspx
    --gt
  • 2 lt-- displays book titles based on author
    name --gt
  • 3 lt-- from database
    --gt
  • 4
  • 5 lt_at_ Page Language"vb" AutoEventWireup"false
    "
  • 6 Codebehind"Authors.aspx.vb"
  • 7 Inherits"Database.Authors"gt
  • 8
  • 9 lt_at_ Register TagPrefix"Header"
    TagName"ImageHeader"
  • 10 Src"ImageHeader.ascx" gt
  • 11
  • 12 lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    Transitional//EN"gt
  • 13 ltHTMLgt
  • 14 ltHEADgt
  • 15 lttitlegtAuthorslt/titlegt
  • 16 ltmeta name"GENERATOR"
  • 17 content"Microsoft Visual
    Studio.NET 7.0"gt
  • 18 ltmeta name"CODE_LANGUAGE"
    content"Visual Basic 7.0"gt
  • 19 ltmeta name"vs_defaultClientScript"
    content"JavaScript"gt

88
Authors.aspx
  • 32 ltEditItemStyle
    BackColor"White"gtlt/EditItemStylegt
  • 33 ltAlternatingItemStyle
    ForeColor"Black"
  • 34 BackColor"LightGoldenrodYello
    w"gt
  • 35 lt/AlternatingItemStylegt
  • 36 ltItemStyle BackColor"White"gtlt/It
    emStylegt
  • 37 ltHeaderStyle BackColor"LightGree
    n"gtlt/HeaderStylegt
  • 38 ltPagerStyle NextPageText"Next
    ampgt"
  • 39 PrevPageText"amplt
    Previous"gt
  • 40 lt/PagerStylegt
  • 41 lt/aspDataGridgt
  • 42
  • 43 ltaspButton id"Button1"
    style"Z-INDEX 104
  • 44 LEFT 29px POSITION absolute
    TOP 188px"
  • 45 runat"server" Width"78px"
    Text"Select"gt
  • 46 lt/aspButtongt
  • 47
  • 48 ltaspDropDownList id"nameList"
    style"Z-INDEX 103
  • 49 LEFT 90px POSITION absolute
    TOP 157px"
  • 50 runat"server" Width"158px"
    Height"22px"gt

Slide
Write a Comment
User Comments (0)
About PowerShow.com