Xem mẫu

Chapter 10 26 ASP.NET Way back in the mists of recent history, there was a time when Microsoft wasn`t particularly interested in the Internet. In fact, for a while it looked like they were going to have nothing to do with the thing. But then certain strategists realized the importance that the Internet was likely to have, and managed to turn the company on a dime to start churning out Web and Internet tools. One technology that sprung out of this was Active Server Pages or ASP. This was, in this author`s humble opinion, one of the best products ever to come out of Microsoft. Strangely, because Microsoft strategy was in a state of flux, it was released without much fanfare yet became as popular as it is today simply because developers loved it. Essentially, ASP allows developers to write software that the user can access through a Web browser rather than a separate program installed on their computer. Thanks to the nature of the Web, it allows developers to write server-specific, Microsoft platform code on the server, but as the application is "operated" through HTML, it`s available to users on virtually any platform. Although this was, and is, possible without Microsoft technology, ASP supported the cut down version of Visual Basic called VBScript and coupled with very powerful and easy to use database access objects such as ADO, this made it possible for developers familiar with Visual Basic to build extremely powerful applications very quickly. With the advent of .NET comes Active Server Pages .NET, or ASP.NET. This technology takes the best of ASP and enhances it to not only provide all of the power of .NET through the Framework classes, but also to incorporate the powerful control-centric paradigm for building applications that we`ve seen on the desktop. What this means is that if we want to put a button on a Web page for the user to click, we can use the Toolbox to draw a button just as we would with a Windows Form. For this reason, the technology used to construct user interfaces in ASP.NET is known as "Web Forms". In this chapter, we`re going to take a look at ASP.NET and Web Forms. We`ll show you how to build basic Web pages as we create a small application that lets customers and salespeople check stock levels and prices over the Web. We then move on to take a look at how we can build a more complex application that lets us change data. Chapter 11 An Introduction Visual Studio .NET has some great tools for developers of ASP.NET sites. However, to use these tools you`ll need a Web server either on your local computer or on your network so you can run the pages. By default, the FrontPage Server Extensions 2000 are installed on your local machine along with the rest of the .NET Framework, providing that you have Internet Information Services installed before you install the Framework. This software allows a Web site editing tool (like FrontPage or Visual Studio .NET) to connect to the server in order to upload pages or, alternatively, download existing pages for editing. In this chapter and the next, I`ll assume that your computer does have the FrontPage Server Extensions 2000 correctly installed and enabled on your computer of choice. In this chapter, we use the term localhostto refer to your own desktop computer. This is an Internet-specific term that means, basically, "the local computer". It is used to refer to the computer that is running the current application or web page itself. Let`s create an ASP.NET project now. Try It Out – Creating the Project 1. Open Visual Studio .NET and create a new Visual Basic – ASP.NET Web Application project. 2. Set the name of the project to MyWebSite and make sure that the Location is set as http://localhost/ Notice the line under the Location box saying Project will be created at http://localhost/MyWebSite. This is important as we`ll need to refer to this later, so keep it in mind. 3. Click the OK button to create your new project. 2 ASP.NET How It Works At this point, Visual Studio .NET would have created your new project. The Solution Explorer, as with any other type of application, shows the files that make up the project. At this stage you can safely ignore most of these files. Right now, we`re only concerned with the Web Form files, with an .aspx extension. These are the Web equivalent of Windows Forms. Let`s start off our first Web Form by adding a button that will do something when clicked. Try It Out – Adding a Button to a Page 1. Right-click WebForm1 in the Solution Explorer and select View Designer to open it in Design view. Note the buttons at the bottom of the editor that tell us whether we are in Design or HTML view, and provide a quick way to switch from one mode to the other. 2. Using the Web Forms tab of the Toolbox, drag a Button control onto the page. 3. Open the Properties window as you would normally, and change the Button control`s Text property to Press Me. Change the ID property to btnPressMe. Notice that Web Form controls do not have a Name property, but instead have an ID property. This is because Dynamic HTML (DHTML), a technology heavily used by ASP.NET, assumes that control names are referenced through a property called ID. 3 Chapter 11 4. Double-click on the Button control. This will create a mouse click event handler, as you might expect, that we shall use to prove to the user that something has happened by changing the button text. Place the following code inside the Click handler: Private Sub btnPressMe_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnPressMe.Click ` Set the text... btnPressMe.Text = "Oh, that tickles!" End Sub 5. Run the project. An instance of Internet Explorer will pop up and display our button. 6. Now, press the button. You`ll see this: How It Works You can see that the .NET approach to building forms for the Web is very similar to the approach when building forms for the desktop. We create a page, we add controls and we wire up events. When the project is run, Internet Explorer is run, given the URL of the Web application, namely http://localhost/MyWebSite/WebForm1.aspx. 4 ... - tailieumienphi.vn
nguon tai.lieu . vn