Web-Based Applications - Advanced Programming - Lecture Notes, Study notes of Advanced Computer Programming

The advance computer programming may not be a piece of cake for every one, but these slides really help you to understand the concept of the programming.Web-Based Applications, Investigates, Validation Controls, Windows Communication Foundation, Web-Based Applications, Windows Applications, Web Applications, Verify User Input, Display Calendars, Windows Communication

Typology: Study notes

2012/2013

Uploaded on 04/30/2013

archa
archa 🇮🇳

4.3

(15)

94 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C# Programming: From Problem Analysis to Program Design, 3rd ed. 15-1
Chapter 15
Web-Based Applications
At a Glance
Instructor’s Manual Table of Contents
Chapter Overview
Chapter Objectives
Instructor Notes
Quick Quizzes
Discussion Questions
Projects to Assign
Key Terms
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Web-Based Applications - Advanced Programming - Lecture Notes and more Study notes Advanced Computer Programming in PDF only on Docsity!

Chapter 15

Web-Based Applications

At a Glance

Instructor’s Manual Table of Contents

♦ Chapter Overview

♦ Chapter Objectives

♦ Instructor Notes

♦ Quick Quizzes

♦ Discussion Questions

♦ Projects to Assign

♦ Key Terms

Lecture Notes

Chapter Overview

This chapter investigates how the design of Web-based applications differs from Windows applications. Validation controls, calendar controls, and other types of widgets are added to Web pages using a drag-and-drop approach. A number of Web server controls are explored. The ADO.NET classes are used with the Web applications to provide database access. This chapter also includes an introduction to Windows Communication Foundation (WCF) and mobile applications.

Chapter Objectives

In this chapter, students will:

  • Discover how Web-based applications differ from Windows applications
  • Use ASP.NET to create Web applications
  • Develop and configure Web Forms pages
  • Learn about the different types of controls that can be added to Web applications
  • Add HTML and Web Forms server controls to Web applications
  • Add validation, custom, and composite controls to verify user input, display calendars, and connect to database tables
  • Become aware of Web Services and Windows Communication Foundation (WCF) and their implications for distributed applications
  • Learn how mobile applications are developed using the Compact Framework (optional)

Instructor Notes

WEB-BASED APPLICATIONS

A Web server is software that hosts or delivers the Web application. The hardware on which the Web server software is loaded is often called a Web server, but it is the software that makes the equipment special and thus enables the computer to be called a server.

Web Programming Model This model is somewhat different from Windows applications in terms of the interaction with users. Use of MessageBox dialog boxes would display output on the server computer instead of at the client computer requesting the page. Messages to users are normally displayed through the Label object or other objects on the page.

Requests to view a Web page require a round-trip to the server. The user requests the page via Hypertext Transfer Protocol (HTTP) by typing the Web address into a Web browser. That request is forwarded to the Web server on which the page is stored. The page is then sent back

Quick Quiz

  1. True or False: The ASP model extends the HTML file by including script code in the same file. Answer: True
  2. Users request a Web page using _______________ by typing the Web address into a Web browser. Answer: Hypertext Transfer Protocol (HTTP)
  3. True or False: An ASP page usually does not include any interaction with the user. Answer: False
  4. Identify one way that interaction with users differs with Windows and Web applications. Answer: Use of MessageBox dialog on a Web application would send the output to the server computer as opposed to the client machine.

ASP.NET

ASP.NET is a programming model that includes a number of classes as part of the .NET Framework. ASP is interpreted; ASP.NET is compiled. An ASP.NET Web page file that contains the HTML tags ends with an .aspx file extension. This logic is stored in a separate file.

A Web server must be available for development with ASP.NET. Two options are available. One option is to use Microsoft’s Internet Information Services (IIS). Another option that works great for applications that are going to be run and tested only on the machine in which they are developed is the new built-in lightweight ASP.NET Development Server. This is included with Visual Studio and is part of the Visual Web Developer.

Visual Web Developer Visual Web Developer is one of the products launched by Microsoft as part of their Express line. Included with the Visual Web Developer is a built-in ASP.NET Development Server for testing and running Web sites.

An option for creating Web applications is to develop a file-based Web site. Selecting this option automatically uses the lightweight test server (ASP.NET Development Server). The File System feature enables Web sites to be stored and run from any directory on the local machine.

IIS

