Asp. New code compiling function in net (II.)

Source: Internet
Author: User
Asp.net| Compile Interest Calculator

First, we open Visual Studio. NET Whidbey and create a new Web site called compilation. After you create a Web site, the IDE should resemble figure 1.



Figure 1:visual Studio. NET Whidbey Web site

Then, we add the \code folder to the Web site, add the method to right-click the item, and then select New Folder (the new folders). This folder must be named Code, but the name is not case-sensitive. After you add a folder, we can add a new class file: Right-click the \code folder, clicking Add New Item ... (Add New Item ... , and then select the Class (Class) item in the Templates pane of the Add New Item dialog box. Name the class Calculateinterest.vb. Then add the code to calculate the interest (add it to the class and End Class statements):

 
   
    
  Public Function calcbalance (ByVal prncpl As Integer, _                        ByVal Rate as Double, _                        ByVal Years As Integer, _                        ByVal Period as Integer) as String    Dim basenum as Double = (1 + rate/period)    calcbalance = _        Format (prncpl * Syst Em. Math.pow (Basenum, _        (Years * Period)), "#,###,# #0.00"). Tostringend Function
 
   


After creating the component class, we need to modify the Default.aspx page to provide the fields to enter the data and to invoke the Calcbalance method of the component. For the sake of simplicity, the complete list of Default.aspx is shown below (note that Default.aspx uses the single file code model).

Default.aspx:

   
    
<%@ page language= "VB"%><script runat= "Server" > Sub button1_click (ByVal sender as Object, _ by Val e as System.EventArgs) Dim Calc as New calculateinterest label6.text = "$" & _ CALC.CALCB Alance (Convert.ToInt32 TextBox1.Text), _ (Convert.ToInt32 (TextBox2.Text)/MB), _ convert.t OInt32 (TextBox3.Text), _ Convert.ToInt16 (dropdownlist1.selectedvalue)) label6.visible = True End Sub</script>


In Design view, the modified default.aspx should resemble figure 2.



Figure 2: Default.aspx in Design view

Note that when you type code in the < script > block that is used to invoke the component class, you get the complete IntelliSense statement completion (including the Component Class), as shown in Figure 3. This is a major improvement on the basis of Visual Studio. NET 2003, and Visual Studio. NET 2003 does not support IntelliSense in server-side < script > blocks.



Figure 3: IntelliSense in the Source view

Browsing Default.aspx produces the output shown in Figure 4. Fill in the principal, interest rate, and number of years, then click Calculate (Calculated), and the output should look like Figure 5.



Figure 4:default.aspx's initial output



Figure 5: The computed output

Resource files If you have previously used Web applications in Visual Studio. NET 2002 or 2003, you must have noticed that each time you create a new Web Forms page, in addition to the. aspx page and the. vb or. cs modular file, visual Stud IO also creates a matching file (that is, WebForm1.aspx.resx) with a. resx extension. Like most WEB developers, you may also ignore or attempt to delete these files because their use and/or usage is not straightforward. In short, these. resx files are called resource files and are primarily used to store individual versions of resources, such as text strings for different languages used for localization.

In Visual Studio. NET 2002 and 2003, resource files need to be added to the project assembly as part of the build project process, and you need to import two namespaces, create a ResourceManager object, and call its GetString method to access the The source string. With the help of the \code directory, the resource access process in Visual Studio. NET Whidbey becomes very simple, as shown in the following example.

Let's start with creating a resource file, or using the project in the previous example. First, right-click the compilation Web site you just created, and then click Add New Item ... (Add New Item ... )。 In the Add New Item dialog box, select Assembly Resource file (assembly resource file) template, name the resource file Strings.resx, and then click Open. The default view of the Strings.resx file should resemble Figure 6.



Figure 6: Editing a resource file in the XML editor

Add the following items to the datasheet (you can leave the "comment" (note), type (type), and mimetype (MIME type) columns blank):

Name Value txtcolorprompt Please choose a color: Txtcolorresponsegreen you have chosen the green! Txtcolorresponseblue you have chosen blue! Txtcolorresponsered you have chosen red!

Now repeat the procedure above, add a new resource file named Strings.en-gb.resx, add the following items to its datasheet, and then save the file (because we did not add the txtcolorresponse* entry, all clients will use the Values for these items in the Strings.resx):

Name Value txtcolorprompt Please choose a color:

Now, to take advantage of the magic of the Code directory, we need to drag these two. resx files from the root directory of the Web site to the code directory. When you do this, you will get a result similar to Figure 7.



Figure 7:code. resx files in the directory

To illustrate how simple it is to use the created resource file now, we add a Web form to the project by right-clicking the Web site node and then clicking Add New Item .... (Add New Item ... )。 In the Add New Item dialog box, select the Web form, name the page colorpicker.aspx, and then click Open. Modify this page so that it matches the following list.

Colorpicker.aspx:

      
       
<%@ page uiculture= "EN-GB" language= "VB"%><script runat= "Server" > Sub Page_Load (ByVal sender as Objec T, ByVal e as System.EventArgs) Label1.Text = Resources.strings.txtColorPrompt End Sub Sub Submit_click (B Yval sender as Object, _ ByVal e as System.EventArgs) Label1.forecolor = _ System.drawing.color.f Romname (dropdownlist1.selectedvalue) Select Case dropdownlist1.selectedvalue case "Red" La Bel1. Text = Resources.strings.txtColorResponseRed case "green" Label1.Text = Resources.strings.txtColorR        Esponsegreen case "Blue" Label1.Text = Resources.strings.txtColorResponseBlue End Select Dropdownlist1.visible = False submit.visible = False End Sub</script>


When browsing colorpicker.aspx from a browser, the default output is similar to Figure 8. If you browse this page from a system set up for UK users (you can simulate this by setting the page's UICulture property to "EN-GB" and saving the page), the output is similar to Figure 9 (note that we have added u in "colour").



Figure 8:colorpicker.aspx Default output



Figure 9: Colorpicker.aspx output of the British system

Note that only one line of code is required to access a resource file in ASP.net Whidbey. Because the resource file is automatically embedded and referenced after it is placed in the Code directory, you do not need to reference any namespaces or assemblies, and you do not need to create objects for accessing resource strings. And ASP.net can also determine which resource file should be used (based on user browser settings), so we don't need to judge this at run time and respond accordingly. ASP.net can help us accomplish all this.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.