Chapter 20 ASP Net Web Forms and Web
Chapter 20 - ASP. Net, Web Forms and Web Controls Outline 20. 1 20. 2 20. 3 20. 4 20. 5 20. 6 20. 7 20. 8 20. 9 Introduction Simple HTTP Transaction System Architecture Creating and Running a Simple Web Form Example Web Controls 20. 5. 1 Text and Graphics Controls 20. 5. 2 Ad. Rotator Control 20. 5. 3 Validation Controls Session Tracking 20. 6. 1 Cookies 20. 6. 2 Session Tracking with Http. Session. State Case Study: Online Guest book Case Study: Connecting to a Database in ASP. NET Tracing 2002 Prentice Hall. All rights reserved. 1
2 20. 1 Introduction • Web-Based Application Development – Creates Web content for Web browser clients • Hyper. Text 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 2002 Prentice Hall. All rights reserved.
3 20. 2 Simple HTTP Transaction • Hyper. Text 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 2002 Prentice Hall. All rights reserved.
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 2002 Prentice Hall. All rights reserved.
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 2002 Prentice Hall. All rights reserved. 5
1 <%-- Fig. 20. 4: Web. Time. aspx --%> Indicates the figure number, 2 <%-- A page that contains two labels --%> the file name and the purpose 3 Document type declaration 4 <%@ Page Language="vb" Auto. Event. Wireup="false" of the file 5 Codebehind="Web. Time. aspx. vb" Inherits="Web. Timer" %> 6 7 <!DOCTYPE HTML PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN" > Specifies the information needed by 8 <HTML> Common Language Runtime (CLR) 9 <HEAD> 10 <title>Web. Time</title> <HTML> and <HEAD> start tags 11 <meta name="GENERATOR" 12 content="Microsoft Visual Studio. NET 7. 0" > Identifies meta element and 13 <meta name="CODE_LANGUAGE" content="Visual Basic 7. 0"> content saves meta element’s data 14 <meta name="vs_default. Client. Script" content="Java. Script"> Begins HTML body 15 <meta name="vs_target. Schema" 16 content="http: //schemas. microsoft. com/ intellisense/ie 5"> Indicates form executes on a server 17 </HEAD> 18 <body MS_POSITIONING= "Grid. Layout"> 19 <form id="Form 1" method="post" runat="server"> Label Web Control Web. Time. aspx 20 21 <asp: Label id="display. Label" style="Z-INDEX: 101; 22 LEFT: 42 px; POSITION: absolute; TOP: 36 px" Output Message 23 runat="server" Width="186 px"> 24 A Simple Web Form Example 25 </asp: Label> 26 27 <asp: Label id="time. Label" style="Z-INDEX: 102; 28 LEFT: 33 px; POSITION: absolute; TOP: 84 px" 29 runat="server" Width="225 px" Height="55 px" 30 Fore. Color="#C 0 FFC 0" Back. Color="Black" 31 Font-Size="XX-Large"> 32 </asp: Label> 33 34 </form> 35 </body> 2002 Prentice Hall. 36 </HTML> All rights reserved. Outline 6
Outline 1 ' Fig. 20. 5: Web. Form 1. aspx. vb 2 ' The code-behind file for a page 3 ' that displays the current time. Specifies namespaces that contain classes 4 5 Imports System for developing Web-based applications 6 Imports System. Web 7 Imports System. Web. UI 8 Imports System. Web. UI. Web. Controls Begins class definition of Web. Timer 9 10 Public Class Web. Timer and System. Web. UI. Page 11 Inherits System. Web. UI. Page 12 13 Protected With. Events display. Label As _ 14 System. Web. UI. Web. Controls. Label Declares references to two Labels 15 16 Protected With. Events time. Label As _ 17 System. Web. UI. Web. Controls. Label Declares initial properties of 18 Web. Form 1. aspx 19 'This call is required by the Web Form Designer. application components 20 Private Sub Initialize. Component() 21 End Sub 22 23 Private Sub Page_Init(By. Val sender As System. Object, _ 24 By. Val e As System. Event. Args) Handles My. Base. Init Defines method Page_Init 25 26 Initialize. Component () 27 28 time. Label. Text = _ 29 String. Format("{0: D 2}: {1: D 2}: {2: D 2}", _ 30 Date. Time. Now. Hour, Date. Time. Now. Minute, _ 31 Date. Time. Now. Second) 32 End Sub ' Page_Init 2002 Prentice Hall. All rights reserved. 7
34 Private Sub Page_Load(By. Val sender As System. Object, _ 35 By. Val e As System. Event. Args) Handles My. Base. Load 36 'Put user code to initialize the page here 37 End Sub ' Page_Load 38 End Class ' Web. Timer Outline Web. Form 1. aspx 2002 Prentice Hall. All rights reserved. 8
1 <!-- Fig. 20. 6: Web. Time. html --> 2 <!-- The HTML generated when Web. Time is loaded. --> 3 4 <!DOCTYPE HTML PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN" > 5 6 <HTML> 7 <HEAD> 8 <title>Web. Time</title> 9 <meta name="GENERATOR" 10 Content="Microsoft Visual Studio 7. 0" > 11 <meta name="CODE_LANGUAGE" Content="Visual Basic 7. 0"> 12 <meta name="vs_default. Client. Script" content="Java. Script"> 13 <meta name="vs_target. Schema" Defines the body of the document 14 content="http: //schemas. microsoft. com/ intellisense/ie 5"> 15 </HEAD> 16 Hidden inputs from the user 17 <body MS_POSITIONING="Grid. Layout"> 18 <form name="Form 1" method="post" 19 action="Web. Time. aspx" id="Form 1"> 20 <input type="hidden" name="__VIEWSTATE" 21 value="d. Dwt. Nj. A 2 MTkw. MTQ 5 Ozs+" /> 22 23 <span id="prompt. Label" 24 style="font-size: Medium; Z-INDEX: 101; LEFT: 25 px; 25 POSITION: absolute; TOP: 23 px" > 26 A Simple Web Form Example 27 </span> 28 <span id="time. Label" style="color: Lime. Green; 29 background-color: Black; font-size: XX-Large; 30 Z-INDEX: 102; LEFT: 25 px; POSITION: absolute; 31 TOP: 55 px">10: 39: 35</span> 32 </form> 33 </body> 34 </HTML> Outline Wb. Time. html 2002 Prentice Hall. All rights reserved. 9
20. 4 Creating and Running a Simple Web – Form Example 2002 Prentice Hall. All rights reserved. 10
20. 4 Creating and Running a Simple Web – Form Example 2002 Prentice Hall. All rights reserved. 11
20. 4 Creating and Running a Simple Web – Form Example 2002 Prentice Hall. All rights reserved. 12
20. 4 Creating and Running a Simple Web – Form Example 2002 Prentice Hall. All rights reserved. 13
20. 4 Creating and Running a Simple Web – Form Example 2002 Prentice Hall. All rights reserved. 14
20. 4 Creating and Running a Simple Web – Form Example 2002 Prentice Hall. All rights reserved. 15
16 20. 5 Web Controls • Text and Graphics Control – Label, Button, Text. Box, Image Radio. Button. List and Drop. Down. List • Ad. Rotator 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 2002 Prentice Hall. All rights reserved.
17 20. 5 Web Controls 2002 Prentice Hall. All rights reserved.
1 <%-- Fig. 20. 17: Conrols. aspx --%> 2 <%-- Demonstrates web controls --%> 3 4 <%@ Page Language="vb" Auto. Event. Wireup="false" 5 Codebehind="Controls. aspx. vb" Document type declaration 6 Inherits="Controls. Web. Form 1" 7 enable. View. State=“False" Enabled. Session. State=“False" %> 8 9 <!DOCTYPE HTML PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN" > 10 <HTML> 11 <HEAD> 12 <title>Web. Form 1</title> 13 <meta name="GENERATOR" 14 content="Microsoft Visual Studio. NET 7. 0" > 15 <meta name="CODE_LANGUAGE" content="Visual Basic 7. 0"> 16 <meta name="vs_default. Client. Script" content="Java. Script"> 17 <meta name="vs_target. Schema" 18 content="http: //schemas. microsoft. com/ intellisense/ie 5"> 19 </HEAD> 20 <body MS_POSITIONING= "Grid. Layout"> 21 <form id="Form 1" method="post" runat="server"> 22 23 <asp: Label id="Welcome. Label" style="Z-INDEX: 101; 24 LEFT: 44 px; POSITION: absolute; TOP: 27 px" 25 runat="server" Width="451 px" Height="28 px" 26 Font-Size="X-Large"> Label Web control 27 This is a simple registration form. 28 </asp: Label> 29 30 <asp: Label id="Register. Label" style="Z-INDEX: 102; 31 LEFT: 48 px; POSITION: absolute; TOP: 71 px" 32 runat="server" Width="376 px" Height="26 px" 33 Font-Italic= "True" Font-Size="Medium"> 34 Please fill in all fields and click Register. 35 </asp: Label> Outline Controls. aspx 2002 Prentice Hall. All rights reserved. 18
37 <asp: Image id="User. Label" style="Z-INDEX: 103; 38 LEFT: 42 px; POSITION: absolute; TOP: 135 px" 39 runat="server" Width="439 px" Height="28 px" 40 Image. Url="images/user. png"> 41 </asp: Image> 42 43 <asp: Label id="Fill. Label" style="Z-INDEX: 104; 44 LEFT: 50 px; POSITION: absolute; TOP: 189 px" 45 runat="server" Width="225 px" Fore. Color="Lime" 46 Font-Size="Medium"> Image control inserts an 47 Please fill out the fields below. image into a Web page 48 </asp: Label> 49 50 <asp: Image id="First. Image" style="Z-INDEX: 105; 51 LEFT: 49 px; POSITION: absolute; TOP: 224 px" 52 runat="server" Width="84 px" Height="36 px" 53 Image. Url="images/fname. png"> 54 </asp: Image> The Image. Url property specifies the 55 file location of the image to display 56 <asp: Image id="Email. Image" style="Z-INDEX: 106; 57 LEFT: 49 px; POSITION: absolute; TOP: 280 px" 58 runat="server" Width="86 px" Height="29 px" Text. Box control allows the programmer 59 Image. Url="images/email. png"> to read and display text 60 </asp: Image> 61 62 <asp: Text. Box id= "First. Text. Box" style="Z-INDEX: 107; 63 LEFT: 145 px; POSITION: absolute; TOP: 231 px" 64 runat="server" Width="115 px" Height="20 px"> 65 </asp: Text. Box> Outline Controls. aspx 2002 Prentice Hall. All rights reserved. 19
67 <asp: Text. Box id= "Email. Text. Box" style="Z-INDEX: 108; 68 LEFT: 147 px; POSITION: absolute; TOP: 284 px" 69 runat="server" Width="112 px" Height="18 px"> 70 </asp: Text. Box> 71 <asp: Image id="Last. Image" style="Z-INDEX: 109; 73 LEFT: 292 px; POSITION: absolute; TOP: 227 px" 74 runat="server" Width="77 px" Height="33 px" 75 Image. Url="images/lname. png"> 76 </asp: Image> 77 78 <asp: Image id="Phone. Image" style="Z-INDEX: 110; 79 LEFT: 292 px; POSITION: absolute; TOP: 273 px" 80 runat="server" Width="80 px" Height="30 px" 81 Image. Url="images/phone. png"> 82 </asp: Image> 83 84 <asp: Text. Box id= "Last. Text. Box" style="Z-INDEX: 111; 85 LEFT: 400 px; POSITION: absolute; TOP: 232 px" 86 runat="server" Width="109 px" Height="20 px"> 87 </asp: Text. Box> 88 89 <asp: Text. Box id= "Phone. Text. Box" style="Z-INDEX: 112; 90 LEFT: 399 px; POSITION: absolute; TOP: 277 px" 91 runat="server" Width="108 px" Height="18 px"> 92 </asp: Text. Box> 93 94 <asp: Label id="Phone. Label" style="Z-INDEX: 113; 95 LEFT: 309 px; POSITION: absolute; TOP: 318 px" 96 runat="server" Width="223 px" Height="18 px"> 97 Must be in the form (555)555 -5555. 98 </asp: Label> Outline Controls. aspx 2002 Prentice Hall. All rights reserved. 20
100 <asp: Image id="Publication. Image" style="Z-INDEX: 114; 101 LEFT: 50 px; POSITION: absolute; TOP: 356 px" 102 runat="server" Width="435 px" Height="27 px" 103 Image. Url="images/downloads. png"> 104 </asp: Image> 105 106 <asp: Label id="Booklabel" style="Z-INDEX: 115; 107 LEFT: 54 px; POSITION: absolute; TOP: 411 px" 108 runat="server" Width="348 px" Height="23 px" 109 Fore. Color="Lime" Font-Size="Medium"> Defines the List. Items that display 110 Which book would you like information about? 111 </asp: Label> the drop-down list is expanded 112 113 <asp: Drop. Down. List id= "Book. Drop. Down. List" 114 style="Z-INDEX: 116; LEFT: 60 px; POSITION: 115 absolute; TOP: 448 px" runat="server" 116 Width="326 px" Height="29 px"> 117 118 <asp: List. Item Value="XML How to Program 1 e"> 119 XML How to Program 1 e 120 </asp: List. Item> 121 <asp: List. Item Value="C# How to Program 1 e"> 122 C# How to Program 1 e 123 </asp: List. Item> 124 <asp: List. Item Value="Java How to Program 4 e" > 125 Java How to Program 4 e 126 </asp: List. Item> 127 <asp: List. Item Value= 128 "Advanced Java How to Program 1 e" > 129 Advanced Java How to Program 1 e 130 </asp: List. Item> 131 <asp: List. Item Value= 132 "Visual Basic. NET How to Program 2 e" > 133 Visual Basic. NET How to Program 2 e 134 </asp: List. Item> Outline when Controls. aspx 2002 Prentice Hall. All rights reserved. 21
135 <asp: List. Item Value="C++ How to Program 3 e"> 136 C++ How to Program 3 e 137 </asp: List. Item> Adds a hyperlink to a Web page 138 </asp: Drop. Down. List> 139 140 <asp: Hyper. Link id="Books. Hyper. Link" 141 style="Z-INDEX: 117; LEFT: 64 px; POSITION: 142 absolute; TOP: 486 px" runat="server" 143 Width="385 px" Height="22 px" 144 Navigate. Url= "http: //www. deitel. com"> 145 Click here to view more information about our books. 146 </asp: Hyper. Link> 147 148 <asp: Image id="Operating. Image" style="Z-INDEX: 118; 149 LEFT: 53 px; POSITION: absolute; TOP: 543 px" 150 runat="server" Width="431 px" Height="32 px" 151 Image. Url="images/os. png"> 152 </asp: Image> 153 154 <asp: Label id="Operating. Label" style="Z-INDEX: 119; 154 LEFT: 63 px; POSITION: absolute; TOP: 591 px" 156 runat="server" Width="328 px" Height="29 px" 157 Fore. Color="Lime" Font-Size="Medium"> 158 Which operating system are you using? 159 </asp: Label> 160 161 <asp: Button id= "Register. Button" style="Z-INDEX: 124; Radio. Button. List Provides a series of radio 162 LEFT: 69 px; POSITION: absolute; TOP: 760 px" buttons from which the user can select only one 163 runat="server" Width="120 px" Height="33 px" 164 Text="Register"> 165 </asp: Button> 166 167 <asp: Radio. Button. List id= "Operating. Radio. Button. List " 168 style="Z-INDEX: 125; LEFT: 65 px; POSITION: 169 absolute; TOP: 624 px" runat="server" 170 Height="122 px" Width="155 px"> Outline Controls. aspx 2002 Prentice Hall. All rights reserved. 22
167 <asp: Radio. Button. List id= "Operating. Radio. Button. List " 168 style="Z-INDEX: 125; LEFT: 65 px; POSITION: 169 absolute; TOP: 624 px" runat="server" 170 Height="122 px" Width="155 px"> 171 172 <asp: List. Item Value="Windows NT"> 173 Windows NT 174 </asp: List. Item> Definition of each radio button 175 <asp: List. Item Value="Windows 2000"> 176 Windows 2000 177 </asp: List. Item> 178 <asp: List. Item Value="Windows XP"> 179 Windows XP 180 </asp: List. Item> 181 <asp: List. Item Value="Linux"> 182 Linux 183 </asp: List. Item> 184 <asp: List. Item Value="Other"> 185 Other 186 </asp: List. Item> 187 </asp: Radio. Button. List> 188 189 </form> 190 </body> 191 </HTML> Outline Controls. aspx 2002 Prentice Hall. All rights reserved. 23
24 20. 5 Web Controls 2002 Prentice Hall. All rights reserved.
1 <%-- Fig 20. 18: Country. Rotator. aspx --%> 2 <%-- A Web Form that demonstrates class Ad. Rotator. --%> 3 4 <%@ Page Language="vb" Auto. Event. Wireup="false" 5 Codebehind="Country. Rotator. aspx. vb" <HTML> and <HEAD> start tags 6 Inherits="Ad. Rotator"%> 7 8 <!DOCTYPE HTML PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN" > Specifies the information needed by 9 <HTML> Common Language Runtime (CLR) 10 <HEAD> 11 <title>Web. Form 1</title> 12 <meta content="Microsoft Visual Studio. NET 7. 0" 13 name="GENERATOR"> 14 <meta content="Visual Basic 7. 0" name="CODE_LANGUAGE"> 15 <meta content="Java. Script" name="vs_default. Client. Script"> Identifies meta element and 16 <meta name="vs_target. Schema" content saves meta element’s data 17 content="http: //schemas. microsoft. com/ intellisense/ie 5"> 18 </HEAD> 19 <body background= 20 "images/background. png" 21 MS_POSITIONING="Grid. Layout"> Label Web control 22 <form id="Form 1" method="post" runat="server"> 23 24 <asp: label id="display. Label" style="Z-INDEX: 101; 25 LEFT: 36 px; POSITION: absolute; TOP: 22 px" 26 runat="server" Font-Size="Medium" Height="28 px" 27 Width="268 px">Ad. Rotator Example 28 </asp: label> Outline 25 Country. Rotator. aspx 2002 Prentice Hall. All rights reserved.
30 <asp: adrotator id="country. Rotator" style="Z-INDEX: 102; 31 LEFT: 36 px; POSITION: absolute; TOP: 47 px" 32 runat="server" Height="72 px" Width="108 px" 33 Advertisement. File ="Ad. Rotator. Information. xml"> 34 </asp: adrotator> 35 36 </form> 37 </body> 38 </HTML> Fig. 20 Outline 26 Country. Rotator. aspx 2002 Prentice Hall. All rights reserved.
1 ' Fig. 20. 19: Country. aspx. vb 2 ' The code-behind file for a page that 3 ' demonstrates the Ad. Rotator class. 4 5 Public Class Ad. Rotator 6 Inherits System. Web. UI. Page Class Ad. Rotator inherits 7 System. Web. UI. Page 8 Protected With. Events display. Label As _ 9 System. Web. UI. Web. Controls. Label 10 11 Protected With. Events country. Rotator As _ 12 System. Web. UI. Web. Controls. Ad. Rotator 13 Declares reference to one Label 14 ' This call is required by the Web Form Designer. 15 Private Sub Initialize. Component() 16 End Sub 17 18 Private Sub Page_Init(By. Val sender As System. Object, _ 19 By. Val e As System. Event. Args) Handles My. Base. Init 20 21 ' CODEGEN: This method call is required by the Web Form Designer 22 ' Do not modify it using the code editor. Defines method Page_Init 23 Initialize. Component () 24 End Sub ' Page_Init 25 26 Private Sub Page_Load(By. Val sender As System. Object, _ 27 By. Val e As System. Event. Args) Handles My. Base. Load 28 'Put user code to initialize the page here 29 End Sub 30 End Class ' Ad. Rotator Outline Country. aspx 2002 Prentice Hall. All rights reserved. 27
28 20. 5 Web Controls 2002 Prentice Hall. All rights reserved.
1 <? xml version="1. 0" encoding="utf-8"? > 2 3 <!-- Fig. 20: Ad. Rotator. Information. xml --> 4 <!-- XML file containing advertisement information. --> 5 Ad elements each provide information 6 <Advertisements> about the advertisement 7 <Ad> 8 <Image. Url>images/us. png</Image. Url> 9 <Navigate. Url> 10 http: //www. odci. gov/cia/publications/factbook/geos/us. html 11 </Navigate. Url> 12 <Alternate. Text>United States Information </Alternate. Text> 13 <Impressions>1</Impressions> 14 </Ad> 15 Alternate. Text is a tool tip which displays the 16 <Ad> message when mouse points over image 17 <Image. Url>images/france. png</Image. Url> 18 <Navigate. Url> 19 http: //www. odci. gov/cia/publications/factbook/geos/fr. html 20 </Navigate. Url> 21 <Alternate. Text>France Information</Alternate. Text> 22 <Impressions>1</Impressions> 23 </Ad> 24 25 <Ad> The higher the Impression value the more 26 <Image. Url>images/germany. png</Image. Url> often the advertisement will appear 27 <Navigate. Url> 28 http: //www. odci. gov/cia/publications/factbook/geos/gm. html 29 </Navigate. Url> 30 <Alternate. Text>Germany Information</Alternate. Text> 31 <Impressions>1</Impressions> 32 </Ad> Outline 29 Ad. Rotator. Informatio n. xml 2002 Prentice Hall. All rights reserved.
34 <Ad> 35 <Image. Url>images/italy. png</Image. Url> 36 <Navigate. Url> 37 http: //www. odci. gov/cia/publications/factbook/geos/it. html 38 </Navigate. Url> Image. Url specifies the location 39 <Alternate. Text>Italy Information</Alternate. Text> 40 <Impressions>1</Impressions> of the advertisement image 41 </Ad> 42 43 <Ad> 44 <Image. Url>images/spain. png</Image. Url> 45 <Navigate. Url> 46 http: //www. odci. gov/cia/publications/factbook/geos/sp. html 47 </Navigate. Url> 48 <Alternate. Text>Spain Information</Alternate. Text> 49 <Impressions>1</Impressions> 50 </Ad> Navigate. Url indicates URL for the 51 52 <Ad> web page that loads when a user 53 <Image. Url>images/latvia. png</Image. Url> clicks the advertisement 54 <Navigate. Url> 55 http: //www. odci. gov/cia/publications/factbook/geos/lg. html 56 </Navigate. Url> 57 <Alternate. Text>Latvia Information</Alternate. Text> 58 <Impressions>1</Impressions> 59 </Ad> 60 61 <Ad> 62 <Image. Url>images/peru. png</Image. Url> 63 <Navigate. Url> 64 http: //www. odci. gov/cia/publications/factbook/geos/pe. html 65 </Navigate. Url> 66 <Alternate. Text>Peru Information</Alternate. Text> 67 <Impressions>1</Impressions> 68 </Ad> Outline 30 Ad. Rotator. Informatio n. xml 2002 Prentice Hall. All rights reserved.
70 <Ad> 71 <Image. Url>images/senegal. png</Image. Url> 72 <Navigate. Url> 73 http: //www. odci. gov/cia/publications/factbook/geos/sg. html 74 </Navigate. Url> 75 <Alternate. Text>Senegal Information</Alternate. Text> 76 <Impressions>1</Impressions> 77 </Ad> 78 79 <Ad> 80 <Image. Url>images/sweden. png</Image. Url> 81 <Navigate. Url> 82 http: //www. odci. gov/cia/publications/factbook/geos/sw. html Image. Url specifies the location 83 </Navigate. Url> 84 <Alternate. Text>Sweden Information</Alternate. Text> of the advertisement image 85 <Impressions>1</Impressions> 86 </Ad> 87 88 <Ad> 89 <Image. Url>images/thailand. png</Image. Url> 90 <Navigate. Url> 91 http: //www. odci. gov/cia/publications/factbook/geos/th. html 92 </Navigate. Url> 93 <Alternate. Text>Thailand Information</Alternate. Text> 94 <Impressions>1</Impressions> 95 </Ad> Navigate. Url indicates URL for the 96 web page that loads when a user 97 <Ad> 98 <Image. Url>images/unitedstates. png</Image. Url> clicks the advertisement 99 <Navigate. Url> 100 http: //www. odci. gov/cia/publications/factbook/geos/us. html 101 </Navigate. Url> 102 <Alternate. Text>United States Information </Alternate. Text> 103 <Impressions>1</Impressions> 104 </Ad> 105 </Advertisements> Outline 31 Ad. Rotator. Informatio n. xml 2002 Prentice Hall. All rights reserved.
1 <%-- Fig. 20. 21: Generator. aspx --%> 2 <%-- A Web Form demonstrating the use of validators. --%> 3 4 <%@ Page Language="vb" Auto. Event. Wireup="false" 5 Codebehind="Generator. aspx. vb" 6 Inherits="Word. Generator" <HTML> and <HEAD> start tags 7 enable. View. State=“False" Enable. Session. State=“False" %> 8 9 <!DOCTYPE HTML PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN" > 10 <HTML> 11 <HEAD> 12 <title>Web. Form 1</title> 13 <meta name="GENERATOR" 14 content="Microsoft Visual Studio. NET 7. 0" > 15 <meta name="CODE_LANGUAGE" content="Visual Basic 7. 0" > 16 <meta name="vs_default. Client. Script" content="Java. Script"> 17 <meta name="vs_target. Schema" 18 content="http: //schemas. microsoft. com/ intellisense/ie 5"> 19 </HEAD> 20 <body MS_POSITIONING= "Grid. Layout"> 21 <form id="Form 1" method="post" runat="server"> 22 23 <asp: Label id="prompt. Label" style="Z-INDEX: 101; Displays the words generated from the 24 LEFT: 32 px; POSITION: absolute; TOP: 17 px" 25 runat="server"> phone number if validator is successful 26 Please enter a phone number in the form 555 -4567. 27 </asp: Label> 28 29 <asp: Text. Box id= "output. Text. Box" style="Z-INDEX: 106; 30 LEFT: 40 px; POSITION: absolute; TOP: 118 px" 31 runat="server" Width="451 px" Height="342 px" 32 Text. Mode="Multi. Line" Visible="False"> 33 </asp: Text. Box> Outline Generator. aspx 2002 Prentice Hall. All rights reserved. 32
35 <asp: Regular. Expression. Validator id="phone. Number. Validator" The regular expression is assigned 36 style="Z-INDEX: 105; LEFT: 204 px; POSITION: absolute; 37 TOP: 44 px" runat="server" Error. Message= to Validation. Expression 38 "The phone number must be in the form 555 -4567. " 39 Control. To. Validate="phone. Text. Box" 40 Validation. Expression= "^d{3}-d{4}$"> 41 </asp: Regular. Expression. Validator. Error. Message > is displayed on 42 the web form if validation fails 43 <asp: Required. Field. Validator id="phone. Input. Validator" 44 style="Z-INDEX: 104; LEFT: 207 px; POSITION: 45 absolute; TOP: 81 px" runat="server" 46 Error. Message= 47 "Please enter a phone number. " Confirms that phone. Text. Box’s 48 Control. To. Validate="phone. Text. Box"> 49 </asp: Required. Field. Validator> content is not empty 50 51 <asp: Button id= "submit. Button" style="Z-INDEX: 103; 52 LEFT: 38 px; POSITION: absolute; TOP: 77 px" 53 runat="server" Text="Submit"> 54 </asp: Button> 55 56 <asp: Text. Box id= "phone. Text. Box" style="Z-INDEX: 102; 57 LEFT: 34 px; POSITION: absolute; TOP: 42 px" 58 runat="server"> 59 </asp: Text. Box> 60 61 </form> 62 </body> 63 </HTML> Outline Generator. aspx 2002 Prentice Hall. All rights reserved. 33
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. Web. Controls 6 7 Public Class Generator 8 Inherits System. Web. UI. Page 9 10 Protected With. Events phone. Input. Validator As _ 11 Required. Field. Validator 12 13 Protected With. Events phone. Number. Validator As _ 14 Regular. Expression. Validator 15 16 Protected With. Events prompt. Label As Label 17 Protected With. Events output. Text. Box As Text. Box 18 Protected With. Events submit. Button As Button 19 Protected With. Events phone. Text. Box As Text. Box 20 21 ' Web Form Designer generated code Determines whether the page is 22 23 Private Sub Page_Load(By. Val sender As System. Object, _ being loaded due to a postback 24 By. Val e As System. Event. Args) Handles My. Base. Load 25 Text property is set to empty string 26 ' if not first time page loads 27 If Is. Post. Back Then 28 Dim number As String Retrieves the number and 29 removes the hyphen 30 output. Text. Box. Text() = "" 31 32 ' retrieve number and remove "-" 33 number = Request. Form( "phone. Text. Box") 34 number = number. Remove( 3, 1) Outline Generator. aspx. vb 2002 Prentice Hall. All rights reserved. 34
36 ' generate words for first 3 digits 37 output. Text. Box. Text &= "Here are the words for the" & _ 38 "first three digits" & vb. Cr. Lf 39 40 Compute. Words(number. Substring(0, 3), "") 41 output. Text. Box. Text &= vb. Cr. Lf 42 43 ' generate words for last 4 digits 44 output. Text. Box. Text &= "Here are the words for the" & _ Initializes the output. Text. Box’s visible 45 "last 4 digits" & vb. Cr. Lf property to True 46 47 Compute. Words(number. Substring(3), "") 48 49 output. Text. Box. Visible = True 50 End If 51 52 End Sub ' Page_Load 53 54 Private Sub Compute. Words(By. Val number As String, _ 55 By. Val temporary. Word As String) 56 57 Dim current As Integer Recursive method that generates list of 58 words from the String containing digits 59 ' if number is empty, print word 60 If number = "" Then 61 output. Text. Box. Text &= temporary. Word & vb. Cr. Lf 62 Return 63 End If 64 65 ' retrieve first number and convert to Integer 66 current = Convert. To. Int 32(number. Substring( 0, 1)) 67 68 ' delete first number 69 number = number. Remove( 0, 1) Outline Generator. aspx. vb 2002 Prentice Hall. All rights reserved. 35
71 ' determine number, call Compute. Word recursively 72 Select Case current 73 74 ' 0 can be q or z 75 Case 0 76 Compute. Words (number, temporary. Word & "q") 77 Compute. Words (number, temporary. Word & "z") 78 79 ' 1 has no letter associated with it 80 Case 1 81 Compute. Words (number, temporary. Word & "") 82 83 ' 2 can be a, b or c 84 Case 2 85 Compute. Words (number, temporary. Word & "a") 86 Compute. Words (number, temporary. Word & "b") 87 Compute. Words (number, temporary. Word & "c") 88 89 ' 3 can be d, e or f 90 Case 3 91 Compute. Words (number, temporary. Word & "d") 92 Compute. Words (number, temporary. Word & "e") 93 Compute. Words (number, temporary. Word & "f") 94 95 ' 4 can be g, h or i 96 Case 4 97 Compute. Words (number, temporary. Word & "g") 98 Compute. Words (number, temporary. Word & "h") 99 Compute. Words (number, temporary. Word & "i") Outline Generator. aspx. vb 2002 Prentice Hall. All rights reserved. 36
101 ' 5 can be j, k or l 102 Case 5 103 Compute. Words (number, temporary. Word & "j") 104 Compute. Words (number, temporary. Word & "k") 105 Compute. Words (number, temporary. Word & "l") 106 107 ' 6 can be m, n or o 108 Case 6 109 Compute. Words (number, temporary. Word & "m") 110 Compute. Words (number, temporary. Word & "n") 111 Compute. Words (number, temporary. Word & "o") 112 113 ' 7 can be p, r or s 114 Case 7 115 Compute. Words (number, temporary. Word & "p") 116 Compute. Words (number, temporary. Word & "r") 117 Compute. Words (number, temporary. Word & "s") 118 119 ' 8 can be t, u or v 120 Case 8 121 Compute. Words (number, temporary. Word & "t") 122 Compute. Words (number, temporary. Word & "u") 123 Compute. Words (number, temporary. Word & "v") 124 125 ' 9 can be w, x or y 126 Case 9 127 Compute. Words (number, temporary. Word & "w") 128 Compute. Words (number, temporary. Word & "x") 129 Compute. Words (number, temporary. Word & "y") 130 End Select 131 132 End Sub ' Compute. Words 133 End Class ' Generator Outline Generator. aspx. vb 2002 Prentice Hall. All rights reserved. 37
38 20. 5 Web Controls 2002 Prentice Hall. All rights reserved.
39 20. 5 Web Controls 2002 Prentice Hall. All rights reserved.
1 <!-- Fig. 20. 23: Generator. html --> 2 <!-- The HTML page that is sent to the client browser. --> 3 4 <!DOCTYPE HTML PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN" > 5 <HTML> 6 <HEAD> 7 <title>Web. Form 1</title> 8 <meta name="GENERATOR" 9 content="Microsoft Visual Studio 7. 0" > 10 <meta name="CODE_LANGUAGE" content="Visual Basic 7. 0" > 11 <meta name="vs_default. Client. Script" 12 content="Java. Script"> 13 <meta name="vs_target. Schema" 14 content="http: //schemas. microsoft. com/ intellisense/ie 5"> 15 </HEAD> 16 17 <body MS_POSITIONING= "Grid. Layout"> 18 19 <form name="Form 1" method="post" 20 action="Generator. aspx" language="javascript" ECMAScript provides implementation 21 onsubmit="Validator. On. Submit(); " id="FORM 1"> 22 <input type="hidden" name="__VIEWSTATE" for validation controls 23 value="d. Dwx. Mjgy. Mz. M 3 ozs+" /> 24 25 <script language= "javascript" 26 src= 27 "/aspnet_client/system_web/1_0_3215_11/ Web. UIValidation. js"> 28 </script> Outline Generator. html 2002 Prentice Hall. All rights reserved. 40
30 <span id="phone. Number. Validator" 31 controltovalidate ="phone. Text. Box" 32 errormessage = 33 "The phone number must be in the form 555 -4567. " 34 evaluationfunction = 35 "Regular. Expression. Validator. Evaluate. Is. Valid " 36 validationexpression ="^d{3}-d{4}$" 37 style="color: Red; Z-INDEX: 106; LEFT: 217 px; 38 POSITION: absolute; TOP: 73 px; visibility: hidden; " > 39 The phone number must be in the form 555 -4567. 40 </span> 41 42 <input name="phone. Text. Box" type="text" 43 id="phone. Text. Box" 44 style="Z-INDEX: 102; LEFT: 16 px; 45 POSITION: absolute; TOP: 52 px" /> 46 47 <input type="submit" name="submit. Button" 48 value="Submit" 49 onclick= "if ( " + 50 "typeof(Page_Client. Validate) == 'function') " + 51 "Page_Client. Validate(); " language="javascript" 52 id="submit. Button" style="Z-INDEX: 103; 53 LEFT: 16 px; 54 POSITION: absolute; 55 TOP: 86 px" /> Outline Generator. html 2002 Prentice Hall. All rights reserved. 41
57 <span id="phone. Input. Validator" 58 controltovalidate ="phone. Text. Box" 59 errormessage ="Please enter a phone number. " 60 evaluationfunction = 61 "Required. Field. Validator. Evaluate. Is. Valid " 62 initialvalue ="" style="color: Red; Z-INDEX: 105; 63 LEFT: 217 px; POSITION: absolute; TOP: 47 px; 64 visibility: hidden; ">Please enter a phone number. 65 </span> 66 67 <span id="prompt. Label" style="Z-INDEX: 101; 68 LEFT: 16 px; POSITION: absolute; TOP: 23 px" > 69 Please enter a phone number in the form 555 -4567: 70 </span> 71 72 <script language= "javascript"> 73 <!-74 var Page_Validators = new Array( 75 document. all[ "phone. Number. Validator"], 76 document. all[ "phone. Input. Validator"] ); ECMAScript provides implementation 77 // --> for validation controls 78 </script> 79 80 <script language= "javascript"> 81 <!-82 var Page_Validation. Active = false; 83 84 if ( 85 typeof(client. Information) != "undefined" && 86 client. Information. app. Name. index. Of("Explorer") 87 != -1 ) { Outline Generator. html 2002 Prentice Hall. All rights reserved. 42
89 if ( typeof(Page_Validation. Ver) == "undefined" ) 90 alert( 91 "Unable to find script library " + 92 "'/aspnet_client/system_web/'"+ 93 "'1_0_3215_11/Web. UIValidation. js'. " + 94 "Try placing this file manually, or " + 95 "reinstall by running 'aspnet_regiis -c'. "); 96 else if ( Page_Validation. Ver != "125" ) 97 alert( 98 "This page uses an incorrect version " + 99 "of Web. UIValidation. js. The page " + 100 "expects version 125. " + 101 "The script library is " + 102 Page_ Validation. Ver + ". "); 103 else 104 Validator. On. Load (); 105 } 106 107 function Validator. On. Submit() { 108 if (Page_Validation. Active) { 109 Validator. Common. On. Submit (); 110 } 111 } 112 // --> 113 </script> 114 </form> 115 </body> 116 </HTML> Outline Generator. html 2002 Prentice Hall. All rights reserved. 43
44 20. 6 Session Tracking • Cookies – Text file stored by a Web site on a individual’s 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 • Http. Session. State – Http. Session. State objects can store any type of objects (not just Strings) as attribute values 2002 Prentice Hall. All rights reserved.
1 <%-- Fig 20. 24: Options. Page. aspx --%> 2 <%-- allows clients to select a programming language --%> 3 <%-- to get recommendations --%> 4 5 <%@ Page Language="vb" Auto. Event. Wireup="false" 6 Codebehind="Options. Page. aspx. vb" 7 Inherits="Cookies. Cookie"%> 8 9 <!DOCTYPE HTML PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN" > 10 <HTML> 11 <HEAD> 12 <title>Cookies</title> 13 <meta content="Microsoft Visual Studio. NET 7. 0" 14 name="GENERATOR"> 15 <meta content="Visual Basic 7. 0" name="CODE_LANGUAGE"> 16 <meta content="Java. Script" name="vs_default. Client. Script"> 17 <meta name="vs_target. Schema" 18 content="http: //schemas. microsoft. com/ intellisense/ie 5"> 19 </HEAD> 20 <body MS_POSITIONING= "Grid. Layout"> 21 <form id="Form 1" method="post" runat="server"> Label Web control 22 23 <asp: label id="prompt. Label" style="Z-INDEX: 101; 24 LEFT: 42 px; POSITION: absolute; TOP: 22 px" 25 runat="server" Font-Bold="True" Font-Size="Large"> 26 Select a programming language. 27 </asp: label> 28 29 <asp: radiobuttonlist id="Language. List" 30 style="Z-INDEX: 111; LEFT: 42 px; POSITION: 31 absolute; TOP: 52 px" runat="server"> 32 33 <asp: List. Item Value="Visual Basic. NET" 34 Selected= "True">Visual Basic. NET</asp: List. Item> Outline Options. Page. aspx 2002 Prentice Hall. All rights reserved. 45
36 <asp: List. Item Value="C#">C#</asp: List. Item> 37 <asp: List. Item Value="C">C</asp: List. Item> 38 <asp: List. Item Value="C++">C++</asp: List. Item> 39 <asp: List. Item Value="Python">Python</asp: List. Item> Defines five radio buttons 40 </asp: radiobuttonlist> 41 42 <asp: hyperlink id= "recommendations. Link" 43 style="Z-INDEX: 110; LEFT: 42 px; POSITION: 44 absolute; TOP: 90 px" runat="server" 45 Visible="False" Navigate. Url= Recommendation hyperlink displayed 46 "Recommendation. Page. aspx"> 47 Click here to get book recommendations 48 </asp: hyperlink> 49 50 <asp: hyperlink id= "language. Link" style="Z-INDEX: 51 109; LEFT: 42 px; POSITION: absolute; 52 TOP: 55 px" runat="server" Visible="False" Language hyperlink displayed 53 Navigate. Url="Options. Page. aspx"> 54 Click here to choose another language 55 </asp: hyperlink> 56 Forwards the user to Options. Page. aspx 57 <asp: label id="welcome. Label" style="Z-INDEX: 108; to display list of languages 58 LEFT: 42 px; POSITION: absolute; TOP: 23 px" 59 runat="server" Visible="False" Font-Bold="True" 60 Font-Size="Large">Welcome to cookies! You selected 61 </asp: label> 62 63 <asp: button id= "submit. Button" style="Z-INDEX: 107; 64 LEFT: 42 px; POSITION: absolute; TOP: 196 px" 65 runat="server" Text="Submit"> 66 </asp: button> 67 68 </form> 69 </body> 70 </HTML> Outline Options. Page. aspx 2002 Prentice Hall. All rights reserved. 46
1 ' Fig. 20. 25: Options. Page. aspx. vb 2 ' Page that allows the user to choose a different language 3 4 Imports System. Web. UI. Web. Controls 5 6 Public Class Cookie Imports System. Web. UI. Web. Controls 7 Inherits System. Web. UI. Page 8 Protected With. Events language. Link As Hyper. Link 9 Protected With. Events recommendations. Link As Hyper. Link 10 Protected With. Events prompt. Label As Label 11 Protected With. Events Language. List As Radio. Button. List Defines books as a Hashtable 12 Protected With. Events welcome. Label As Label 13 Protected With. Events submit. Button As Button 14 Private books = New Hashtable() 15 16 ' Visual Studio. NET generated code 17 18 Private Sub Page_Init(By. Val sender As System. Object, _ 19 By. Val e As System. Event. Args) Handles My. Base. Init 20 21 Initialize. Component () ISBN numbers for the recommended books 22 23 ' add values to Hastable 24 books. Add("Visual Basic. NET", "0 -13 -456955 -5") 25 books. Add("C#", "0 -13 -062221 -4") 26 books. Add("C", "0 -13 -089572 -5") 27 books. Add("C++", "0 -13 -089571 -7") 28 books. Add("Python", "0 -13 -092361 -3") 29 End Sub ' Page_Init Outline Options. Page. aspx 2002 Prentice Hall. All rights reserved. 47
31 Private Sub Page_Load(By. Val sender As System. Object, _ 32 By. Val e As System. Event. Args) Handles My. Base. Load 33 34 If Is. Post. Back Then 35 36 ' if postback is True, user has submitted information 37 ' display welcome message and appropriate hyperlinks 38 welcome. Label. Visible = True 39 language. Link. Visible = True 40 recommendations. Link. Visible = True 41 42 ' hide option information Determines if the user selected a language 43 submit. Button. Visible = False 44 prompt. Label. Visible = False 45 Language. List. Visible = False Displays language in welcomelabel 46 47 If (Language. List. Selected. Item Is Nothing) = False Then 48 welcome. Label. Text &= " " & _ 49 Language. List. Selected. Item. Text. To. String & ". " 50 Else 51 welcome. Label. Text &= "no language. " 52 End If 53 End If By clicking submit creates a cookie 54 End Sub ' Page_Load 55 56 Private Sub submit. Button_Click(By. Val sender As System. Object, _ 57 By. Val e As System. Event. Args) Handles submit. Button. Click 58 59 Dim language, ISBN As String 60 Dim cookie As Http. Cookie Outline Options. Page. aspx 2002 Prentice Hall. All rights reserved. 48
62 ' if choice was made by user 63 If (Language. List. Selected. Item Is Nothing) = False Then 64 language = Language. List. Selected. Item. To. String() 65 ISBN = books(language). To. String() Adds the cookie to the Cookies collection 66 67 ' create cookie, name/value pair is 68 ' language chosen and ISBN number from Hashtable 69 cookie = New Http. Cookie(language, ISBN) 70 71 ' add cookie to response, 72 ' thus placing it on user's machine Stores the language and its corresponding 73 Response. Cookies. Add(cookie) ISBN number in cookie 74 End If 75 76 End Sub ' submit. Button_Click 77 End Class ' Cookie Outline Options. Page. aspx 2002 Prentice Hall. All rights reserved. 49
50 20. 6 Session Tracking 2002 Prentice Hall. All rights reserved.
51 20. 6 Session Tracking 2002 Prentice Hall. All rights reserved.
1 <%-- Fig 20. 26: Recommendation. Page. aspx --%> 2 <%-- displays book recommendations using cookies --%> 3 4 <%@ Page Language="vb" Auto. Event. Wireup="false" 5 Codebehind="Recommendation. Page. aspx. vb" 6 Inherits="Cookies. Recommendations" %> 7 8 <!DOCTYPE HTML PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN" > 9 <HTML> 10 <HEAD> 11 <title>Book recommendations</title> 12 <meta content="Microsoft Visual Studio. NET 7. 0" 13 name="GENERATOR"> 14 <meta content="Visual Basic 7. 0" name="CODE_LANGUAGE"> 15 <meta content="Java. Script" name="vs_default. Client. Script"> 16 <meta name="vs_target. Schema" 17 content="http: //schemas. microsoft. com/ intellisense/ie 5"> 18 </HEAD> Label displays text recommendations 19 <body MS_POSITIONING= "Grid. Layout"> 20 <form id="Form 1" method="post" runat="server"> 21 22 <asp: label id="recommendations. Label" 23 style="Z-INDEX: 101; LEFT: 55 px; POSITION: Displays the recommendations created 24 absolute; TOP: 38 px" runat="server" by the code-behind file 25 Font-Size="X-Large">Recommendations 26 </asp: label> 27 28 <asp: listbox id="books. List. Box" style="Z-INDEX: 102; 29 LEFT: 50 px; POSITION: absolute; TOP: 80 px" 30 runat="server" Width="442 px" Height="125 px"> 31 </asp: listbox> 32 33 </form> 34 </body> 35 </HTML> Outline 52 Recomendation. Page. aspx 2002 Prentice Hall. All rights reserved.
1 ' Fig. 20. 27: Recommendations. Page. aspx. vb 2 ' Reading cookie data from the client 3 4 Imports System. Web. UI. Web. Controls 5 6 Public Class Recommendations 7 Inherits Page 8 Protected With. Events recommendations. Label As Label 9 Protected With. Events books. List. Box As List. Box 10 11 ' Visual Studio. NET generated code 12 13 Private Sub Page_Init(By. Val sender As System. Object, _ 14 By. Val e As System. Event. Args) Handles My. Base. Init 15 16 Initialize. Component () Method Page_Init retrieves the 17 18 ' retrieve client's cookies from the client 19 Dim cookies As Http. Cookie. Collection 20 Dim i As Integer 21 22 cookies = Request. Cookies Determines whether at least 23 two cookies exist 24 ' if there are cookies besides the ID cookie, 25 ' list appropriate books and ISBN numbers 26 If (((cookies Is Nothing) = False) _ 27 And. Also cookies. Count <> 1) Then 28 29 For i = 1 To cookies. Count - 1 30 books. List. Box. Items. Add(cookies(i). Name & _ 31 " How to Program. ISBN#: " & _ 32 cookies(i). Value) 33 Next Outline Recomedations. Page. aspx. vb 2002 Prentice Hall. All rights reserved. 53
35 ' if no cookies besides ID, no options were 36 ' chosen. no recommendations made 37 Else 38 recommendations. Label. Text = "No Recommendations" 39 books. List. Box. Items. Clear() 40 books. List. Box. Visible = False 41 End If Executes if the user did not select 42 any recommendations 43 End Sub ' Page_Init 44 45 Private Sub Page_Load(By. Val sender As System. Object, _ 46 By. Val e As System. Event. Args) Handles My. Base. Load 47 48 'Put user code to initialize the page here 49 End Sub ' Page_Load 50 End Class ' Recommendations Outline Recomedations. Page. aspx. vb 2002 Prentice Hall. All rights reserved. 54
55 20. 6 Session Tracking 2002 Prentice Hall. All rights reserved.
1 <%-- Fig. 20. 29: Options. Page. aspx --%> 2 <%-- displays a list of book options --%> 3 4 <%@ Page Language="vb" Auto. Event. Wireup="false" 5 Codebehind="Options. Page. aspx. vb" 6 Inherits="Sessions. Options 2"%> 7 8 <!DOCTYPE HTML PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN" > 9 <HTML> 10 <HEAD> 11 <title>Session Tracking</title> 12 <meta name="GENERATOR" 13 content="Microsoft Visual Studio. NET 7. 0" > 14 <meta name="CODE_LANGUAGE" content="Visual Basic 7. 0"> 15 <meta name="vs_default. Client. Script" content="Java. Script"> 16 <meta name="vs_target. Schema" 17 content="http: //schemas. microsoft. com/ intellisense/ie 5"> 18 </HEAD> Label Web control 19 <body MS_POSITIONING= "Grid. Layout"> 20 <form id="Form 1" method="post" runat="server"> 21 22 <asp: label id="prompt. Label" style="Z-INDEX: 106; 23 LEFT: 43 px; POSITION: absolute; TOP: 32 px" 24 runat="server" Font-Bold="True" Font-Size="Large"> 25 Select a programming language. 26 </asp: label> 27 28 <asp: Label id="time. Out. Label" style="Z-INDEX: 108; 29 LEFT: 42 px; POSITION: absolute; TOP: 100 px" 30 runat="server"> 31 </asp: Label> Outline Options. Page. aspx 2002 Prentice Hall. All rights reserved. 56
33 <asp: Label id="id. Label" style="Z-INDEX: 107; 34 LEFT: 42 px; POSITION: absolute; TOP: 66 px" 35 runat="server"> 36 </asp: Label> 37 38 <asp: radiobuttonlist id="Language. List" style="Z-INDEX: 105; Defines five radio buttons 39 LEFT: 43 px; POSITION: absolute; TOP: 69 px" runat="server"> 40 41 <asp: List. Item Value="Visual Basic. NET" 42 Selected= "True">Visual Basic. NET</asp: List. Item> 43 44 <asp: List. Item Value="C#">C#</asp: List. Item> 45 <asp: List. Item Value="C">C</asp: List. Item> 46 <asp: List. Item Value="C++">C++</asp: List. Item> 47 <asp: List. Item Value="Python">Python</asp: List. Item> 48 </asp: radiobuttonlist> 49 50 <asp: hyperlink id= "recommendations. Link" style="Z-INDEX: 104; 51 LEFT: 42 px; POSITION: absolute; TOP: 172 px" 52 runat="server" Navigate. Url="Recommendation. Page. aspx" Recommendation hyperlink displayed 53 Visible="False"> 54 Click here to get book recommendations Language hyperlink displayed 55 </asp: hyperlink> 56 57 <asp: hyperlink id= "language. Link" style="Z-INDEX: 103; 58 LEFT: 42 px; POSITION: absolute; TOP: 137 px" 59 runat="server" Navigate. Url="Options. Page. aspx" 60 Visible="False"> 61 Click here to choose another language 62 </asp: hyperlink> Outline Options. Page. aspx 2002 Prentice Hall. All rights reserved. 57
64 <asp: label id="welcome. Label" style="Z-INDEX: 102; 65 LEFT: 42 px; POSITION: absolute; TOP: 32 px" 66 runat="server" Visible="False" Font-Bold="True" 67 Font-Size="Large">Welcome to sessions! You selected 68 </asp: label> 69 70 <asp: button id= "submit. Button" style="Z-INDEX: 101; 71 LEFT: 42 px; POSITION: absolute; TOP: 207 px" 72 runat="server" Text="Submit"> 73 </asp: button> 74 75 </form> 76 </body> 77 </HTML> Outline Options. Page. aspx 2002 Prentice Hall. All rights reserved. 58
1 ' Fig. 20. 30: Options. Pge 2. aspx. vb 2 ' A listing of programming languages, 3 ' cookie is created based on choice made. 4 5 Imports System. Web. UI. Web. Controls 6 7 Public Class Options 2 8 Inherits System. Web. UI. Page 9 Protected With. Events language. Link As Hyper. Link 10 Protected With. Events recommendations. Link As Hyper. Link 11 Protected With. Events Language. List As Radio. Button. List 12 Protected With. Events id. Label As Label 13 Protected With. Events time. Out. Label As Label 14 Protected With. Events prompt. Label As Label 15 Protected With. Events welcome. Label As Label 16 Protected With. Events submit. Button As Button 17 Private books = New Hashtable() 18 19 ' Visual Studio. NET generated code 20 21 Private Sub Page_Init(By. Val sender As System. Object, _ 22 By. Val e As System. Event. Args) Handles My. Base. Init 23 24 Initialize. Component () 25 26 ' add values to Hastable 27 books. Add("Visual Basic. NET", "0 -13 -456955 -5") 28 books. Add("C#", "0 -13 -062221 -4") 29 books. Add("C", "0 -13 -089572 -5") 30 books. Add("C++", "0 -13 -089571 -7") 31 books. Add("Python", "0 -13 -092361 -3") 32 End Sub ' Page_Init Outline 59 Options. Pge 2. aspx. vb 2002 Prentice Hall. All rights reserved.
Outline 60 34 Private Sub Page_Load(By. Val sender As System. Object, _ 35 By. Val e As System. Event. Args) Handles My. Base. Load 36 37 If Is. Post. Back Then 38 39 ' if postback is True, user has submitted information 40 ' display welcome message and appropriate hyperlinks Retrieves information about current client’s 41 welcome. Label. Visible = True session from the Session object’s properties 42 language. Link. Visible = True 43 recommendations. Link. Visible = True 44 45 ' hide option information 46 submit. Button. Visible = False 47 prompt. Label. Visible = False 48 Language. List. Visible = False 49 50 If (Language. List. Selected. Item Is Nothing) = False Then 51 welcome. Label. Text &= " " & _ Options. Pge 2. aspx. vb 52 Language. List. Selected. Item. Text. To. String & ". " 53 Else 54 welcome. Label. Text &= "no language. " Property Session. ID contains the sessions unique ID 55 End If 56 57 id. Label. Text = "Your unique session ID is: " & _ 58 Session. ID 59 60 time. Out. Label. Text = "Timeout: " & Session. Timeout & _ 61 " minutes. " 62 63 End If Property Timeout specifies maximum time that 64 Http. Session. State object can be inactive before discarded 65 End Sub ' Page_Load 2002 Prentice Hall. All rights reserved.
67 Private Sub submit. Button_Click(By. Val sender As System. Object, _ 68 By. Val e As System. Event. Args) Handles submit. Button. Click 69 70 Dim language, ISBN As String 71 72 ' if choice was made by user Responds by adding a key-value pair to our Session object 73 If (Language. List. Selected. Item Is Nothing) = False Then 74 language = Language. List. Selected. Item. To. String() 75 ISBN = books(language). To. String() 76 77 ' add name/value pair to Session 78 Session. Add(language, ISBN) 79 End If 80 Invokes method Add to place the language 81 End Sub ' submit. Button_Click 82 End Class ' Options 2 and its corresponding recommended book’s Outline 61 ISBN number into Http. Session. State object Options. Pge 2. aspx. vb 2002 Prentice Hall. All rights reserved.
62 20. 6 Session Tracking 2002 Prentice Hall. All rights reserved.
63 20. 6 Session Tracking 2002 Prentice Hall. All rights reserved.
1 <%-- Fig. 20. 32: Recommendation. Page. aspx --%> 2 <%-- displays book recommendations based on session --%> 3 <%-- information --%> 4 5 <%@ Page Language="vb" Auto. Event. Wireup="false" 6 Codebehind="Recommendation. Page. aspx. vb" 7 Inherits="Sessions. Recommendations" %> 8 9 <!DOCTYPE HTML PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN" > 10 <HTML> 11 <HEAD> 12 <meta content="Microsoft Visual Studio. NET 7. 0" 13 name="GENERATOR"> 14 <meta content="Visual Basic 7. 0" name="CODE_LANGUAGE"> 15 <meta content="Java. Script" name="vs_default. Client. Script"> 16 <meta name="vs_target. Schema" 17 content="http: //schemas. microsoft. com/ intellisense/ie 5"> 18 </HEAD> 19 <body MS_POSITIONING= "Grid. Layout"> 20 <form id="Form 1" method="post" runat="server"> 21 22 <asp: label id="recommendation. Label" 23 style="Z-INDEX: 101; LEFT: 55 px; Defines List. Box Web control that is used to 24 POSITION: absolute; TOP: 38 px" runat="server" present recommendations to the user 25 Font-Size="X-Large">Recommendations 26 </asp: label> 27 28 <asp: listbox id="books. List. Box" style="Z-INDEX: 102; 29 LEFT: 50 px; POSITION: absolute; TOP: 80 px" 30 runat="server" Width="442 px" Height="125 px"> 31 </asp: listbox> 32 33 </form> 34 </body> 35 </HTML> Outline 64 Recomendation. Page. aspx 2002 Prentice Hall. All rights reserved.
1 ' Fig. 20. 33: Recommendation. Page. aspx. vb 2 ' Reading cookie data from the client 3 4 Imports System. Web. UI. Web. Controls 5 6 Public Class Recommendations 7 Inherits Page 8 Protected With. Events recommendation. Label As Label 9 Protected With. Events books. List. Box As List. Box 10 11 ' Visual Studio. NET generated code 12 13 Private Sub Page_Init(By. Val sender As System. Object, _ 14 By. Val e As System. Event. Args) Handles My. Base. Init 15 16 Initialize. Component () 17 Method Page_Init retrieves the session information 18 Dim i As Integer 19 Dim key. Name As String 20 21 ' determine if Session contains information 22 If Session. Count <> 0 Then 23 Keys property returns a collection 24 ' iterate through Session values, containing all the keys in the session 25 ' display in List. Box 26 For i = 0 To Session. Count - 1 27 28 ' store current key in session. Name 29 key. Name = Session. Keys(i) Outline 65 Recommendations. Page. aspx. vb 2002 Prentice Hall. All rights reserved.
31 ' use current key to display 32 ' Session's name/value pairs 33 books. List. Box. Items. Add(key. Name & _ 34 " How to Program. ISBN#: " & _ 35 Session(key. Name)) 36 Next Concatenates key. Name’s value to the String 37 Else 38 recommendation. Label. Text = "No Recommendations" 39 books. List. Box. Visible = False 40 End If 41 End Sub ' Page_Init 42 43 Private Sub Page_Load(By. Val sender As System. Object, _ 44 By. Val e As System. Event. Args) Handles My. Base. Load 45 46 'Put user code to initialize the page here 47 End Sub ' Page_Load 48 End Class ' Recommendations Outline 66 Recommendations. Page. aspx. vb 2002 Prentice Hall. All rights reserved.
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 server’s machine 2002 Prentice Hall. All rights reserved.
68 20. 7 Case Study: Online Guest book 2002 Prentice Hall. All rights reserved.
1 <%-- Fig 20. 35: Guestbook. Page. aspx --%> 2 <%-- controls and layout for guestbook application --%> 3 4 <%@ Page Language="vb" Auto. Event. Wireup="false" 5 Codebehind="Guestbook. Page. aspx. vb" 6 Inherits="Guestbook"%> 7 8 <!DOCTYPE HTML PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN" > 9 <HTML> 10 <HEAD> 11 <title>Guest. Book</title> 12 <meta content="Microsoft Visual Studio. NET 7. 0" 13 name="GENERATOR"> 14 <meta content="Visual Basic 7. 0" name="CODE_LANGUAGE"> 15 <meta content="Java. Script" name="vs_default. Client. Script"> 16 <meta content= 17 "http: //schemas. microsoft. com/ intellisense/ie 5" 18 name="vs_target. Schema"> 19 </HEAD> 20 <body MS_POSITIONING= "Grid. Layout"> 21 <form id="Form 1" method="post" runat="server"> 22 23 <asp: Label id="prompt. Label" style="Z-INDEX: 101; 24 LEFT: 39 px; POSITION: absolute; TOP: 20 px" Label Web control 25 runat="server" Font-Size="X-Large" 26 Fore. Color="Blue"> 27 Please leave a message in our guestbook: 28 </asp: Label> 29 30 <asp: Button id= "clear. Button" style="Z-INDEX: 110; 31 LEFT: 383 px; POSITION: absolute; TOP: 318 px" 32 runat="server" Width="56 px" Text="Clear"> 33 </asp: Button> Outline 69 Guestbook. Page. aspx 2002 Prentice Hall. All rights reserved.
35 <asp: Button id= "submit. Button" style="Z-INDEX: 109; 36 LEFT: 187 px; POSITION: absolute; TOP: 319 px" 37 runat="server" Text="Submit"> 38 </asp: Button> 39 40 <asp: Data. Grid id="Data. Grid 1" style="Z-INDEX: 108; 41 LEFT: 39 px; POSITION: absolute; TOP: 372 px" 42 runat="server" Width="541 px" Height="95 px" 43 Horizontal. Align ="Left" Border. Color="#E 7 E 7 FF" 44 Border. Width="1 px" Grid. Lines="None" Cell. Padding="3" 45 Page. Size="5"> 46 47 <Selected. Item. Style Fore. Color ="#F 7 F 7 F 7" 48 Back. Color ="#738 A 9 C"> 49 </Selected. Item. Style> 50 <Alternating. Item. Style Back. Color ="#F 7 F 7 F 7"> 51 </Alternating. Item. Style> 52 <Item. Style Horizontal. Align ="Left" Width="100 px" 53 Fore. Color ="#4 A 3 C 8 C" Back. Color="#E 7 E 7 FF"> 54 </Item. Style> 55 <Header. Style Fore. Color="#F 7 F 7 F 7" 56 Back. Color ="#4 A 3 C 8 C"> 57 </Header. Style> 58 <Footer. Style Fore. Color="#4 A 3 C 8 C" 59 Border. Color ="#B 5 C 7 DE"> 60 </Footer. Style> 61 <Pager. Style Horizontal. Align ="Right" 62 Fore. Color ="#4 A 3 C 8 C" Back. Color="#E 7 E 7 FF" 63 Mode="Numeric. Pages"> 64 </Pager. Style> 65 </asp: Data. Grid> Outline 70 Guestbook. Page. aspx 2002 Prentice Hall. All rights reserved.
67 <asp: Text. Box id= "message. Text. Box" style="Z-INDEX: 107; 68 LEFT: 135 px; POSITION: absolute; TOP: 181 px" 69 runat="server" Width="449 px" Height="113 px" 70 Text. Mode="Multi. Line"> 71 </asp: Text. Box> 72 73 <asp: Text. Box id= "email. Text. Box" style="Z-INDEX: 106; 74 LEFT: 135 px; POSITION: absolute; TOP: 132 px" 75 runat="server" Width="449 px"> 76 </asp: Text. Box> 77 78 <asp: Text. Box id= "name. Text. Box" style="Z-INDEX: 105; 79 LEFT: 135 px; POSITION: absolute; TOP: 85 px" 80 runat="server" Width="449 px"> 81 </asp: Text. Box> 82 83 <asp: Label id="message. Label" style="Z-INDEX: 104; 84 LEFT: 39 px; POSITION: absolute; TOP: 167 px" 85 runat="server" Width="51 px">Tell the world: 86 </asp: Label> 87 88 <asp: Label id="email. Label" style="Z-INDEX: 103; 89 LEFT: 39 px; POSITION: absolute; TOP: 118 px" 90 runat="server" Width="69 px">Your email address: 91 </asp: Label> 92 93 <asp: Label id="name. Label" style="Z-INDEX: 102; 94 LEFT: 39 px; POSITION: absolute; TOP: 90 px" 95 runat="server">Your name: 96 </asp: Label> 97 98 </form> 99 </body> 100 </HTML> Outline 71 Guestbook. Page. aspx 2002 Prentice Hall. All rights reserved.
1 ' Fig. 20. 36: Guestbook. Page. aspx 2 ' The code-behind file for the guest book page. 3 4 Imports System. Web. UI. Web. Controls 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 With. Events prompt. Label As Label 13 Protected With. Events name. Label As Label 14 Protected With. Events email. Label As Label 15 Protected With. Events message. Label As Label 16 Protected With. Events data. Grid As Data. Grid 17 Protected With. Events submit. Button As Button 18 Protected With. Events message. Text. Box As Text. Box 19 Protected With. Events email. Text. Box As Text. Box 20 Protected With. Events name. Text. Box As Text. Box 21 Protected With. Events clear. Button As Button 22 Protected With. Events data. View As System. Data. View 23 24 ' Visual Studio. NET generated code 25 26 Private Sub Page_Load(By. Val sender As System. Object, _ 27 By. Val e As System. Event. Args) Handles My. Base. Load 28 29 'Put user code to initialize the page here 30 data. View = New Data. View(New Data. Table()) 31 32 End Sub Outline 72 Guestbook. Page. aspx 2002 Prentice Hall. All rights reserved.
34 ' clear text boxes; user can enter new input 35 Private Sub clear. Button_Click(By. Val sender As System. Object, _ 36 By. Val e As System. Event. Args) Handles clear. Button. Click 37 38 name. Text. Box. Text = "" 39 email. Text. Box. Text = "" Clears each Text. Box by setting its 40 message. Text. Box. Text = "" Text property to an empty string 41 End Sub ' clear. Button_Click 42 43 Public Sub Fill. Message. Table() 44 Dim table As New Data. Table() 45 Dim reader As Stream. Reader 46 Dim separator As Char() 47 Dim message As String 48 Dim parts As String() 49 50 table = data. View. Table 51 52 table. Columns. Add( "Date") 53 table. Columns. Add( "First. Name") 54 table. Columns. Add( "email") 55 table. Columns. Add( "Message") 56 57 ' open guestbook file for reading 58 reader = New Stream. Reader( _ 59 Request. Physical. Application. Path & "guestbook. txt") 60 61 separator = New Char() {vb. Tab} 62 63 ' read one line from file 64 message = reader. Read. Line() Outline 73 Guestbook. Page. aspx 2002 Prentice Hall. All rights reserved.
66 While message <> "" 67 68 ' split String into four parts 69 parts = message. Split(separator) 70 71 ' load data into table 72 table. Load. Data. Row(parts, True) 73 74 ' read one line from file 75 message = reader. Read. Line() 76 End While 77 78 data. Grid. Data. Bind() ' update grid 79 80 reader. Close() 81 End Sub ' Fill. Message. Table 82 83 ' add the user's entry to the guestbook 84 Private Sub submit. Button_Click(By. Val sender As System. Object, _ 85 By. Val e As System. Event. Args) Handles submit. Button. Click Append appropriate message 86 87 Dim guestbook As Stream. Writer to the guest-book file 88 89 ' open stream for appending to file Adds the user’s information to guestbook. text 90 guestbook = New Stream. Writer( _ 91 Request. Physical. Application. Path & _ 92 "guestbook. txt", True) 93 Creates a Stream. Writer that references 94 ' write new message to file the file containing the guestbook entries 95 guestbook. Write. Line( _ 96 Date. Time. Now. Date. To. String(). Substring( 0, 10) & _ 97 vb. Tab & name. Text. Box. Text & vb. Tab & email. Text. Box. Text & _ 98 vb. Tab & message. Text. Box. Text) Outline 74 Guestbook. Page. aspx 2002 Prentice Hall. All rights reserved.
100 ' clear textboxes and close stream 101 name. Text. Box. Text = "" 102 email. Text. Box. Text = "" 103 message. Text. Box. Text = "" Invokes method Fill. Message. Table 104 guestbook. Close() 105 106 Fill. Message. Table() 107 End Sub ' submit. Button_Click 108 End Class ' Guestbook Outline 2002 Prentice Hall. All rights reserved. 75
76 20. 7 Case Study: Online Guest book 2002 Prentice Hall. All rights reserved.
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 2002 Prentice Hall. All rights reserved. 77
1 <%-- Fig. 20. 37: login. aspx --%> 2 <%-- controls and formatting for login page --%> 3 4 <%@ Register Tag. Prefix="Header" Tag. Name="Image. Header" 5 Src="Image. Header. ascx" %> 6 7 <%@ Page Language="vb" Auto. Event. Wireup="false" Web user control to the ASPX file 8 Codebehind="login. aspx. vb" 9 Inherits="Database. Login"%> 10 11 <!DOCTYPE HTML PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN" > 12 <HTML> 13 <HEAD> 14 <title></title> 15 <meta content="Microsoft Visual Studio. NET 7. 0" 16 name="GENERATOR"> 17 <meta content="Visual Basic 7. 0" name="CODE_LANGUAGE"> 18 <meta content="Java. Script" name="vs_default. Client. Script"> 19 <meta name="vs_target. Schema" 20 content="http: //schemas. microsoft. com/ intellisense/ie 5"> 21 </HEAD> 22 <body bg. Color="#ffebff" MS_POSITIONING="Grid. Layout"> Label Web control 23 <form id="Form 1" method="post" runat="server"> 24 25 <asp: label id="prompt. Label" style="Z-INDEX: 108; 26 LEFT: 20 px; POSITION: absolute; TOP: 144 px" 27 runat="server">Please select your name and enter 28 your password to log in: 29 </asp: label> 30 31 <asp: label id="name. Label" style="Z-INDEX: 101; 32 LEFT: 15 px; POSITION: absolute; TOP: 188 px" 33 runat="server">Name 34 </asp: label> Outline Login. aspx 2002 Prentice Hall. All rights reserved. 78
36 <asp: dropdownlist id="name. List" style="Z-INDEX: 105; 37 LEFT: 92 px; POSITION: absolute; TOP: 185 px" 38 runat="server" Width="154 px"> 39 </asp: dropdownlist> 40 41 <asp: label id="password. Label" style="Z-INDEX: 102; 42 LEFT: 15 px; POSITION: absolute; TOP: 220 px" 43 runat="server">Password 44 </asp: label> 45 46 <asp: textbox id= "password. Text. Box" style="Z-INDEX: 103; 47 LEFT: 92 px; POSITION: absolute; TOP: 221 px" Custom. Validator specifies the circumstances 48 runat="server" Text. Mode="Password"> under which a field is valid 49 </asp: textbox> 50 51 <asp: customvalidator id="invalid. Password. Validator " 52 style="Z-INDEX: 107; LEFT: 262 px; POSITION: 53 absolute; TOP: 221 px" runat="server" A Required. Field. Validator specifies 54 Control. To. Validate ="password. Text. Box" Font-Bold="True" the user to enter a password 55 Fore. Color="Dark. Cyan" Error. Message="Invalid password!"> 56 </asp: customvalidator> 57 58 <asp: requiredfieldvalidator id= 59 "required. Password. Validator " style="Z-INDEX: 106; 60 LEFT: 262 px; POSITION: absolute; TOP: 221 px" 61 runat="server" Control. To. Validate="password. Text. Box" 62 Font-Bold="True" Fore. Color="Dark. Cyan" 63 Error. Message="Please enter a password!" > 64 </asp: requiredfieldvalidator> 65 66 <asp: button id= "submit. Button" style="Z-INDEX: 104; 67 LEFT: 92 px; POSITION: absolute; TOP: 263 px" 68 runat="server" Text="Submit"> 69 </asp: button> Outline Login. aspx 2002 Prentice Hall. All rights reserved. 79
71 <Header: Image. Header id="Image. Header 1" runat="server"> 72 </Header: Image. Header> 73 74 </form> 75 </body> 76 </HTML> Outline Login. aspx 1 <%-- Fig. 20. 38: Image. Header. ascx -%> 2 <%-- Listing for the header user control --%> 3 Instantiates user control’s tag name 4 <%@ Control Language="vb" Auto. Event. Wireup="false" 5 Codebehind="Image. Header. ascx. vb" 6 Inherits="Database. Image. Header" 7 Target. Schema="http: //schemas. microsoft. com/int Image. Header. ascx 2002 Prentice Hall. All rights reserved. 80
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. Component. Model 7 Imports System. Data 8 Imports System. Data. Ole. Db 9 Imports System. Drawing 10 Imports System. Web 11 Imports System. Web. Session. State 12 Imports System. Web. UI 13 Imports System. Web. UI. Web. Controls 14 Imports System. Web. UI. Html. Controls 15 Imports System. Web. Security 16 17 Public Class Login 18 Inherits System. Web. UI. Page 19 20 Protected With. Events required. Password. Validator As _ 21 Required. Field. Validator 22 23 Protected With. Events invalid. Password. Validator As _ 24 Custom. Validator 25 26 Protected With. Events submit. Button As Button 27 Protected With. Events password. Text. Box As Text. Box 28 Protected With. Events password. Label As Label 29 Protected With. Events name. List As Drop. Down. List 30 Protected With. Events name. Label As Label Outline Login. aspx. vb 2002 Prentice Hall. All rights reserved. 81
Outline 31 Protected With. Events Ole. Db. Data. Adapter 1 As Ole. Db. Data. Adapter 32 Protected With. Events Ole. Db. Select. Command 1 As Ole. Db. Command 33 Protected With. Events Ole. Db. Insert. Command 1 As Ole. Db. Command 34 Protected With. Events Ole. Db. Update. Command 1 As Ole. Db. Command 35 Protected With. Events Ole. Db. Delete. Command 1 As Ole. Db. Command 36 Protected With. Events Ole. Db. Connection 1 As Ole. Db. Connection 37 Protected With. Events prompt. Label As Label 38 Protected data. Reader As Ole. Db. Data. Reader 39 40 ' Visual Studio. NET generated code 41 42 Private Sub Page_Init(By. Val sender As System. Object, _ 43 By. Val e As System. Event. Args) Handles My. Base. Init 44 45 Initialize. Component () 46 Defines method Page_Init 47 ' if page loads due to postback, process information 48 ' otherwise, page is loading for first time, so Login. aspx. vb 49 ' do nothing 50 If Not Is. Post. Back Then 51 52 ' open database and read data 53 Try 54 ' open database connection 55 Ole. Db. Connection 1. Open() 56 Try/Catch/Finally block handles any database connectivity 57 ' execute query 58 data. Reader = _ exceptions and ensures that the database is closed 59 Ole. Db. Data. Adapter 1. Select. Command. Execute. Reader() 60 Iterates through the rows and places items 61 ' while we can read row from query result, 62 ' add first item to drop-down list in the first column of each row 63 While (data. Reader. Read()) 64 name. List. Items. Add(data. Reader. Get. String(0)) 2002 Prentice Hall. 65 End While All rights reserved. 82
67 ' catch error if database cannot be opened 68 Catch exception As Ole. Db. Exception 69 Response. Write( "Unable to open database!" ) 70 71 ' close database 72 Finally 73 ' close database connection 74 Ole. Db. Connection 1. Close() 75 End Try 76 End If 77 End Sub ' Page_Init 78 79 ' validate user name and password 80 Private Sub invalid. Password. Validator _Server. Validate( _ 81 By. Val source As Object, _ 82 By. Val args As Server. Validate. Event. Args ) _ 83 Handles invalid. Password. Validator. Server. Validates the user’s password 84 85 ' open database and check password 86 Try 87 ' open database connection 88 Ole. Db. Connection 1. Open() 89 90 ' set select command to find password of username 91 ' from drop-down list 92 Ole. Db. Data. Adapter 1. Select. Command. Text = _ 93 "SELECT * FROM Users WHERE login. ID = '" & _ 94 Request. Form( "name. List"). To. String() & "'" 95 96 data. Reader = _ 97 Ole. Db. Data. Adapter 1. Select. Command. Execute. Reader() 98 99 data. Reader. Read() Outline Login. aspx. vb 2002 Prentice Hall. All rights reserved. 83
101 ' if password user provided is correct create 102 ' authentication ticket for user and redirect 103 ' user to Authors. aspx; otherwise set Is. Valid to false 104 If args. Value = data. Reader. Get. String(1) Then 105 Forms. Authentication. Set. Auth. Cookie( _ 106 Request. Form( "namelist"), False) 107 Session. Add( "name", _ Executes if user’s password is confirmed 108 Request. Form( "name. List"). To. String()) 109 Response. Redirect( "Authors. aspx") 110 Else 111 args. Is. Valid = False 112 113 End If 114 115 ' display error if unable to open database 116 Catch exception As Ole. Db. Exception 117 Response. Write( "Unable to open database!" ) 118 119 ' close database 120 Finally 121 ' close database connection 122 Ole. Db. Connection 1. Close() 123 End Try 124 End Sub ' Invalid. Password. Validator _Server. Validate 125 End Class ' Login Outline Login. aspx. vb 2002 Prentice Hall. All rights reserved. 84
20. 8 Case Study: Connecting to a Database in ASP. NET 2002 Prentice Hall. All rights reserved. 85
20. 8 Case Study: Connecting to a Database in ASP. NET 2002 Prentice Hall. All rights reserved. 86
1 <%-- Fig. 20. 40: Authors. aspx --%> 2 <%-- displays book titles based on author name --%> 3 <%-- from database --%> 4 5 <%@ Page Language="vb" Auto. Event. Wireup="false" 6 Codebehind="Authors. aspx. vb" 7 Inherits="Database. Authors"%> 8 9 <%@ Register Tag. Prefix="Header" Tag. Name="Image. Header" 10 Src="Image. Header. ascx" %> 11 12 <!DOCTYPE HTML PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN" > 13 <HTML> 14 <HEAD> 15 <title>Authors</title> 16 <meta name="GENERATOR" 17 content="Microsoft Visual Studio. NET 7. 0" > 18 <meta name="CODE_LANGUAGE" content="Visual Basic 7. 0"> 19 <meta name="vs_default. Client. Script" content="Java. Script"> 20 <meta name="vs_target. Schema" 21 content="http: //schemas. microsoft. com/ intellisense/ie 5"> 22 </HEAD> 23 <body MS_POSITIONING= "Grid. Layout" bg. Color="#ffebff"> ASPX file creates a Data. Grid 24 <form id="Form 1" method="post" runat="server"> 25 26 <asp: Data. Grid id="data. Grid" style="Z-INDEX: 106; Visible property is set to False 27 LEFT: 15 px; POSITION: absolute; TOP: 131 px" 28 runat="server" Fore. Color="Black" Allow. Paging="True" 29 Data. Source="<%# data. View %>" Allow. Sorting="True" 30 Visible="False" Width="700 px" Height="23 px"> Outline Authors. aspx 2002 Prentice Hall. All rights reserved. 87
32 <Edit. Item. Style Back. Color="White"></Edit. Item. Style> 33 <Alternating. Item. Style Fore. Color ="Black" 34 Back. Color ="Light. Goldenrod. Yellow"> 35 </Alternating. Item. Style> 36 <Item. Style Back. Color="White"></Item. Style> 37 <Header. Style Back. Color="Light. Green"></Header. Style> 38 <Pager. Style Next. Page. Text="Next & gt; " 39 Prev. Page. Text ="& lt; Previous"> ASPX file creates a Button 40 </Pager. Style> 41 </asp: Data. Grid> 42 43 <asp: Button id= "Button 1" style="Z-INDEX: 104; 44 LEFT: 29 px; POSITION: absolute; TOP: 188 px" 45 runat="server" Width="78 px" Text="Select"> ASPX file creates a Drop. Down. List and Label 46 </asp: Button> 47 48 <asp: Drop. Down. List id= "name. List" style="Z-INDEX: 103; 49 LEFT: 90 px; POSITION: absolute; TOP: 157 px" 50 runat="server" Width="158 px" Height="22 px"> 51 </asp: Drop. Down. List> 52 53 <asp: Label id="Label 2" style="Z-INDEX: 102; 54 LEFT: 28 px; POSITION: absolute; TOP: 157 px" 55 runat="server" Width="48 px" Height="22 px"> 56 Authors: 57 </asp: Label> Outline Authors. aspx 2002 Prentice Hall. All rights reserved. 88
59 <asp: Label id="Label 3" style="Z-INDEX: 105; 60 LEFT: 19 px; POSITION: absolute; TOP: 127 px" 61 runat="server" Visible="False" Width="210 px"> 62 You chose 63 </asp: Label> 64 Visible property is set to False 65 <Header: Image. Header id="Image. Header 1" runat="server"> 66 </Header: Image. Header> 67 68 </form> 69 </body> 70 </HTML> Outline Authors. aspx 2002 Prentice Hall. All rights reserved. 89
1 ' Fig. 20. 41: Authors. aspx. vb 2 ' The code-behind file for a page that allows a user to chose an 3 ' author and then view that author's books. 4 5 Imports System 6 Imports System. Data. Ole. Db 7 Imports System. Collections 8 Imports System. Component. Model 9 Imports System. Data 10 Imports System. Drawing 11 Imports System. Web 12 Imports System. Web. Session. State 13 Imports System. Web. UI 14 Imports System. Web. UI. Web. Controls 15 Imports System. Web. UI. Html. Controls 16 17 Public Class Authors 18 Inherits System. Web. UI. Page 19 20 Protected With. Events Label 3 As Label 21 Protected With. Events Label 2 As Label 22 Protected With. Events name. List As Drop. Down. List 23 Protected With. Events Button 1 As Button 24 Protected With. Events data. Grid As data. Grid 25 Protected With. Events Ole. Db. Data. Adapter 1 As Ole. Db. Data. Adapter 26 Protected With. Events Ole. Db. Select. Command 1 As Ole. Db. Command 27 Protected With. Events Ole. Db. Connection 1 As Ole. Db. Connection 28 Protected With. Events data. View As Data. View 29 Protected data. Table As New Data. Table() 30 Protected data. Reader As Ole. Db. Data. Reader 31 32 ' Visual Studio. NET generated code Outline Authors. aspx. vb 2002 Prentice Hall. All rights reserved. 90
34 Private Sub Page_Load(By. Val sender As System. Object, _ 35 By. Val e As System. Event. Args) Handles My. Base. Load 36 37 ' test if the page was loaded due to a post back 38 If Not Is. Post. Back Then 39 40 ' add data sort string 41 Session. Add("sort. String", "Title") Determines whether the page is 42 loaded as a result of postback event 43 ' open database connection 44 Try 45 Ole. Db. Connection 1. Open() 46 47 ' execute query 48 data. Reader = _ 49 Ole. Db. Data. Adapter 1. Select. Command. Execute. Reader() 50 51 ' while we can read a row from the result of the 52 ' query, add the first item to the dropdown list Retrieves all authors first and 53 While (data. Reader. Read()) 54 name. List. Items. Add(data. Reader. Get. String(0) & _ last names from the database 55 " " & data. Reader. Get. String(1)) 56 End While 57 58 ' if database cannot be found Iterates through the result set and adds the 59 Catch exception As System. Data. Ole. Db. Exception 60 Label 3. Text = "Server Error: Unable to load database!" authors’ first and last names to namelist 61 62 Finally 'close database connection 63 Ole. Db. Connection 1. Close() 64 End Try Outline Authors. aspx. vb 2002 Prentice Hall. All rights reserved. 91
65 Else 66 ' set some controls to be invisible 67 name. List. Visible = False 68 Button 1. Visible = False 69 Label 2. Visible = False 70 71 ' set other controls to be visible 72 Label 3. Visible = True Adds the selected author’s name to the label control 73 data. Grid. Visible = True 74 75 ' add the author name to the label 76 Label 3. Text = "You Chose " & name. List. Selected. Item. Text _ 77 & ". " 78 Dim author. ID As Integer = name. List. Selected. Index + 1 79 80 Try Creates a database query to retrieve 81 ' open database connection 82 Ole. Db. Connection 1. Open() the title, ISBN ad publisher name 83 84 ' grab the title, ISBN and publisher name for each book 85 Ole. Db. Data. Adapter 1. Select. Command. Text = _ 86 "SELECT Titles. Title, Titles. ISBN, " & _ 87 "Publishers. Publisher. Name FROM Author. ISBN " & _ 88 "INNER JOIN Titles ON Author. ISBN = " & _ 89 "Titles. ISBN, Publishers WHERE " & _ 90 "(Author. ISBN. Author. ID = " & author. ID & ")" 91 92 ' fill dataset with results 93 Ole. Db. Data. Adapter 1. Fill( data. Table) 94 data. View = New Data. View(data. Table) 95 data. View. Sort = Session("sort. String") 96 data. Grid. Data. Bind() ' bind grid to data source Outline Authors. aspx. vb 2002 Prentice Hall. All rights reserved. 92
98 ' if database cannot be found 99 Catch exception As System. Data. Ole. Db. Exception 100 101 Label 3. Text = "Server Error: Unable to load database!" 102 Finally ' close database connection 103 Ole. Db. Connection 1. Close() 104 End Try 105 End If Handles the Data. Grid’s Page. Index. Changed 106 End Sub ' Page_Load event when user clicks the Next link 107 108 ' handles data. Grid page changed event 109 Private Sub On. New. Page(By. Val sender As Object, _ 110 By. Val e As Data. Grid. Page. Changed. Event. Args ) _ 111 Handles data. Grid. Page. Index. Changed 112 113 ' set current page to next page 114 data. Grid. Current. Page. Index = e. New. Page. Index 115 116 data. View. Sort = Session("sort. String") 117 data. Grid. Data. Bind() ' rebind data 118 119 End Sub ' On. New. Page 120 121 ' handles Sort event 122 Private Sub data. Grid_Sort. Command(By. Val source As Object, _ 123 By. Val e As Data. Grid. Sort. Command. Event. Args ) _ 124 Handles data. Grid. Sort. Command 125 126 ' get table to sort 127 Session. Add("sort. String", e. Sort. Expression. To. String()) Method data. Grid_Sort. Command handles 128 data. View. Sort = Session("sort. String") ' sort the sort event of the Data. Grid control 129 data. Grid. Data. Bind() ' rebind data 130 131 End Sub ' data. Grid_Sort. Command Outline Authors. aspx. vb 2002 Prentice Hall. All rights reserved. 93
132 End Class ' Authors Outline Authors. aspx. vb 2002 Prentice Hall. All rights reserved. 94
20. 8 Case Study: Connecting to a Database in ASP. NET 2002 Prentice Hall. All rights reserved. 95
96 20. 9 Tracing • ASP. NET – Tracing • Is the process of placing statements throughout the codebehind file that output information during executions about the program’s status – Page Tracing • Involves the tracing of the actions of an individual page 2002 Prentice Hall. All rights reserved.
97 20. 9 Tracing 2002 Prentice Hall. All rights reserved.
98 20. 9 Tracing 2002 Prentice Hall. All rights reserved.
20. 10 Internet and World Wide Web Resources • Resources – – – – www. asp. net www. asp 101. com/aspplus www. 411 asp. net www. aspfree. com www. aspng. com www. aspnetfaq. com www. 123 aspx. com 2002 Prentice Hall. All rights reserved. 99
100 20. 10 Internet and World Wide Web 2002 Prentice Hall. All rights reserved.
- Slides: 100