The other option available for creating Web application is to use IIS. When a new Web site is created and HTTP (as opposed to File System ) is selected for the Location , IIS is used. If the HTTP option is selected, you do not have to work directly with IIS. Most everything dealing with IIS happens behind the scenes. IIS requires a server-like operating system. One situation that is somewhat problematic about using IIS is the fact that users must have administrative debugging privileges.

Quick Quiz

  1. In order to create an ASP.NET application using IIS, you must have administrative ___________ privileges. Answer: debugging
  2. True or False: ASP.NET uses the script code stored in a separate file to enable interaction with users. Answer: False
  3. True or False: In order to create any ASP.NET application, you must have IIS installed. Answer: False
  4. List three ways traditional ASP pages differ from ASP.NET pages. Answer: 1) File extensions: ASP pages end with the .asp extension; an ASP.NET file that contains the html tags ends with an .aspx extension; 2) ASP is interpreted; ASP.NET is compiled; 3) ASP file embeds script code in the same file that contains the HTML tags; ASP.NET creates two files. The .aspx file contains the html, and the .cs contains the code- behind file.

WEB FORMS PAGES

Classes used to create .NET Web applications are part of the System.Web.UI namespace. This namespace includes a Control and a Page class. The Page class is instantiated when a Web application is created using Visual Studio.

Many of the same concepts used to create Windows applications are used when a Web site is developed. Both are designed with the event-driven model. There are fewer events with Web applications. There are some significant differences between a Windows and Web application. When an ASP.NET Web application is built, two separate files are created for the user interface. One of the files contains the visual HTML components, and the other contains the logic. The file storing the logic is referred to as the code-behind file. All the event-handler methods are stored in this code-behind file.

Creating a Web Site Instead of selecting File > New > Project , as was used with Windows applications, select File

New > Web Site from the File menu. Browsing is enabled when File System as the Location is selected.

ASP.NET Empty Web Site When you take the selection File, New,Web Site, ASP.NET Empty Web Site, no .aspx files are created.

HTML Document File The Properties window allows access to a number of HTML Document properties. There are fewer properties available than are available for a Windows application, and many use different identifiers. Review Table 15-1. It shows some of the page properties that can be set. Changes made to these properties are all made to the .aspx file, which is the file containing the HTML tags.

Quick Quiz

  1. True or False: When a Web site is created, if you select File System as the Location, you specify where on the local machine the Web application files are stored. Answer: True
  2. True or False: The code-behind file ends with the extension .aspx.cs. Answer: True
  3. By default, C:\localhost is physically mapped to ____________. Answer: C:\InetPub\wwwroot
  4. Describe two differences between building a Windows and a Web application. Answer: There are fewer events and properties with Web applications. With Web applications, two files are created for the user interface, one storing the markup tags and the other holding the code-behind statements.

CONTROLS

Several different types of controls can be added to a Web Forms page. They include Standard, HTML, AJAX Extensions, Web Parts, Validation, Navigation, Login, and Dynamic Data and Reporting. The Toolbox controls are visible in both Design and Source mode. Controls can be dragged and dropped onto the .aspx markup page as easily as on the design page.

HTML Controls The properties associated with the HTML controls are different from the Windows applications control properties. For example, to set the text for a Label object, type the value directly onto the label. To change font type or size, choose Style in the Properties windows. This brings up a Style Builder dialog box, where cascading style sheet (CSS) style attributes for these HTML controls are changed.

Adding HTML Controls Like a Windows application, the application can be executed from within Visual Studio. The page can also be opened within a Web browser using the application name because Visual Studio creates a virtual directory for it.

Adding controls to the form alters the code in the file containing the HTML (.aspx). It does not change the .aspx.cs (the code-behind file).

Web pages are stateless. When the Submit button is clicked, a postback to the Web server occurs. When the page is redisplayed upon return from the round-trip to the server, the values in the text box are lost and not available for redisplay. To retrieve the values entered by the user, one approach is to programmatically analyze the HTTP request. This has been the traditional method; however, it requires more coding. Another approach is to make the controls available to the Web server.

.aspx and .aspx.cs Files To add an event-handler method for the button click to an HTML control, “function Submit1_onclick( )” is added to the .aspx markup file. In order to provide functionality, script code would need to be written in the .aspx markup file. The HTML button can be converted to an HTML Server control button. When the HTML control is converted to an HTML server control, an event-handler method can be added to the .aspx.cs file.

Server Control Events Double-clicking on an HTML Server Control registers the event exactly as it was done with Windows applications. An empty event-handler method is added. This code is inserted into the code-behind (.aspx.cs) file. Now when the page is requested using HTTP, the Web server has access to these controls and can retrieve and process the data entered by the user.

HTML Server Controls The properties of HTML server controls are different from those associated using Windows applications. The Value property is used with Input (Text) field objects to get and set values. Value is also used to set the caption for the various button types.

Quick Quiz

  1. Instead of using the Text property to set the caption for the HTML Input (Text), you must use ________________ to get or set values. Answer: Value
  2. True or False: HTML controls map straight to HTML tags. Answer: True
  3. To change font type or size of a HTML control, choose __________ in the Properties windows. Answer: Style

Automatic PostBack ListBox, RadioButton, RadioButtonList, CheckBox, CheckBoxList, and DropDownList controls have a property called AutoPostBack that can be set to true to automatically trigger a postback to the server. Not only is a postback an expensive activity involving the server, but it is also more difficult to program. Thus, one should be judicious with changes to this AutoPostBack property. The server loads an ASP.NET Web page every time it is requested by a client browser and then unloads it back to the browser after it processes the server-side code to render the HTML.

Setting the Properties of the Controls Some of the properties to consider exploring are:

  • Id, Text, and GroupName properties for RadioButton objects
  • AutoPostBack for the radio button object; so the message is displayed as soon as the user clicks the radio button object, the AutoPostBack property is set to true
  • SelectionMode; for the ListBox object, the SelectionMode was left unchanged (set as Single)

Wiring Event-Handler Methods To wire the same event-handler method to more than one control, select the control and then select the method name in the Events Properties window.

Quick Quiz

  1. One difference between Windows and Web controls comes with naming the object. With Windows controls, the Name property is used; with Web Forms controls, the ___________ property is used. Answer: Id
  2. True or False: When a Web Forms Control object is dragged onto a form and its properties are set, entries are added to the code-behind file. Answer: False
  3. True or False: ListBox controls have a property called AutoPostBack, which is initially set to true to automatically trigger a postback to the server. Answer: False
  4. List three or four names that Server controls go by. Answer: Web controls, Web Forms server controls, ASP server controls, or Web Forms controls

VALIDATION, CUSTOM, AND COMPOSITE CONTROLS

These are controls that enable input to be validated or checked by the server. Review Table 15-

  1. To use one of these controls, drag the control to the Web Forms page and place it beside the

control to be validated. Then treat the validation control object like any other control. It is a class that has its own properties.

Validation Control Properties ControlToValidate: tie the validation control to a specific form control object such as a TextBox object ErrorMessage: set to the message you want to display when the input control does not pass the validation test Text: customize the error information displayed to users. Common practice is to set the Text properties of the validation controls to a red star (asterisk) and place it next to the input box to be validated. An asterisk is displayed at run time if the control fails validation.

The Text property and the ErrorMessage property both set the message that prints when validation fails. If both are set, the Text property overrides the ErrorMessage property.

Validation control does not have to be placed beside the control that it is validating. Place it where the error message should be displayed.

Page Validation CausesValidation: property found with some controls. By default, every Button object’s CausesValidation property is set to true. By default, page validation occurs when any button on the form is clicked. It is called page validation because every control field on the page that has an associated validation control is checked to see if it passes the validation rules.

If you do not want a specifid Button object to cause page validation, set its CausesValidation Property to false. As solutions are designed, careful attention should be given to which buttons on the page should cause page validation of the input controls.

Some of the validators are: RangeValidator: used to make sure that the value entered falls between a predetermined set of values RegularExpressionValidator: used to do pattern matching RequiredFieldValidator: used to check that an entry has a value

IsValid: property that can be used with the Page object to check to determine whether all validation controls on a page are currently valid. The property can be placed in a selection statement, and actions can be performed based on its Boolean result.

Calendar Control This control is used to display calendar months on a Web page. Once placed on the page, the calendar is live and users can use the calendar to view and select dates.

SelectedDate property: used to pick the month, day, and year for display. When the page is loaded, SelectedDate property is initially set to the current date.

DateTime Class Calendar control is based on the DateTime class. To programmatically work with the calendar, create an object of the DateTime class. Its declaration is placed in the code-behind file. Review

