Sams Teach Yourself ASP.NET in 21 Days, Second Edition
By Chris Payne
Table of Contents
Day 19.  Separating Code from Content

Today you learned about a few new methods to maintain the content of your pages separately from the code. With the Web forms framework, you can use code-behind forms, which separate code from content by placing them in separate files. You can use localization and resource files to display different content to your users without modifying your code.

To create a code-behind form, you need to create a class that derives from the System.Web.UI.Page class (or the System.Web.UI.UserControl class if the code-behind is for a user control). Move all ASP.NET code from the .aspx file into the new class, and declare public versions of the server controls used in your page. For example, if you have a label named lblMessage in your .aspx file, add the following to your code-behind:

 public lblMessage as Label 

This ensures that you are able to handle the properties and events generated by the server controls. Then add the Inherits and src attributes to your .aspx file, specifying the name of the code-behind class and the location of the file, respectively. Your code-behind and ASP.NET combination should work the same as it did prior to building the code-behind.

You can use the Request.UserLanguages property to retrieve a user's primary language from the browser. You can use this language to set culture information with the CultureInfo object. This object provides numerous methods to retrieve culture information such as date and time formats. The RegionInfo object is similar to CultureInfo, but provides information about a specified region or country, such as the currency symbol and country abbreviation.

Use the resgen.exe tool to generate .resources files from your resource text files. Be sure to name these latter files in the format: prefix.culture.txt. Create a new resource file for every language that you want your pages to be displayed in. These files store information in name/value pairs. You can then use the ResourceManager object to load these resource files and display the localized strings.

Tomorrow you'll look at another very important part of developing ASP.NET pages: debugging them. No one's perfect, so chances are that you've made mistakes when building your pages. Tomorrow's lesson will examine quite a few ways to track down and correct these errors.


    Sams Teach Yourself ASP. NET in 21 Days
    Sams Teach Yourself ASP.NET in 21 Days (2nd Edition)
    ISBN: 0672324458
    EAN: 2147483647
    Year: 2003
    Pages: 307
    Authors: Chris Payne

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: