Go to ASP.NET from Visual Basic

xiaoxiao2021-03-06  49

Http://www.microsoft.com/china/msdn/archives/library/dnaspp/html/ASPNET-MOVINGVBTOASPNET.ASP

Go to ASP.NET from Visual Basic

Scott Mitchell

August 2003

Suitable for: Microsoft® ASP.NETMICROSOFT® Visual Basic®

Summary: Understand the same point and difference between using the ASP.NET development web application and traditional desktop applications. This article applies to Visual Basic 6.0 developers who created an ASP.NET web application, which introduces the difference between Creating a desktop application with Visual Basic 6.0 and creating an ASP.NET web application using Visual Basic .NET. (This article contains some links to English sites.)

Download AspnetCrashcourse.msi. (Note that in the sample file, the programmer's annotation is used in English. In this article, it is translated into Chinese to facilitate the reader's understanding.)

table of Contents

Introduction Prepare Preparation Before using ASP.NET Creating the first ASP.NET web page Web application and desktop applications send a web form in another web form summary

Introduction

Before .Net is born, create a traditional ASP web application with a lot of significant and fundamental differences with Microsoft Visual Basic® 6.0 Creating Desktop Microsoft® WINDOWS® applications. For beginners, the Visual Basic application user interface is created by dragging and dropping the GUI tool in Microsoft Visual Studio®, and creates an ASP web application user interface, you need to manually add an HTML tag to generate the corresponding interface. In addition, the ASP web page contains script code that mixed HTML tags and source code, while Visual Basic applications handle classes, modules, and other structured, object-oriented programming configurations.

After .Net is born, traditional ASP evolves into ASP.NET. ASP.NET's various features make the ASP.NET web page is very similar to creating a single form in the Visual Basic application. Although it is very similar, there is some fundamental difference between the creation of web applications and creating desktop applications due to differences in transfer media.

This article first introduces preparations before starting to create an ASP.NET web application (developers who have started using ASP.NET can choose to skip this part). Then describe the ASP.NET web applications that make the ASP.NET web application with the creation of a Windows desktop application. This article also provides some excellent examples of using Visual Studio .NET to create an ASP.NET Web page to illustrate a surprising similarity between the creation of web applications and creating desktop applications. This also explains the fundamental difference between creating a web application and creating desktop applications. Finally, this paper discusses a common Visual Basic Task - transmitting information between forms and explains how this feature is implemented in the ASP.NET web application.

Preparation before using ASP.NET

Before starting to create an ASP.NET web page, you first need a computer that supports the ASP.NET web page. The computer needs to run Microsoft Windows 2000, Microsoft Windows XP Professional or Microsoft Windows ServerTM 2003. If there is no such computer, you can always use a company that provides a Web host service. For information on web host companies that support ASP.NET, please visit ISPS with ASP.NET Support in ASP.NET Forums. If you choose to provide an ASP.NET web page with your own computer, you need to install the following two software components:

Microsoft Internet Information Services (IIS) 5.0 or later .NET Framework

IIS is Microsoft's web server, free of charge with Windows server versions. IIS 6.0 is provided with Windows Server 2003, and IIS 5.0 is provided with Windows 2000 and Windows XP Professional. To determine if IIS is installed on your Windows 2000, Windows XP Professional, or Windows 2003 Server machine, open Control Panel, go to Add / Remove Programs, and click Add / Remove Windows Components button. The dialog shown in Figure 1 will be displayed. If you see the Internet Information Services checkbox, IIS is installed; otherwise, select the check box and click the "Next" button to install IIS.

Figure 1: View the Windows Components dialog to make sure IIS is installed

For more information on installing IIS, see the following official guides in Microsoft.com:

Installing IIS on Windows 2003 Installing IIS on Windows 2000 on Windows 2000 IIS on WINDOWS 2000

In addition to installing the IIS Web server, you must also install .NET Framework Redistributable on the web server. When writing this article, the .NET Framework version is 1.1, you can download it from Microsoft .NET Framework Version 1.1 Redistributable Package. The .NET Framework contains the class framework required to execute the .NET application on the web server. Since the ASP.NET web application is a .NET application and utilizing many classes in the .NET Framework, you must install the .NET Framework re-distribute package on the web server.

Note: Although the Web server and the .NET Framework installation are not divided, it is recommended that you install the web server first. This is because it is subsequently installed .NET Framework, updating the configuration settings for the web server so that the web server can support the ASP.NET web page. If you first install .NET Framework, the web server will not support the ASP.NET web page. But you can easily resolve this issue after running the ASPNET_REGIIS.EXE file. For more information, please visit

4Guysfromrolla.com: ASP FAQS: ASP.NET.

Create the first ASP.NET web page

Although the ASP.NET web page is like Visual Basic applications, you can create through any text editor, but tools such as Visual Studio .NET can greatly simplify the creation process. To develop an ASP.NET web page, you should install the latest version of Visual Studio .NET on your computer for developing an ASP.NET Web page. Although there are other development editors suitable for ASP.NET, such as ASP.NET Web Matrix, Visual Studio .NET provides an extremely similar design interface with Visual Basic 6.0. Below, we will create a simple ASP.NET Web page to reveal how much the ASP.NET application is to create a traditional Windows desktop application. First, start Visual Studio .NET and create a new Visual Basic ASP.NET web application project. Then, in the Location drop-down list, specify the URL of the ASP.NET application. If you want to provide an ASP.NET web page on the same computer used to develop, set the URL to http: // localhost / and select a project name, then enter http: // localhost / projectname. If you place an ASP.NET web application on a remote web server, enter the URL or IP address of the web server and the project name, such as http: // WebserveRurn / ProjectName. Figure 2 shows the screen snapshot of the Visual Studio dialog box, which will create a new Visual Basic ASP.NET web application located at http: // localhost / aspnetcrashcourse.

Figure 2: Creating a new Visual Basic ASP.NET web application

After clicking the OK button, Visual Studio .NET will create this new web application. This will include creation of directories specified in the Location drop-down list, add certain files, and updating the configuration of the web server to indicate that the newly created directory should be an IIS application.

The ASP.NET web application consists of some ASP.NET Web pages. Each web page is different files with file extensions .aspx. The relationship between the ASP.NET web page and the ASP.NET web application is the same as the relationship between a single form and the Visual Basic 6.0 application. That is, the ASP.NET Web page provides an interface for interacting with the web accessor. Like the Visual Basic form, each ASP.NET web page contains two different parts:

Graphical User Interface Class, its code response events thrown by controls in the user interface

The ASP.NET web page is often referred to as "web form". Controls in the user interface that can be added to the ASP.NET web page are called "Web Controls".

When you create a new ASP.NET web application, you will create a default web form WebForm1.aspx. Figure 3 shows a snapshot of the Visual Studio .NET screen after creating a new ASP.NET web application project.

Figure 3: Visual Studio .NET after creating a new ASP.NET web application project

The following detailed description of Figure 3. Note that the designer of WebForm1.aspx is currently displayed in the screen. The designer provides a WysiwyG view of the ASP.NET web page, which shows the content that users who have access to the web page will see. To add a web control to the ASP.NET web page, just drag and drop these controls from the Toolbox (Toolbox) to the designer. Since the ASP.NET Web page is a web page, there is a HTML tag in addition to the web control. To view the HTML tag of the ASP.NET web page, simply click the HTML button, which is next to the design (Design) button in the lower left corner of the designer. Therefore, you can process the user interface of the ASP.NET web page through the Wysiwyg designer, or you can directly modify the HTML of the web page via the HTML view.

To familiarize with Visual Studio .NET and better understand the process of creating an ASP.NET web application, let's add some web controls to WebForm1.aspx. Specifically, we will create an ASP.NET web page for simple financial calculations for calculating users' investment income in a given initial principal, interest rate, and investment period.

To do this, we need to add some tags and text boxes (TextBox) web controls, and a Button web control. The label web control is the same as in Visual Basic 6, which is used to display static or dynamic text content. To add a label web control, just drag and drop it from the Toolbox (Try). Then, click the Label Web control to load its properties into the Properties pane. In this pane, you can set its text attribute to the text value to display, set its ID attribute to a unique value. You can add a text box web control to the designer with the same method.