can be dragged to the form in exactly the same manner that other control objects are placed on the form.

Connecting to the Database Connection object must be instantiated. Use the same connection string that was used in the examples in Chapter 13. The connection string identifies the provider as an Access database. It also specifies the name of the database, including the full path to the database location.

A minor modification is needed to the code presented in Chapter 13 (only one line of code). The DataBind( ) method call is different for Web applications. To display the data using the DataGrid, objects of the DataAdapter and DataSet are used for both Web and Windows applications.

Retrieving Data from the Database As with Example 14-21, an SQL statement stored in the string identifier, sql, is used to select the records. Once selected, use the sql string object as the as the CommandText property value. The connection string object is set as the Connection property.

To use the Fill( ) method with the DataSet object, instantiate an object of the DataAdapter class. The SelectCommand property of the DataAdapter class is set to the CommandText object holding the SQL statement.

Using the DataAdapter’s Fill( ) method, the table from the actual Access database (memberTable) is used to populate the DataSet object (memberDS). The DataGrid object must be bound to the DataSet. Recall that the DataBind( ) method is the only statement that changed from the Windows application.

AccessDataSource Instead of writing the program statements to connect to the database and retrieve the data, another option is to use the data visual configuration tools and have these statements automatically generated. Review Chapter 13. Recall that when data-bound controls are dragged onto the application using Visual Studio 2005 or the Visual Web Developer Express edition product, a smart tag is available on the control that enables the binding data source to be selected.

Data source classes are available with ASP.NET that reduce the need for accessing individual Data Provider classes. These classes, AccessDataSource and SqlDataSource, simplify connecting an ASP.NET Web page to a database. All providers can use the SqlDataSource.

The AccessDataSource is a special class for Microsoft Access databases. Both of these classes use SQL queries to perform data retrieval. Both have a large number of properties that can be set, and both have many public and protected methods and events.

A unique characteristic of the AccessDataSource control is the DataFile property. The Connection String property does not have to be set. Just identify the location of the Access .mdb file, and the underlying connection to the database is made. A relative path to the database can be set. This makes deploying or moving the data-driven Web site easier. App_Data folder, which is created automatically when the Web site is originally designed, is reserved for storing

data files. This is the location where the .mdb file should be placed if it is to be stored with the Web application.

Using Visual Tools to Connect From the GridView objects smart tag, the Data Source Configuration Wizard can be launched from the Choose a Data Source option.

Modifying the Data By default, the GridView control displays data in a read-only format. To allow users to edit data, use the Advanced tab to configure the select statement from the Data Source Configuration tool. Selecting this option generates the additional Insert, Delete, and Update SQL statements needed. In order to use this feature, all primary key values must be retrieved as part of the select statement.

Other Controls More than 250 classes make up the System.Web.UI.WebControls namespace. Encourage students to review Figure 15-17. It shows the different Web Forms controls that can be added to a Web Forms page from the Toolbox using a drop-and-drag approach. A number of other Web Forms server controls are available. Explore the System.Web.UI.WebControls namespace to find others. Encourage students to explore the classes listed in Table 15-5.

Navigation Site navigation can be added to a Web site by defining a site map using one of the new navigation controls, such as the TreeView and SiteMapPath controls. These controls automatically create a menu or tree view of pages.

Data Explore the DetailsView and FormView controls.They are also used to display and edit data from different data sources.

Login New security controls that enable authentication of users are available. The Login control prompts users for credentials and validates them. The PasswordRecovery control helps users change or remember their password. There is also a LoginStatus control that enables a Login or Logout button to be presented.

Quick Quiz

  1. The ___________ property can be used with the Calendar control to set the month, day, and year for display. When the page is loaded, this property is initially set to the current date. Answer: SelectedDate
  2. True or False: When both the Text and the ErrorMessage properties are set to messages, the ErrorMessage property overrides the Text property. Answer: False

WSDL

The Web Service Definition Language (WSDL) describes the Web service. It includes details about the messages it can receive and the results it will return.

A number of Web services are already available that can be added to applications. UDDI stands for Universal Description, Discovery, and Integration. UDDI, like WSDL, uses an XML grammar to publish information about their location and functionality.

Windows Communication Foundation (WCF) WCF is an Application Programming Interface (API) used to build connected service-oriented applications.WCF can be used to build Web services.

