box is 540 pixels wide and floats to the left. The float forces other content to flow to its right, starting at the top.
199
200
Part III: Enhancing the Interface and User Experience You can use the ASP.NET Panel control to generate
199
200
Part III: Enhancing the Interface and User Experience You can use the ASP.NET Panel control to generate
tags for layout. Use class-based style rules — the ones that start with a dot (.) — and set the Panel control’s CssClass property to the style’s name.
Reducing Load Times and Improving Performance As Web pages become busier, performance starts to drag. If a page seems slow on your workstation, expect it to get worse when posted to the Web server. While you can’t always control the speed of the Internet connection or the Web server’s horsepower, you can certainly do your part to cut page load times and improve the overall throughput. This section looks at two techniques: reducing ViewState and employing caching.
Turning off ViewState ASP.NET server controls make programming easier, but they make Web pages fatter. If you’re not careful, ASP.NET can bloat a relatively small page by overdoing a hidden feature called ViewState. Look at the output of any ASP.NET page and you find ViewState. It resembles the following (much abbreviated) markup when you look at the HTML in the browser (View➪Source): ViewState is how ASP.NET tracks the current settings for all its controls. You see an ordinary, hidden tag with a string of nonsense characters as the value attribute. That value is the Web server’s personal aide-memoire as to what the page was like when it sent the HTML to the browser. That blob of overhead travels back and forth (it roundtrips in geekspeak) for no good reason. If there’s nothing in the server-side code that can change the text in a Label control or GridView, why track the ViewState? You can turn off ViewState for an individual control (for instance, a TextBox) by setting its EnableViewState property to False. If you’re confident that none of the controls on a page will change, you can turn off ViewState for the entire page. In Source view, at the top of the page, add the part that you see shown in bold: <%@ Page Language=”VB” EnableViewState=”false” %>
Chapter 12: Web Standards, Page Layout, and Usability Going to extremes, if you’re positive that you don’t need ViewState anywhere in the whole ASP.NET site, you can switch it off in the web.config file. Look for the element (somewhere within the element) and add the following part shown in bold: Even when you turn off ViewState for an entire site, ASP.NET doesn’t give up completely. Some remnants remain in the page — as if it’s using ViewState to track the fact that ViewState is off.
Caching “expensive” content Imagine poor old Internet Information Services grinding away to produce an ASP.NET page full of elaborate three-dimensional charts. The data for the charts comes from a SQL Server database that executes some mindnumbingly complicated queries. Eventually, the HTML, images, and associated JavaScript are assembled and shipped across the Internet to the browser. Congratulations on a job well done. In the next fraction of a second, someone else requests exactly the same page. The Web server starts all over again, building the shading and gradients on the charts and re-requesting the data. It’s a waste of time and, in geekspeak, expensive. To reduce the load on the server, you can store a copy of a complicated diagram, image, report, or page in the server’s memory. On the next identical request, the Web server looks for that content in the cache. If it finds something usable, it sends the cached copy.
Page-level caching When you identify a page that would benefit from caching, open the page in Source view and, at the top of the page, add the following directive: <%@ OutputCache Duration=”300” VaryByParam=”none” %> The preceding line of code gives these instructions to the Web server: Create this page as usual on the occasion that someone requests it. Hold the page in memory for 300 seconds (five minutes) and send your cached copy if anyone else asks for it. Yes, send the cached version even if the data in the database has changed. After five minutes, dump the cached version and start over with a new page if asked.
201
202
Part III: Enhancing the Interface and User Experience Caching is powerful, but it can drive you crazy if you’re not aware of it. Try this experiment to see just how strange things can get: 1. Drop a Label control and Button control on an ASP.NET page. 2. In Source view, configure the markup as follows: <% =Now.ToLongTimeString%>
The <% =Now.ToLongTimeString%> inserts the time as text for the Label control. 3. Run the page and click the button several times. The time updates on every button click, as you’d expect. 4. Add the following code to the very top of the ASP.NET page. <%@ OutputCache Duration=”300” VaryByParam=”none” %>
5. Run the page and click the button. The page updates as before. 6. Click again, several times. Time stands still for five minutes — at least according to the server. On the first click, the Web server updates the page and then caches the content. On subsequent clicks, it sends the cached version. If you’re having problems with a page that doesn’t update properly, check whether it’s using caching. It may be that you’re viewing a cached version.
Cache is not forever Although you can ask a Web server to cache pages, there’s no guarantee that the server keeps the cache for the set time — or caches it at all. When the Web server runs short of memory (because the cache is full or there are severe demands on its memory), it dumps items from the cache automatically. If your pages are running on a shared server, it’s possible that someone else’s pages gobble up the available cache and your performance deteriorates unexpectedly.
Meeting Accessibility Requirements Increasingly, Web sites created for government agencies must meet accessibility requirements. These standards ensure that the site is usable by people who have difficulty seeing, hearing, moving, or processing images. The goal is
Chapter 12: Web Standards, Page Layout, and Usability to generate online documents that specialized browsers, such as screen readers and braille displays, can handle easily. In the United States, people often refer to Section 508 of the Rehabilitation Act of 1973, more commonly known as Section 508. In 1999, the W3C produced its Web Content Accessibility Guidelines 1.0 (WCAG 1.0). You can read the document at www.w3.org/TR/WAI-WEBCONTENT/. Accessibility and usability have many issues in common. See the upcoming section, “Increasing a Page’s Usability,” for ways to make pages easier to use for all visitors.
Alternate text for images For the most part, ASP.NET server controls meet accessibility requirements, but a great deal depends on the page developer. For example, you wouldn’t want to create problems for blind — or colorblind — readers by instructing them to click a red or a blue image to continue. A key requirement for accessibility is a text equivalent for images and multimedia content. The ASP.NET Image, ImageButton, and ImageMap controls include an AlternateText property in which you can enter an explanation of the image. If your layout uses images for padding and fillers, you can set the GenerateEmptyAlternateText property to true so the controls generate empty strings (that is, quotes with nothing inside them). This way, accessibility checker software won’t report those images as missing alternate text, and specialized readers won’t consider them as unimportant.
Avoiding output as tables Some ASP.NET controls, such as the Menu control, use the HTML tag to create their structure. Accessibility guidelines frown on tables except for tabular data. Fortunately, many ASP.NET controls are templated, which means that you’re free to design their markup and replace undesirable tags with style sheet–friendly
Reducing Load Times and Improving Performance As Web pages become busier, performance starts to drag. If a page seems slow on your workstation, expect it to get worse when posted to the Web server. While you can’t always control the speed of the Internet connection or the Web server’s horsepower, you can certainly do your part to cut page load times and improve the overall throughput. This section looks at two techniques: reducing ViewState and employing caching.
Turning off ViewState ASP.NET server controls make programming easier, but they make Web pages fatter. If you’re not careful, ASP.NET can bloat a relatively small page by overdoing a hidden feature called ViewState. Look at the output of any ASP.NET page and you find ViewState. It resembles the following (much abbreviated) markup when you look at the HTML in the browser (View➪Source): ViewState is how ASP.NET tracks the current settings for all its controls. You see an ordinary, hidden tag with a string of nonsense characters as the value attribute. That value is the Web server’s personal aide-memoire as to what the page was like when it sent the HTML to the browser. That blob of overhead travels back and forth (it roundtrips in geekspeak) for no good reason. If there’s nothing in the server-side code that can change the text in a Label control or GridView, why track the ViewState? You can turn off ViewState for an individual control (for instance, a TextBox) by setting its EnableViewState property to False. If you’re confident that none of the controls on a page will change, you can turn off ViewState for the entire page. In Source view, at the top of the page, add the part that you see shown in bold: <%@ Page Language=”VB” EnableViewState=”false” %>
Chapter 12: Web Standards, Page Layout, and Usability Going to extremes, if you’re positive that you don’t need ViewState anywhere in the whole ASP.NET site, you can switch it off in the web.config file. Look for the
Caching “expensive” content Imagine poor old Internet Information Services grinding away to produce an ASP.NET page full of elaborate three-dimensional charts. The data for the charts comes from a SQL Server database that executes some mindnumbingly complicated queries. Eventually, the HTML, images, and associated JavaScript are assembled and shipped across the Internet to the browser. Congratulations on a job well done. In the next fraction of a second, someone else requests exactly the same page. The Web server starts all over again, building the shading and gradients on the charts and re-requesting the data. It’s a waste of time and, in geekspeak, expensive. To reduce the load on the server, you can store a copy of a complicated diagram, image, report, or page in the server’s memory. On the next identical request, the Web server looks for that content in the cache. If it finds something usable, it sends the cached copy.
Page-level caching When you identify a page that would benefit from caching, open the page in Source view and, at the top of the page, add the following directive: <%@ OutputCache Duration=”300” VaryByParam=”none” %> The preceding line of code gives these instructions to the Web server: Create this page as usual on the occasion that someone requests it. Hold the page in memory for 300 seconds (five minutes) and send your cached copy if anyone else asks for it. Yes, send the cached version even if the data in the database has changed. After five minutes, dump the cached version and start over with a new page if asked.
201
202
Part III: Enhancing the Interface and User Experience Caching is powerful, but it can drive you crazy if you’re not aware of it. Try this experiment to see just how strange things can get: 1. Drop a Label control and Button control on an ASP.NET page. 2. In Source view, configure the markup as follows:
The <% =Now.ToLongTimeString%> inserts the time as text for the Label control. 3. Run the page and click the button several times. The time updates on every button click, as you’d expect. 4. Add the following code to the very top of the ASP.NET page. <%@ OutputCache Duration=”300” VaryByParam=”none” %>
5. Run the page and click the button. The page updates as before. 6. Click again, several times. Time stands still for five minutes — at least according to the server. On the first click, the Web server updates the page and then caches the content. On subsequent clicks, it sends the cached version. If you’re having problems with a page that doesn’t update properly, check whether it’s using caching. It may be that you’re viewing a cached version.
Cache is not forever Although you can ask a Web server to cache pages, there’s no guarantee that the server keeps the cache for the set time — or caches it at all. When the Web server runs short of memory (because the cache is full or there are severe demands on its memory), it dumps items from the cache automatically. If your pages are running on a shared server, it’s possible that someone else’s pages gobble up the available cache and your performance deteriorates unexpectedly.
Meeting Accessibility Requirements Increasingly, Web sites created for government agencies must meet accessibility requirements. These standards ensure that the site is usable by people who have difficulty seeing, hearing, moving, or processing images. The goal is
Chapter 12: Web Standards, Page Layout, and Usability to generate online documents that specialized browsers, such as screen readers and braille displays, can handle easily. In the United States, people often refer to Section 508 of the Rehabilitation Act of 1973, more commonly known as Section 508. In 1999, the W3C produced its Web Content Accessibility Guidelines 1.0 (WCAG 1.0). You can read the document at www.w3.org/TR/WAI-WEBCONTENT/. Accessibility and usability have many issues in common. See the upcoming section, “Increasing a Page’s Usability,” for ways to make pages easier to use for all visitors.
Alternate text for images For the most part, ASP.NET server controls meet accessibility requirements, but a great deal depends on the page developer. For example, you wouldn’t want to create problems for blind — or colorblind — readers by instructing them to click a red or a blue image to continue. A key requirement for accessibility is a text equivalent for images and multimedia content. The ASP.NET Image, ImageButton, and ImageMap controls include an AlternateText property in which you can enter an explanation of the image. If your layout uses images for padding and fillers, you can set the GenerateEmptyAlternateText property to true so the controls generate empty strings (that is, quotes with nothing inside them). This way, accessibility checker software won’t report those images as missing alternate text, and specialized readers won’t consider them as unimportant.
Avoiding output as tables Some ASP.NET controls, such as the Menu control, use the HTML