Figure 4 shows the designer screen snapshot after adding all the required tags and text box web controls. Note that each user input to be collected has a text box web control. When you add these web controls, you should set the ID attribute of each web control to a meaningful value. As in Visual Basic, the ID attribute is used as a unique identifier, and in the source code of the ASP.NET web page, it is also programmed to reference the web control. In this exercise, we set the ID of these three text box Web controls to "StartValue", "Rate", and "Duration".

Figure 4: Designer after adding the corresponding label and text box web control

In addition to the tags and text box web controls shown in Figure 4, we also need to add two web controls. The first is a label web control for displaying financial calculations. Put this tab web control near the bottom of the page, clear its text attribute and set its ID attribute to "Results". Finally, we still need a button web control. This web control will display a button that you want to click after entering the data. Set this button Web Control to "Display Future Investment Value". After adding these two web controls, your screen will be shown in the screenshot of Figure 5.

Figure 5: Designer after adding the last two web controls

At this point, we have completed the design of the user interface, and then we will start writing code. Specifically, it is to perform financial calculations when the user clicks the button web control and display the result in the "RESULTS" tab. As in the Visual Basic 6 form, you only need to double-click the button in the designer to add an event handler for the Click event of the button Web control. After creating an event handler for the Click event for the button web control, you will go directly to the source code section of the ASP.NET web page, which shows a new event handler (as shown in Figure 6). The source code part of the ASP.NET web page resides in a separate file called WebForm1.aspx.vb.

Figure 6: Click Event Handle Automatically added to the source code section of the ASP.NET web page

The rest of the job is to write code to calculate the future investment value and display this value in the "Results" tab. The complete code of the Click event handler is shown below. This code is very simple for Visual Basic developers.