SMART DEVICE APPLICATIONS (OPTIONAL)

There are many different platforms that a developer can choose for creating mobile applications. Windows Mobile OS is a compact operating system developed by Microsoft.You can create mobile applications for this operating system using Visual Studio. One of the installed templates with Visual Studio versions prior to Visual Studio 2010 was Smart Device applications. When Visual Studio 2010 was first released, it did not include support for smart device development. The Windows Phone Developer Tools–CTP were added as a separate install. It includes a Windows Phone IDE and the Windows Phone Emulator. The template for developing applications for smart devices targets Silverlight.

Silverlight Silverlight user interfaces are declared in Extensible Application Markup Language (XAML) and programmed using a subset of the .NET Framework. You have fewer controls to drag and drop and fewer events to program.

Creating a Smart Device Application Select File > New > Project > Visual C# > Silverlight for Windows Phone > Windows Phone Application. A blank miniature form is displayed with a main Menu control automatically included. Using the WYSIWYG approach, controls can be dragged and dropped on the form.

Each of the controls added are classes with properties that can be set. Two files are created. An .xaml file with tags similar to HTML tags holds presentation details. The second file, .xaml.cs, holds program statements like event-handler methods.

Building and Running the Smart Device Application Click Start Without Debugging on the Debug menu. An Emulator simulating a phone is displayed after a few minutes.

Quick Quiz

  1. A(n) _____________ is software that simulates a device such as a mobile phone and has many of the same features you find with the hardware device. Answer: emulator
  2. True or False: Visual Studio creates two files, an .xaml file and an .xaml.cs file, for Windows Communication Foundation (WCF) applications. Answer: False
  3. True or False: WCF is used to create Web service applications Answer: True
  4. Identify two things that are different from Web or Windows applications that should be taken into consideration when designing smart devices. Answer: Mobile devices are less powerful, run more slowly, and have less memory available, and their display capability restricts their functionality. The user interface should be kept simpler than traditional applications.

CODING STANDARDS

As was suggested with Windows application, prefix controls with a consistent identifier to aid in using IntelliSense during development.

Discussion Questions

Some interesting topics of discussion in this chapter include:

  • How does the Web application model differ from the Windows application model?
  • What type of Web services are currently available to include in your Web applications?
  • What additional development environments are available today to create C# applications? ASP.NET applications?

Projects to Assign

All of the Multiple Choice Exercises, Problems 1- Odd-numbered Short Answer Exercises, Problems 21- Programming Exercises, Problems 3, 5, 7, and 9

¾ server-side scripts : code that must be processed at the server level before the page is delivered ¾ Silverlight : Web application framework that enables multimedia, graphics, and animations to be integrated into an application. Used to create mobile apps for Windows Mobile OS ¾ Simple Object Access Protocol ( SOAP ): an XML-based protocol used to encapsulate method calls (and return results) into XML statements so they can be sent via HTTP ¾ SOAP request : SOAP message that includes a request for the service ¾ SOAP response : SOAP message that includes the return value from a soap request ¾ stateless : page does not retain its values from one trip to the Web server to the next ¾ static Web page : precreated pages that reside on the server’s hard drive, and basically are delivered as HTML documents that do not require any processing on the client computer or by a Web server ¾ universal resource locator ( URL ): Web address ¾ validation controls : controls that enable input to be validated or checked by the server ¾ VBScript : scripting language used to write code for the HTML document; subset of Microsoft Visual Basic 6. ¾ Web application : collection of one or more related files or components stored on a Web server; sometimes called Web sites ¾ Web controls : controls that can be added to Web pages that have more built-in features than the HTML controls; also referred to as server controls, Web forms server controls, ASP server controls, or Web forms controls ¾ Web forms server controls : controls that can be added to Web pages that have more built-in features than the HTML controls; also referred to as Web controls, server controls, ASP server controls, or Web forms controls ¾ Web server : software that hosts or delivers the Web application ¾ Web services : applications that return data; enable you to exchange data from one computer to another over the Internet or an intranet ¾ Web sites : collection of one or more related files or components stored on a Web server; sometimes called Web application ¾ Windows Communication Foundation (WCF) : An application programming interface for building connected, service-oriented applications ¾ WYSIWYG : “what you see is what you get” design approach based on dragging and dropping controls to the desired location