Answers for Day 19


Sams Teach Yourself ASP.NET in 21 Days, Second Edition
By Chris Payne
Table of Contents
Appendix A.  Answers to Quiz Questions



True or false: A code-behind file must derive from the System.Web.UI.Page class.


True, unless the code-behind is for a user control, in which case it should derive from the System.Web.UI.UserControl class.


Why must you declare public variables in a code-behind for the server controls you use on an ASP.NET page?


A code-behind file must programmatically control server controls that are located on a separate page; normally, this would be impossible. However, if you declare the controls in the code-behind and provide event handlers for them, ASP.NET can provide implementations for those controls. The ASP.NET page can use the controls declared in the code-behind because it derives from the code-behind. A code-behind declares and provides event handlers for server controls, whereas the ASP.NET page simply displays them.


Create an @ Page directive that uses a code-behind class named MyControl, in a .vb file with the same name.


 <%@ Page Inherits="MyControl" src="/books/4/226/1/html/2/MyControl.vb" %> 

What is a resource file?


It is a file that contains localizable values for an ASP.NET application.


What does the following line do?

 dim strVar as string = Request.UserLanguages(0) 

It retrieves the primary language of a user as indicated by her browser, and stores it in a string variable named strVar.


What does the resgen.exe tool do?


It converts your text resource files into .resources files, which can be read by ASP.NET's resource manager.


What is the difference between CurrentCulture and CurrentUICulture?


Both represent the culture used by an application, but the latter is used by ASP.NET to load resource files. The former is used by localizable objects, such as DateTime.


True or false: The CultureInfo.CurrentCulture is a shortcut for the System.Threading.CurrentThread.CurrentCulture object.


True. The former is read-only, whereas the latter can be used to set culture information.



Create a currency exchange calculator for several different countries. Allow the user to enter a monetary value in one region's units, and convert to another by selecting a value from a select box. Use a resource file to store the exchange values. (Don't worry if you don't know the exchange rates, just make something up!) Use code-behind techniques!


This solution uses en-US, fr, de, and jp for the culture encodings. The resource files contain the conversion units for each culture. The file exchange.en-US.txt is as follows:

 Toen-US=1 Tofr=.7 Tode=1.67 Tojp=1098.45 

The file is as follows:

 Toen-US=1.43 Tofr=1 Tode=2.34 Tojp=1569.21 

The file is as follows:

 Toen-US=.0009 Tofr=.00064 Tode=.0015 Tojp=1 

The file is as follows:

 Toen-US=.59 Tofr=.42 Tode=1 Tojp=657.7 

Don't forget to use resgen.exe to place these files in .resource files.

The user interface, exercise1.aspx, is as follows:

 1:    <%@ Page Inherits="ExchangeCalculator" src="/books/4/226/1/html/2/exercise1.vb" %> 2: 3:    <html><body> 4:       <font size="5">Currency Calculator</font><hr> 5:       <form runat="server"> 6:          <asp:Label  runat="server"/> 7:          <asp:TextBox  runat="server"/> to 8:          <asp:ListBox  runat="server" 9:             Size=1 10:             AutoPostBack=true> 11:             <asp:ListItem>en-US</asp:ListItem> 12:             <asp:ListItem>fr-FR</asp:ListItem> 13:             <asp:ListItem>de-ge</asp:ListItem> 14:             <asp:ListItem>jp-ja</asp:ListItem> 15:          </asp:ListBox> 16:       </form> 17:       <asp:Label  runat="server"/> 18:    </body></html> 

The code-behind, exercise1.vb, uses the conversions in the resource files to convert monetary values. It needs to retrieve the currency symbols, so it converts from culture to region by using a case statement; the user selects a culture, which is transferred to a region, which is used to retrieve the currency symbol. The code is as follows:

 1:    Imports System 2:    Imports System.Web 3:    Imports System.Web.UI 4:    Imports System.Web.UI.WebControls 5:    Imports System.Globalization 6:    Imports System.Resources 7:    Imports System.Threading 8: 9:    Public Class ExchangeCalculator : Inherits Page 10:       public lblAnswer as Label 11:       public lblCurrency as Label 12:       public tbValue as TextBox 13:       public lbRegion as ListBox 14: 15:       sub Page_Load(Sender as Object, e as EventArgs) 16:          dim objRM as ResourceManager 17:          dim strLanguage as string = Request.UserLanguages(0). ToString 18: 19:          dim objCulture as new CultureInfo(strLanguage) 20:          Thread.CurrentThread.CurrentCulture = new CultureInfo(strLanguage) 21:          Thread.CurrentThread.CurrentUICulture = new CultureInfo(strLanguage) 22: 23:          dim objRegion as new RegionInfo(GetRegionFromCulture (objCulture.Name)) 24:          lblCurrency.Text = objRegion.CurrencySymbol.ToString 25: 26:          if Page.IsPostBack then 27:             objRM = ResourceManager. CreateFileBasedResourceManager("exchange", _ 28:                Server.MapPath("."), Nothing) 29:             objRegion = new RegionInfo(GetRegionFromCulture (lbRegion.SelectedItem. graphics/ccc.gifText)) 30:             dim dblAnswer as double 31:             dblAnswer = Ctype(tbValue.Text, Double) * CType(objRM.GetString("To" &  graphics/ccc.giflbRegion.SelectedItem.Text), Double) 32:             lblAnswer.Text = objRegion.CurrencySymbol. ToString & dblAnswer.ToString 33: 34:             objRM.ReleaseAllResources 35:          end if 36:       end sub 37: 38:       private function GetRegionFromCulture (strCulture as string) as string 39:          select case strCulture 40:             case "en-US" 41:                Return "us" 42:             case "ja-ja" 43:                Return "jp" 44:             case "de-ge" 45:                Return "de" 46:             case "fr-FR" 47:                Return "fr" 48:          end select 49:       end function 50:    End Class 


    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 © 2008-2017.
    If you may any questions please contact us: