12
CIS 3260 Intro to Programming in C# Dr. David McDonald Chapter 9 Page 1 McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Web Applications McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-2 Explain the functions of the server and the client in Web programming Create a Web Form and run it in a browser Describe the differences among the various types of Web controls and the relationship of Web controls to controls used on Windows forms Understand the event structure required for Web programs McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-3 Create multiple pages in a Web application Navigate using the HyperLink control and the Server object Validate Web input using the validator controls Use state management to preserve values in variables

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All ... PDFs/Ch09.pdf · CIS 3260 –Intro to Programming in C# Dr. David McDonald Chapter 9 –Page 2 McGraw-Hill © 2010 The

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All ... PDFs/Ch09.pdf · CIS 3260 –Intro to Programming in C# Dr. David McDonald Chapter 9 –Page 2 McGraw-Hill © 2010 The

CIS 3260 – Intro to Programming in C#

Dr. David McDonald Chapter 9 – Page 1

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved.

Web Applications

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-2

Explain the functions of the server and the client in Web programming

Create a Web Form and run it in a browser

Describe the differences among the various types of Web controls and the relationship of Web controls to controls used on Windows forms

Understand the event structure required for Web programs

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-3

Create multiple pages in a Web application

Navigate using the HyperLink control and the Server object

Validate Web input using the validatorcontrols

Use state management to preserve values in variables

Page 2: McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All ... PDFs/Ch09.pdf · CIS 3260 –Intro to Programming in C# Dr. David McDonald Chapter 9 –Page 2 McGraw-Hill © 2010 The

CIS 3260 – Intro to Programming in C#

Dr. David McDonald Chapter 9 – Page 2

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-4

Web Forms are used to create the user interface for Web applications

A Web Form displays as a document in a browser

You can create documents that display on mobile devices, such as cell phones and PDAs (using the Standard Edition and above)

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-5

• Web applications require a server and

a client

– Server sends the Web Pages to the client

– Client displays the Web Pages in a

browser

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-6

To develop Web applications◦ Use a remote Web server

◦ Make your local machine a Web server

Visual Studio Web Server

Installs automatically with Visual Web Developer or Visual Studio

Simplifies development, testing, and debugging

Use IDE tools to transfer application to an IIS Web server after development to share with others

Internet Information Services (IIS)

Page 3: McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All ... PDFs/Ch09.pdf · CIS 3260 –Intro to Programming in C# Dr. David McDonald Chapter 9 –Page 2 McGraw-Hill © 2010 The

CIS 3260 – Intro to Programming in C#

Dr. David McDonald Chapter 9 – Page 3

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-7

Browsers display pages written in a markup language such as HTML or XHTML

Pages may also contain program logic ◦ Script JavaScript, VBScript, JScript

◦ Java applets

Programming for the Internet◦ Test and check output on multiple browsers Internet Explorer, Mozilla FireFox and Opera

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-8

Web pages are stateless◦ The page does not store any information about

its contents from one invocation to the next

Techniques for working around the stateless limitation◦ Storing cookies on the local machine◦ Sending state information to the server as part of

the page's address, called the uniform resource locator (URL) The server can send the state information back with the

next version of the page

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-9

ASP.NET 3.5 is the latest Web programming technology from Microsoft

ASP.NET provides libraries, controls, and programming support for programs that:◦ Interact with the user◦ Maintain state◦ Render controls◦ Display data◦ Generate appropriate HTML

Write object-oriented, event-driven Web applications using C# and ASP.NET

Page 4: McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All ... PDFs/Ch09.pdf · CIS 3260 –Intro to Programming in C# Dr. David McDonald Chapter 9 –Page 2 McGraw-Hill © 2010 The

CIS 3260 – Intro to Programming in C#

Dr. David McDonald Chapter 9 – Page 4

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-10

Each Web Form has two distinct pieces◦ HTML and instructions needed to render the page◦ C# code

A Web Form generates two files:◦ .aspx extension for the HTML◦ .aspx.cs extension for the C# code Holds the logic to respond to events

Called the "code behind" file

C# code is not compiled into an .exe file, it is compiled into a dynamic link library (dll) file located on the server

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-11

IIS on a local computer hosts a Web server that must be secured

Requires administrative rights on the computer to create IIS Web projects

