Other DataBound Controls
In addition to the simple bound controls, ASP.NET includes several more complex controls . They work very much like the simple bound controls in that you attach a data source to them and they render automatically. However, these controls differ by displaying the data in more elaborate ways . These controls include the GridView, the FormView, the DetailsView, and the DataList. The best way to understand the nature of these controls is to work through a couple of examples . First, the GridView....
ASPNET Login Controls
Earlier in this chapter, you handcrafted a couple of different login pages . During the heyday of ASP.NET 1 .1, that's exactly what you had to do to get Forms Authentication to work . Later versions of ASP.NET add a number of login controls that perform the most common login scenarios you might need for your site . These controls include the Login, LoginView, PasswordRecovery, LoginStatus, LoginName, ChangePassword, and CreateUserWizard controls . Here's a summary of what each control does...
Defining Custom Attributes for Each Node
Another way to extend your Web application's navigation is to define custom attributes for the site nodes in web . sitemap and retrieve them at run time. Imagine that you want to associate a specific image with each page in your site. To accomplish this, just create a new attribute and specify it in the siteMapNode element in the site map data. ASP.NET site map navigation support makes it very easy to add arbitrary attributes to each node. In this example, you add JPEG URLs to the site map...
W
WarningLevel attribute, 146 Watch window, 377 Wayback Machine, 514 WCF, 555 ASP.NET and, 560-561 behaviors, 558-559 channels, 558 compatibility mode, 561 elements of, 557-559 endpoints, 557-558 messages, 559 role of, 556-557 service contracts, 573 side-by-side mode, 560 Web sites enabled for, 573 WCF clients, building, 567-572, 573 WCF contracts, 558 Visual Studio-generated placeholders, 562 WCF proxies, Silverlight-enabled, 548 template, 562 WCF services calling asynchronously, 570-571...
The SiteMapPath Control
You might have seen user interface UI elements similar to the SiteMapPath control on other sites especially online forms that go several layers deep . The SiteMapPath control shows a trail indicating where the user is in the Web page hierarchy and shows a path back to the top node kind of like a trail of breadcrumbs . The SiteMapPath is most useful in sites that maintain a very deep hierarchy for which a Menu or a TreeView control would be overwhelmed. Although the SiteMapPath control is like...
What Good Are Web Parts
WebPart controls are useful for developing portal-type Web sites . Work flow and collaboration management is quickly becoming one of the most important application areas for Web site development . Because portals often have much of the same functionality from one to another, it makes more sense to build portals from a framework than to build them completely from scratch . Much of this functionality includes such items as file transfers, implementing user profiles, and user administration....
Using a DataSource to populate controls with a DataReader
1. Add a new form to DataBindORama named DataBindingWithDB. 2. The example for this chapter named DataBindORama , available on the accompanying CD, includes a SQL Server Express database named ASPNETStepByStep4.mdf. First create an App_Data folder by right-clicking the Project node in Solution Explorer and clicking Add, Add ASP.NET Folder, App_Data. Add the database to your project by right-clicking the App_Data node in the project and clicking Add, Existing Item. Locate the ASPNETStepByStep4 ....
Getting Started with Caching
ASP.NET's caching facility is extremely easy to use. In addition, it's also configurable in the sense that you can tell ASP.NET to apply expirations to cached items and you can set up callback methods so that your application knows when items have been removed from the cache . The following exercise shows how using the cache can benefit your application. Making an application that benefits from caching 1. Create a new project . Make it an Empty ASP.NET Web Application. Call it UseDataCaching....
A Fragmented Communications API
Each of the older technologies mentioned previously has its own specific advantages especially when you consider the periods in computing history in which they were introduced. However, having so many different means of writing distributed computing applications has led to a fragmented application programming interface API . Which technology to use has always been a decision that must be made early in development . Earlier distributed technologies often tied applications to a specific transport...
Using the Object Tag
The first way to add Silverlight content is by using the standard lt object gt tag the one that loaded ActiveX controls on pages written in the late 1990s . In fact, Visual Studio creates an HTML page for you that loads the Silverlight content using the lt object gt tag. lt -Run-time errors from Silverlight will be displayed here. -- gt lt -- This will contain debugging information and should be removed -- gt lt -- or hidden when debugging is completed -- gt lt div id 'errorLocation' sty1e...
User Controls
User controls are composite controls that contain child controls very much like binary composite controls do . However, instead of deriving from System.Web.UI.CompositeControl, they derive from System.Web.UI.UserControl. Perhaps a better description is that they're very much like miniature Web Forms . They have a UI component an .ascx file that works with the Visual Studio Designer, and they employ a matching class to manage the execution. However, unlike a Web Form, you can drag them onto the...
Using the Code Samples
Each chapter in this book explains when and how to use any code samples for that chapter. When it's time to use a code sample, the book lists the instructions for how to open the files . Many chapters begin projects completely from scratch so that you can understand the entire development process . Some examples borrow bits of code from previous examples . Here's a comprehensive list of the code sample projects HelloWorld. asp, Selectnoform.asp, Selectfeature .htm, Selectfeature2 .htm...
Validation
One of the primary goals of ASP.NET is to provide functionality to cover the most used scenarios . For example, later you will see that authorization and authentication are commonly required on Web sites . Most sites won't let visitors get to the real goodies until the visitors authenticate as valid users . ASP.NET now includes some login controls and an entire security infrastructure with which those controls work to make authorization and authentication easier Another scenario you often find...
Creating the Palindrome Checker control
1. To create the Palindrome Checker control, in Solution Explorer, right-click the CustomControlLib node, and click Add, New Item on the shortcut menu. Under Installed Templates, be sure the Web category is selected, and then highlight the ASP.NET Server Control node . Enter PalindromeCheckerRenderedControl in the Name text box, and click Add to generate the code . 2. Add a method to the PalindromeCheckerRenderedControl class to test for a palindrome . A palindrome is a word, sentence, or...
Part IV Diagnostics and Plumbing
17 Diagnostics and Page Trace Application Enabling Tracing The TraceFinished Piping Other Trace Debugging with Visual Error Unhandled Chapter 17 Quick 18 The HttpApplication Class and HTTP Modules 385 The Application A Rendezvous Overriding Global, asax vs . 19 HTTP ASP.NET Request The Built-in Handlers and Generic Handlers ASHX Part V Dynamic Data, XBAP, MVC, AJAX, and Silverlight 20 Dynamic Dynamic Data Dynamic Data 21 ASP.NET and WPF Improving Perceived Performance by Reducing Round-Trips...
The Pages Rendering Model
To get a good idea of how the ASP.NET Page model works, you can run the page again, but this time turn on the tracing mechanism. You examine tracing in more detail later when you look at the diagnostic features of ASP.NET. For now, you simply need to know that ASP.NET dumps the entire context of a request and a response if you set the page's Trace attribute to true. Here's the Page directive with tracing turned on lt Page Language C Trace true gt Figure 3-3 shows what the page looks like with...
The Wizard Control An Alternative to Session State
One of the most common uses for session state is to keep track of information coming from a user even though the information is posted back through several pages . For example, scenarios such as collecting mailing addresses, applying for security credentials, or purchasing something on a Web site introduce this issue. Sometimes the information to be gathered is minimal and can be done through only one page. However, when collecting data from users requires several pages of forms, you need to...
Site Map Configuration
The global configuration settings configure ASP.NET sites to use the default XmlSiteMapProvider. Listing 11-1 shows the configuration information that is part of the default web . config. Of course, as with all things configurable, you can swap in a different site map provider in your own site by modifying the web . config that goes along with your application LISTING 11-1 Default configuration for the site map data lt add siteMapFile web.sitemap System.Web, Version 4.0.0.0, Culture neutral,...
Using IIS to configure ASPNET
1. Before creating this new Web site, be sure to execute Visual Studio as an administrator that is, right-click Visual Studio on the Start menu and select Run As Administrator. This is necessary when creating or editing Web sites hosted directly by IIS. Create a new Web site called ConfigORamallS. .NET Framework 4 jJ Sort by Default 3 l l I ii I Search Installed Templat P Silverlight 1,0 Web Site Visual C j f Dynamic Data Linq to SQL Web Site Visual C jt Dynamic Data Entities Web Site Visual C...
ScriptManagerProxy Control
Scripts on a Web page often require a bit of special handling in terms of how the server renders them. Typically, the page uses a ScriptManager control to organize the scripts at the page level. Nested components such as content pages and user controls require the ScriptManagerProxy to manage script and service references to pages that already have a ScriptManager control This is most notable in the case of master pages . The master page typically houses the ScriptManager control. However,...
WPF Content and Web Applications
You can serve WPF content from an ASP.NET application in much the same way that ASP.NET serves other content . You can include loose XAML files in a Web application, or you can host some specific WPF content in an lt iframe gt HTML element.This exercise illustrates how you can use WPF content in an ASP.NET application. 1. Create a new Empty ASP.NET Web Application project in Visual Studio. Name the project XAMLORama. 2. Use Visual Studio to add a new text file to the project. Right-click the...
The HttpCachePolicy
The second way to manage the output cache is through the HttpCachePolicy, which is available from the Response class . Table 16-2 shows a portion of the HttpCachePolicy class . table 16-2 The HttpCachePolicy Class Appends specified text to the Cache-Control HTTP header Sets the Cache-Control HTTP header, which controls how documents are to be cached on the network Sets the ETag HTTP header to the specified string Sets the Expires HTTP header to an absolute date and time Sets the Last-Modified...
Personalization
After completing this chapter, you will be able to Use ASP.NET personalization. Apply personalization to a Web site . This chapter covers the built-in personalization features of ASP.NET. A major theme throughout ASP.NET is to provide frameworks and support for implementing features most Web sites need. For example, ASP.NET provides support for applying a common look and feel to a site through master pages and themes, as described in Chapter 7, A Consistent Look and Feel. Chapter 9, Logging In,...
Packaging the UI as Components
Being able to assemble the UI from component parts is one of the most-cited benefits of producing components . The earliest methods for building components in the Windows operating system were to write custom Windows procedures, to use the owner draw capabilities of controls such as list boxes or buttons, and to subclass an existing window. In the early 1990s, Windows employed Microsoft Visual Basic Controls VBXs as a viable UI technology. Of course, that was more than a decade ago . Throughout...
The IIS x Integrated Pipeline
The integrated IIS 7.x pipeline is very similar to the ASP.NET HTTP pipeline that's been around since ASP.NET was first released shown in Figure 2-5 . As you can see from earlier investigations using the IIS 7.x management console, the IIS 7.x integrated pipeline employs modules and handlers just like earlier versions of the ASP.NET HTTP pipeline did. However, whereas the ASP.NET HTTP pipeline runs entirely within the ASP.NET worker process, IIS 7.x runs the pipeline as directed by IIS. The...
Building a WCF Client
A WCF service is useless without any clients to employ it . This section illustrates how to build a client application that consumes the Quotes service. Here, you can see how Visual Studio makes it very easy to create a reference to a service . You see how to make WCF service calls both synchronously and asynchronously. 1. Start Visual Studio 2010. Open the WCFQuotesService solution and add a new Console Application project named ConsumeQuotesService to it . The following graphic illustrates...
G
Gamma, Erich, 451 GDI and GDI interfaces, 434, 436 Generic Handler template, 417, 419 GetAuthCookie method, 190 GetAverageLengthOfRequest method, 401 GetElementById, 551 get_isInAsyncPostBack method, 499 argument, 548-549 GetPropertyValue method, 260 GetRedirectUrl method, 190 GET requests, 4, 10-11, 35 GetResponse method, 6-7 GetVaryByCustomString method, 351 global application class, adding, 338 338, 388 Global.asax.cs files, 338, 387 global event handlers, 390 Global asax files, 387 vs HTTP...
The Application A Rendezvous Point
As you have seen so far, one of the most distinctive aspects of Web-based development is the requirement that you must be very mindful of the state of your application. By itself, raw Web application development includes no support for dealing with state . After all, Web requests are made over a disconnected protocol and much of the state of a request evaporates as soon as it hits an endpoint Chapter 4, Custom Rendered Controls, examines the notion of view state in an ASP.NET Web Forms...
WebConfig
Machine . config lays out the default settings for your computer and ultimately for your applications . The default settings are generally targeted toward the most common use cases you will encounter rather than some special configuration you might need to apply to a specific application . For example, sessionState is configured to be handled in process by default . That's fine when you're developing but almost certainly is not appropriate for a commercial-grade application that services many...
MultiView
From time to time, it's useful to gather controls together in several panes and give the user the opportunity to page through the panes . During the lifetime of ASP.NET 1 . 0, Microsoft released several rich dynamic though officially unsupported controls that emitted dynamic HTML DHTML instead of regular HTML . A trio of these controls TabStrip, MultiView an older version , and PageView worked together to form essentially a set of tabbed panes . These exact controls aren't available in later...
Updating Progress
A recurring theme when programming any UI environment is keeping the user updated about the progress of a long-running operation. If you're programming Windows Forms, you can use the BackgroundWorker component and show progress updating using the Progress control. Programming for the Web requires a slightly different strategy. ASP.NET AJAX support includes a component for this the ASP.NET AJAX UpdateProgress control. UpdateProgress controls display during asynchronous postbacks . All...
GridView Control
Whereas ASP.NET 1 .x supported only the DataGrid control, later versions of ASP.NET support a DataGrid on steroids the GridView. The GridView control is what it says it is It renders collections in a grid with individual columns and rows . Each row in the grid represents an individual record in a collection. Each column in a row represents an individual field in the record. Moreover, the original DataGrid required you as a developer to manage paging and sorting of data. The GridView control, on...
Routed Events
Silverlight control events look much like typical .NET events . The event handlers all use an object as the first parameter always the sender . Following the sender parameter, the handlers include some permutation of the standard event arguments . To manage events Silverlight uses routed events, which are like typical .NET events in that handlers for routed events can be attached directly to the controls that produce them for example, the Button control exposes a Click event . However, routed...
Global asax vs HttpModules
The application object expressed through Global, asax and the one offered through HTTP modules offer a rendezvous point for your application . You can use both of them to store global state between requests as well as to respond to application-wide events . When choosing one over the other, remember that Global.asax really goes with your application Global.asax is intended to manage state and events specific to your application . HTTP modules exist as completely separate assemblies they're not...
Layout Considerations
You might have noticed when building the last page that the layout of the page flowed. That is, every time you dropped a control onto the page, the Designer forced it up against the placement of the previous control. If you've worked with earlier versions of Visual Studio, you know that this is different default behavior. Visual Studio 2003 uses absolute positioning for elements on a page which is what you're used to if you've done rich client or standard Windows development . Although today...
ImageBased Controls
Graphic images are often used in Web pages . HTML includes an image tag that tells the browser to fetch an image file for example, a . gif, jpg, or .png file and display it . When you need to set an image on a page, the HTML lt img gt tag is the one to use . Naturally, ASP.NET wraps the lt img gt tag using a server-side control the Image control. Using the Image control is fairly straightforward. You select it in the Toolbox like any other control and drop it on the page. The ASP.NET Image...
A Trip Through the ASPNET Architecture
When it arrives on the Web server, the HTTP request response is routed through many server-side objects for processing. Once a request ends up at the server, it winds its way through the IIS ASP.NET pipeline . The best way to understand the path of an HTTP request through ASP.NET is to follow a request as it originates in the browser and is intercepted by Internet Information Services and your Web application. When an end user presses the Return key after typing in a URL, the browser sends an...
Modifying the OutputCache Directive
It's often very useful to be able to govern output caching. For example, some pages present exactly the same content to all the users who access the page . In that case, caching a single version of the content is just fine. However, there are other circumstances in which sending the same content to everyone is inappropriate . The easiest way to control the behavior of output caching is to modify the OutputCache directive . One obvious case in which controlling the cache is important is while...
Trapping the SiteMapResolve Event
ASP.NET is full of extensibility points . They're all over the place and the navigation architecture is no exception. ASP.NET site map support includes an application-wide event that informs listeners usually the application object whenever the end user is navigating through the Web site using a control connected to the site map data. Here's an example that shows how to handle that event . 1. You can add the SiteMapResolve handler anywhere in the project . In this example, you add it to the...
The Web Parts Architecture
The Web Parts architecture serves multiple purposes . Given that the job of Web Parts is to behave as a bigger UI lever, the functional components have been broken down into overall page management and zone management. WebPart controls need to be coordinated together In addition, the different functional areas of a page often need to be handled as a group of controls for managing layout, for example . In terms of framework classes, Web Parts are nested in zones, which are managed by a singular...
TreeView
One of the most common user interface conventions in modern software is a hierarchy represented by expandable nodes . For example, whenever you browse for a file using Windows Explorer, you need to expand and collapse various folders subdirectories to see what's inside . This type of control is generically known as a tree control. With tree controls, users can navigate hierarchies represented by expandable and collapsible nodes . For example, when you explore your C drive using Windows Explorer...
Running the Forms Authentication example
1. To run the Forms Authentication example, create an IIS application to hold the site. This example is intended to be simple ASP.NET requires some target and version info so this site is configured for ASP.NET version 2 .0. Right-click the directory in the IIS Connections window, and click Manage Application, Advanced Settings . Select A Classic Mode Application Pool for the application pool property. Add an HTML file to the directory that simply displays a banner text of Hello World. Name the...
Writing a WCF Service
Here's an example of a WCF service to help illustrate how WCF works . Recall the XML Web Service example application from Chapter 23, the QuoteService that doled out pithy quotes to any client wishing to invoke the service . The example here represents the same service but using a WCF-based Web site instead of an ASMX-based Web service. This way, you can see what it takes to write a WCF-based service and client, and you can see some of the differences between WCF-based services and ASMX-based...
Mixing HTML with Executable Code
Classic ASP had an interesting way of marking code segments within a page . ASP always supported the classic script tags lt script gt lt script gt where anything found between the lt script gt tags was treated as executable code . However, in classic ASP, the script blocks were sent to the browser, and it became the browser's job to run the script . In addition to clientside script blocks, a classic ASP Web page could define script blocks to be interpreted on the server. These methods often...
Configuration Section Handlers
At the top of machine, config you can see a number of configuration section handlers . Each handler understands a specific vocabulary for configuring .NET and ultimately ASP.NET . Whereas machine, config controls the settings for the entire computer, ASP.NET applications rely on files named web . config to manage configuration. You see much more about web . config shortly. However, for now here is an example of what you might find in a web . config file for a specific application lt xm1 version...
Building Navigable Web Sites
Adding navigation support to a Web site is pretty straightforward. Once you establish the hierarchical layout of the site, use the site map XML file to describe the structure. When that's done, just point any navigation controls you put on the page to the new XML site map file. The navigation controls will then populate themselves and render a navigable Web site . The following example shows how to add navigation support to a Web site and use the ASP.NET navigation controls in the application...
The ASPNET HTTP Pipeline
As soon as ASP.NET 1. 0 was released, it offered a huge improvement over classic ASP by introducing well-defined code processing modules that together form the ASP.NET HTTP pipeline. Classic ASP was patched together from several disparate components IIS, the Web Application Manager, and the ASP ISAPI DLL . The Request and Response objects were COM objects hanging off the threads owned by IIS . If you wanted to do any processing outside the context of ASP, you needed to write an ISAPI filter. If...
ASPNET MVC vs Web Forms
The ASP.NET MVC framework is fundamentally different from ASP.NET Web Forms . MVC makes a very clear distinction between data sources, program interaction with the data, and the presentation of data. MVC enforces a separation of concerns, whereas that sort of separation is something you'd have to build into an ASP.NET Web Forms application explicitly. MVC eschews some of the other features of standard Web Forms . For example, ASP.NET MVC does not support view state directly that feature really...
WebBased Security
Software security is a prominent topic these days, especially with ever increasing public awareness of security issues such as privacy. When a Web application runs in the Microsoft environment, several security issues arise immediately 1 the security context of Internet Information Services IIS , 2 being sure your clients are who they say they are, and 3 specifying what those clients may and may not do with your application . Managing Web-based security is similar to managing typical network...
Unhandled Exceptions
In the last example page that threw an exception, ASP.NET responded by redirecting to the default error page . In ASP.NET, you also can trap exceptions by setting up a handler for Error events fired by HttpApplication so that you can handle them more appropriately. The easiest way to accomplish this is to define a handler in your HttpApplication-derived class in Global.asax. cs . With the handler connected to the event, your application will receive notifications whenever something bad happens,...



