Private sub button1_click (Byval Sender As System.Object, ByVal E AS

System.eventargs Handles Button1.click

'Computing the future value of investment, it is

'This gold and interest after a certain amount of time

'Formula: Value = Principal * E ^ (Rate / 12 * Duration)

DIM Value As Double

DIM BEGINNINGWITH AS DOUBLE = StartValue.text

DIM InterestRate as double = rate.text

DIM InvestmentTime As Double = DURATION.TEXT

Value = beginningwith * math.exp ((InterestRate / 100) *

InvestmentTime / 12)

'Specify the result value into the "Results" tab

Results.text = "The Investment, After" & InvestmentTime & "

Months, "& _

Will BE Worth: "& Formatcurrency (Value)

End Sub

To test this ASP.NET web page, just go to the debug menu and select Start (or press F5). This will compile the source code for the ASP.NET web page, while starting a web browser loading the specified start page (WebForm1.aspx is the default start page). You will see the browser window similar to Figure 7. If you enter a value in the text box and then click the button, enter a result as a result, as shown in Figure 8.

Figure 7: WebForm1.aspx displayed during the first visit

Figure 8: Webform1.aspx after entering information and click the "Display Future Investment Value" button

Important differences between web applications and desktop applications

Although there are many similarities between creating web applications and creating desktop applications, Visual Basic developers can quickly become familiar with the creation of ASP.NET applications, but there are still some subtle but important differences. The most fundamental difference between the web application and the desktop application is the medium used to communicate.

The desktop application runs directly on the user's computer as a stand-alone entity. There is no physical separation between the user interface of the application and the processing of the user input. In addition, the status of the application is independent. That is, if the user needs to fill in a number of forms containing data, each form can easily know and access values ​​in previous forms. For web applications, the above rules are not applicable. In a web application, the source code for handling the user is resident on the web server, while the user interface is sent to the user computer and displayed in a web browser in the user's computer. Therefore, there is no direct connection between the user interface and the code of the web application.

Take the above financial calculator application as an example, when the web accesser requests the ASP.NET web page, the ASP.NET web page returns an HTML tag based on the user interface of this page and the code executed in the source code section of this page. The user's Web browser presented the received HTML tag as a graphical user interface on the user's computer. At this point, there is no direct connection between the ASP.NET web page that generates this HTML tag and the user's web browser.

The user enters the data into each text box on their own computer, and then click the "Display Future Investment Value" button. After clicking this button, the user's web browser will send back a request to the same ASP.NET web page, send the value entered in each text box and other information. Through this information, the ASP.NET web page can determine that the user clicks this button to raise the Click event of the Button web control and perform the Click event handler. As mentioned above, this Click event handler calculates the final investment value and displays the value in the "RESULTS" tab. Subsequently, the new display will return to the user's browser in the form of an HTML tag, and the user will see the previously displayed web page, but the future investment value is displayed near the bottom of the page.

Note that Internet itself limits the functionality of web applications to a certain extent. For example, suppose to design a application prompting the user to enter a application such as postal coding. If you use Visual Basic, you only need to add a text box control and capture each key operation that is performed on this control. If, for example, the user enters a non-numeric key, or may not accept the input, and some feedback mechanism can be provided to notify the user to enter an invalid American postal coding character. However, for web applications, this feature cannot be implemented through the source code portion of the ASP.NET web page. This is because the source code section of the ASP.NET web page is only executed when the user's browser requests the page. This only occurs when the page is first loaded or submits this page by clicking the button or other mechanism.

Send a value in a web form to another Web Form

A common task in the Visual Basic application is to send values ​​in a form to another form. For example, an employee database application may list the company's employee and all employees' public information in a list of drop-down lists of a form. In this form, the user can select a particular employee, and then click the "Details" button to display the second form, which shows the name, address, and pay information of the selected employee. Since the Visual Basic desktop application has connectivity and status, you can use a public module to save all the forms of forms, or the main form can provide the selected value in the drop-down list to the detailed information form.

Since Internet is not connected and stateless, the web form is only executed when the form is requested from a web browser. Therefore, to enable a web form to load another Web form, you must indicate the URL of the user's browser to load the second web form in some way. You can pass values ​​from a web form to another by querystring. In discussing how to pass the value between the web form through QueryString, let's first understand the response.redirect () method, which is used to indicate the URL request specified by the web browser. With this method, a web form can load another web form. To illustrate the specific application of the response.redirect () method, we create an ASP.NET web page containing a single button web control. When you click this button, the user will be turned to another web form. First create the first web form (ie, a form containing the button), named First.ASPX. To create a new web form, right-click the project name ASPNETCRASHCOURSE, then select Add / Add New Web Form (Add / Add New Web Form). For this web form, just add a button web control, set its text attribute value to "Click ME". Figure 9 shows a snapshot of the Visual Studio .NET screen photo that creates this ASP.NET web page and adds a button web control.

Figure 9: Add a button web control in the first.aspx web form

Next, double-click the button web control in the designer to add an event handler for the Click event of the button. Add the following code line in this event handler:

Response.Redirect ("Second.aspx")

As you can see, the response.redirect () method will get a string parameter, which is used to redirect the URL of the user browser. Now we create a web form called Second.aspx, which contains only a tab Web control, which is "Welcome to the Second Web Form".

Now, to start testing the first.aspx web page, you need to set the first.aspx to Start Page. To do this, right-click First.aspx in Solution Explorer and select SET AS START PAGE option. Then, go to Debug and select the START option, which will compile the source code section of the ASP.NET web page and start the web browser to access First.aspx. A button will be displayed in this point in the browser. Clicking this button will re-request First.aspx again. This request will cause the source code section of First.aspx, while initiateting the button's Click event and performs an event handler. This will raise the response.redirect () method, which will send a message to the user's web browser, indicating the browser request Second.aspx. Subsequently, the web browser will request Second.aspX and display the final output result "Welcome to the Second Web Form".

At this point, we have learned how to load other web forms in a user browser using response.Redirect (). A set of simple scales values ​​(such as strings, numbers, and dates) can be passed from a web form to another via queryString. QueryString is an additional text attached to the URL of the web form, which contains a list of variable names and its values ​​in the following format: variablename1 = value1 & variablename2 = value2 & ... & variablenamen = Valuen

Note that each variable name and value pair are separated by the & symbol, and each variable name is separated from its value by the equal sign. The URL containing additional queryString is as follows:

http://www.somesite.com/somepage.aspx?somename =somevalue&some =Some==

SomeoTherValue

Below, we will modify First.aspx so that in addition to the button web control, we also contain a text box web control for users to enter strings. Then we will modify the response.redirect () method call in this button Web Control Event handler to pass the value of the string to the Second.aspx web form. Finally, we display the string values ​​in the first web form in the tag web control of the Second.aspx web form.

First, add a text box Web control to First.aspx, which is "ValyToPass". In the Click event handler, we need to pass the text attribute value of the "ValyToPass" text box. To achieve this, the code in the event handler should be from:

Response.Redirect ("Second.aspx")

change to:

Response.Redirect ("SECOND.ASPX? TextBoxValue =" &

Server.urlencode (Valuetopass.text))

Server.urlenCode () method encodes the incoming string parameters to make it secure QueryString. Earlier versions of browsers do not allow some characters (such as spaces) to queryString, and require these characters to escape as safe equivalents (for example, transfistence of spaces ). Server.urlenCode () can do all of these escape operations well. Note that after making such a change, when you click the button in First.aspx, the user's web browser will request the following URL: Second.aspx? TextBoxValue = ValueEnteredintintExtBox. This is actually passed to second.aspx named TextBoxValue.

Now, we need to add a tag web control in Second.aspx, whose ID attribute is "Results". We want to display the value of incoming textBoxValue queryString variables in this tag web control. To do this, we need to see the source code section of Second.aspx (to view the source code section, right-click Second.aspx in the Solution Explorer and select View Code [View Code]). Note that the source code section contains a subroutine called Page_Load. This subroutine will be executed once each time the ASP.NET web page is loaded, and the code you want to execute later is placed in the subroutine. Therefore, we add the following code line in the Page_Load event handler: results.text = request.QueryString ("TextBoxValue")

Request.QueryString () is the nameValueCollection of the queryString variable passed to the page. To access the value of a specific queryString variable named "name", use the syntax: request.QueryString ("name").

To demonstrate the operation of passing the value from a web form to another form, go to Debug and select the Start option. This will compile this source code and display First.aspx in the browser. Type "Hello, World!" In the text box, then click the button. This will go to Second.aspx and pass "Hello, World!" In queryString, "Hello% 2C World!". The queryString variable value after canceling the escape will be displayed in the label web control of Second.aspx, and generate the output result as shown in FIG.

Figure 10: Showing TextBoxValue QueryString Variables

Note that there are other methods to pass information from a web form to another, but this article will not be described later.

summary

This article describes the increasing similarity between using the ASP.NET development web application and traditional Windows desktop applications. Like design desktop applications, use the Visual Studio .NET design ASP.NET application, you also need to create a graphical user interface in the WysiwyG designer. However, there is no source code section of the ASP.NET web page consisting of classes in the user interface.

Although there are many similarities between desktop applications and web applications, there are still important differences. These differences limit the overall experience of end users. The most fundamental differences are the different media used to deploy desktop applications and web applications. Specifically, each component of the desktop application can be connected to each other and have a state, and there is a physical separator between the front end and the rear end of the web application. This separation limits the response performance of the web site user interface, and makes it difficult to complete in a Visual Basic desktop application environment in a web environment.

If you have already begun to be interested in ASP.NET (this is what I hope), then I suggest you refer to the following resources to continue learning ASP.NET.

ASP.NET Kick Start, Author: Stephen Walther. (ISBN: 0672324768) 4Guysfromrolla.com aspalliance.com

These printed resources and web resources help you understand the ASP.NET. In addition to the above resources, there are many well places where you can propose questions about ASP.NET and get fast and accurate answers. If you like to access the online forum, you can use ASP.NET Forums, which is a very active forum, which brings together a variety of ASP.NET developers. If you wish to use an email list service, register in Aspadvice.com, there are many very targeted list services, you can choose the services you need. Enjoy the fun of programming!

About the Author

Scott Mitchell has five books on ASP / ASP.NET, the founder of the 4guysfromrolla.com site, has been engaged in Microsoft Web technology in the past five years. Scott is a very active member of ASP and ASP.NET community, very popular ASP and ASP.NET technology, and is very willing to help others understand these exciting technologies. For more information on DataGrid, DataList, and Repeater controls, see SCOTT's book "ASP.NET DATA Web Controls Kick Start" (ISBN 0672325012).

转载请注明原文地址:https://www.9cbs.com/read-79849.html

New Post(0)