By default files are stored in C:\inetpub\wwwroot folder◦ A virtual IIS directory can be created to store a Web

site

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-12

Select New Web Site from the File menu

In the New Web Site dialog box, select the template, location, and language

Give the Web site a name by appendingthe name tothe location

Page 5: McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All ... PDFs/Ch09.pdf · CIS 3260 –Intro to Programming in C# Dr. David McDonald Chapter 9 –Page 2 McGraw-Hill © 2010 The

CIS 3260 – Intro to Programming in C#

Dr. David McDonald Chapter 9 – Page 5

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-13

A new Web site automatically contains one Web page, Default.aspx◦ Visual representation of the page (HTML tags)

A second file, Default.aspx.cs, holds the C# code for the page

ASP.NET has a second model for Web applications—single-file model◦ Stores all HTML, control definitions, and code in

one single file The textbook teaches the code-separation

model that uses two separate files

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-14

Differences between a Web application and a Windows Forms application◦ Web Form (page or document) not a Windows

Form

◦ Toolbar

◦ List of files in the Solution Explorer

◦ Toolbox has different controls Properties and events different than controls that have a

Windows counterpart

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-15

Three tabs at bottom of Web Form◦ Design

◦ Split

Splits the Document window horizontally to see both the design and source

◦ Source

Static HTML code

Page 6: McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All ... PDFs/Ch09.pdf · CIS 3260 –Intro to Programming in C# Dr. David McDonald Chapter 9 –Page 2 McGraw-Hill © 2010 The

CIS 3260 – Intro to Programming in C#

Dr. David McDonald Chapter 9 – Page 6

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-16

Several types of controls available for Web Forms◦ Standard (ASP.NET server controls)—Richest and

most powerful controls provided by ASP.NET and .NET framework

◦ Data—Display database data◦ Validation—Validate user input before it is sent to

server◦ Navigation—Includes the Menu control◦ Login—Login controls and wizards◦ WebParts—Change the appearance and behavior

of the interface from the browser

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-17

Web application files differ greatly from a Windows application

aspx file◦ Holds specifications for the user interface used by

the server to render pages

aspx.cs file◦ Code-behind file for the aspx file

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-18

The IDE does not automatically generate the code necessary for debugging a Web application

Run without Debugging◦ Ctrl + F5 or Debug/Start without debugging

Page 7: McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All ... PDFs/Ch09.pdf · CIS 3260 –Intro to Programming in C# Dr. David McDonald Chapter 9 –Page 2 McGraw-Hill © 2010 The

CIS 3260 – Intro to Programming in C#

Dr. David McDonald Chapter 9 – Page 7

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-19

If you try to run with debugging (F5), a message box gives the choice to run without debugging or add the debugging code to the Web.config file◦ Add the following line of code to the Web.config

file to run with debugging<compilation debug = “true” />

Set breakpoints, single step execution, and display the contents of variables and properties at debugging time

Always remove debugging support before deploying an application◦ Debugging code slows an application

considerably

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-20

HTML tables contain rows and columns and allow control over placement of elements on a Web page

Add controls and text to the table cells to align the columns

A table is an HTML control, which requires no server-side programming

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-21

Add controls to any table cell or type text in a cell during design time◦ To refer to the text in a cell at run time, add a label

and give it an ID or type text directly into the cell

◦ Merge cells to allow room for a title or a larger image

Page 8: McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All ... PDFs/Ch09.pdf · CIS 3260 –Intro to Programming in C# Dr. David McDonald Chapter 9 –Page 2 McGraw-Hill © 2010 The

CIS 3260 – Intro to Programming in C#

Dr. David McDonald Chapter 9 – Page 8

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-22

Places controls in a specific X and Y position◦ Controls may not show up if user has set browser

window to small size

◦ Controls may overlap if the user has selected a large font size for the browser window

Controls can be dragged using the white tab at the top of a selected control

Controls remain in the same position relative to the Web page

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-23

ASP.NET provides several techniques to navigate from one Web page to another

Easiest form of navigation is to use a HyperLink control

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-24

Add a HyperLink control to allow the user to navigate to another page or another site

Set properties of the HyperLink control◦ Text—Text to display for the user

◦ NavigateUrl—Specify the URL to which to navigate

For another Web site, type the Web address (URL)

For another page in the application, use the Select URL dialog box and select the page from a list

Page 9: McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All ... PDFs/Ch09.pdf · CIS 3260 –Intro to Programming in C# Dr. David McDonald Chapter 9 –Page 2 McGraw-Hill © 2010 The

CIS 3260 – Intro to Programming in C#

Dr. David McDonald Chapter 9 – Page 9

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-25

Add a new form to the project

Add controls to the new page

Add a HyperLink to the Main Page

Set the Start Page

Run the application, test the links

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-26

At times a hyperlink cannot be used because some other action in code needs to be performed prior to the transfer

Transfer to another page in code◦ Use the Server.Transfer method

Server.Transfer (“Offer.aspx”);

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-27

ASP.NET provides several validator controlsthat can automatically validate input◦ Add a validator control◦ Attach it to the controlToValidate (such as a text

box)

A blank entry passes the validation for each of the controls except the RequiredFieldValidator

Enter a complete message in the ErrorMessage property or set the property to an asterisk

Page 10: McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All ... PDFs/Ch09.pdf · CIS 3260 –Intro to Programming in C# Dr. David McDonald Chapter 9 –Page 2 McGraw-Hill © 2010 The

CIS 3260 – Intro to Programming in C#

Dr. David McDonald Chapter 9 – Page 10

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-28

Validator ControlsRequiredFieldValidator Requires that the user enter something into the

field

CompareValidator Compares the value in the field to the value in

another control or to a constant. Also can be

used to verify that the input value can be

converted to the correct data type

RangeValidator Requires that the input value falls in the specified

range

RegularExpressionValidator Validates against a regular expression, similar to

the mask in a masked text box

ValidationSummary Displays a summary of all of the messages from

the validator controls

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-29

Web pages hold static data Each time a page is displayed or redisplayed

it is a fresh copy Each time the page is posted back to the

server a new copy of the program is loaded Steps must be taken to maintain the state of

the page◦ Otherwise the values of variables and controls on

the page will be lost in every postback

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-30

ASP.NET can retain and redisplay control contents

Web controls have an EnableViewState property◦ Set to true by default

◦ Indicates to the server to send the control’s contents back with the page

Page 11: McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All ... PDFs/Ch09.pdf · CIS 3260 –Intro to Programming in C# Dr. David McDonald Chapter 9 –Page 2 McGraw-Hill © 2010 The

CIS 3260 – Intro to Programming in C#

Dr. David McDonald Chapter 9 – Page 11

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-31

Local variables work the same in a Web or Windows application

Class-level variables are lost unless steps are taken to save them

Store the value of a class-level variable in a control on a Web page◦ EnableViewState property holds the value during

postback

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-32

Set up a control with its Visible property set to false or use the HiddenField control

Assign the class-level variable to the invisible control◦ For a hidden field, use the (string) Value property

// Declare a class level variable.private decimal discountTotalDecimal;

protected void submitButton_Click(object sender, EventArgs e){

// . . . Calculations omitted.discountHiddenField.Value = discountTotalDecimal.ToString();

}

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-33

The Page_Load event occurs many times (during each postback) in a Web application

IsPostBack property is set to false for an initial page load and true for all page loads following the first

Test for IsPostBack == false to perform initialization tasks

Test for IsPostBack == true to perform tasks only on postback

Page 12: McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All ... PDFs/Ch09.pdf · CIS 3260 –Intro to Programming in C# Dr. David McDonald Chapter 9 –Page 2 McGraw-Hill © 2010 The

CIS 3260 – Intro to Programming in C#

Dr. David McDonald Chapter 9 – Page 12

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-34

One instance of the Session object exists for each user of an application◦ Use to store information about the user on the

server◦ Each time a user accesses an application, a Session

object is created with a unique SessionID◦ Session values are maintained as long as the

session exists Sessions can time out, terminate or users can logout

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-35

Drawbacks to session objects◦ Information is stored on server

Large amounts of data for multiple users can bog down the server

◦ Web sites split server loads among several systems (Web Farm)

State information might be on a different server than where the postback originates

.NET handles this problem by specifying the name of the machine that stores the session values in the Web.config file

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 9-36

Use the Contents collection of the Session object to store values in code

Each item in the collection is a key/value pair◦ Make up a name (the key) and assign a value

Clear all session variables by using the Session.Clear method