Simplest Example: Writing a Simple ASP.NET Page


Let’s look at the simplest ASP.NET example that I could think of. Shown in Figure 3-4, it displays the current time on the server, either with or without the seconds digits, in the user’s choice of text color. You can download the code from this book’s Web site, http://www.introducingmicrosoft.net, and follow along with the discussion.

An ASP.NET sample program begins here.

click to expand
Figure 3-4: Simplest example of ASP.NET.

I wrote this sample using Visual Studio .NET. While I’ve tried to write my examples in a tool-agnostic manner, Visual Studio .NET contains so much built-in support for ASP.NET, especially debugging, that not using it would have been like using Notepad to write a Visual Basic Windows desktop app. You can do it if you really, REALLY want to and probably get it to work eventually. But it will be much faster and less painful if you take the right approach from the beginning.

The key to understanding this example is to think of your Web page as a Visual Basic form. I use that analogy because I think that my readers are most familiar with it, but you could write this same code in any other Visual Studio .NET language—C#, C++, or J#—if you want to. (I’ve provided C# version of all the code in the sample files.) I started by generating an ASP.NET Web Application project from Visual Studio .NET, as shown in Figure 3-5. Doing this creates, among other things, an ASP.NET page called WebForm1.aspx. I then used the Toolbox to drag Web controls onto the form, shown in Figure 3-6, adding a check box, a button, a drop-down list, and a couple of labels. I then set the properties of these controls using the Properties window in the lower right corner of the Visual Studio .NET window, specifying the text of each control, the font size of the labels, and the items in my drop-down list collection. I set the AutoPostBack property of the drop- down list control to True, meaning that it automatically posts the form back to the server when the user makes a new selection in the drop-down list. Setting user interface properties with a slick, familiar editor like this is so much easier than writing HTML strings in Notepad (which is what you had to do in ASP) that it isn’t funny.

You put Web controls on your .ASPX pages, just as you do on a Visual Basic form.

click to expand
Figure 3-5: Choosing an ASP.NET Web Application in Visual Studio .NET.

click to expand
Figure 3-6: Using the Toolbox to drag Web controls onto the form.

Now that I’ve laid out my controls, I need to write the code that ties them together. When I generated the project, Visual Studio .NET also generated the class that represents the code behind my ASP.NET page. You view it by right-clicking on the page and selecting View Code from the shortcut menu, just as you do today with forms in Visual Basic 6.0. Web controls fire events to their forms, again just as in Visual Basic, so I need to write event handlers for them. I can add an event handler by choosing the control from the upper-left drop-down list and choosing the appropriate event from the upper-right drop-down list. I show excerpts from my Visual Basic class in Listing 3-2, and all of it is included with this book’s downloadable sample code. In this simple example, when the button reports a Click event, I change the label’s text property to hold the current time. I also added a handler for my drop-down list box’s SelectedIndexChanged event. When the user makes a color selection from the list box, I set the label’s color to the value selected by the user. When I build the project, Visual Studio .NET automatically publishes it to my machine’s Web root directory.

You write event handlers for your controls’ events, just as you do for a Visual Basic form.

Listing 3-2: Excerpts from the code behind the WebForm1.aspx page.

start example
Public Class WebForm1 Inherits System.Web.UI.Page ’ User clicked the button. Fetch the time and display it in the ’ label control. Public Sub Button1_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Button1.Click If CheckBox1.Checked = True Then Label1.Text = "The time is now " + now.ToLongTimeString Else Label1.Text = "The time is now " + now.ToShortTimeString End If End Sub ’ User has selected a different color from the list box. ’ Change the label’s color to reflect the user’s choice. Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender _ As System.Object, _ ByVal e As System.EventArgs) _ Handles DropDownList1.SelectedIndexChanged Label1().ForeColor = _ Color.FromName(DropDownList1().SelectedItem.Text) End Sub End Class
end example

The event handler code that you write runs on the server, not the user’s browser. That’s why Web Forms controls are often known as “server controls.” When the user clicks the Get Time button or makes a selection from the drop-down list, the page is posted back to the server. ASP.NET catches the incoming page, detects which control event triggered the postback, and invokes the handler code for that event. This causes the page to be regenerated, with whatever changes the event handler code might have made to it, and written back to the client. You can read more about this process in the next section of this chapter. If you’d like even more detail, you can download the chapter on Web Forms controls from this book’s Web site, http://www.introducingmicrosoft.net.

The event handler code that you write runs on the server.

Now that I’ve finished writing my code, I’d like to see it in action. The easiest way to do this is to right-click on the page in Solution Explorer and select Build And Browse from the shortcut menu. This opens a browser window within the Visual Studio .NET environment. When this browser (or any other client) requests the .ASPX page, IIS loads it into the ASP.NET execution engine, which then parses the page. The first time the engine encounters the page it compiles its code “just-in-time.” Subsequent requests for the same page will load and run the compiled code. The engine then executes the class’s compiled code, creating the Web controls that I earlier placed on the form. The controls run only on the server side, rendering HTML that reflects their current state, which is incorporated in the page that gets sent to the client. The engine also executes the event handlers’ code and renders the output HTML created by the code’s interaction with the controls. The final HTML is written back to the client, which produces the page shown previously in Figure 3-4.

An execution engine executes the class associated with the ASP.NET page and renders HTML from its controls and code.

That’s all I had to do to write an .ASPX page using Visual Studio .NET and Web controls. It’s much easier than original ASP. It looks like and feels like writing Visual Basic code, which we’re already familiar and comfortable with.

start sidebar
Tips from the Trenches

My clients report that the key to most user interface design problems is locating a third-party Web Forms control that provides the prefabricated behavior that they are looking for. Often they’ll change their user interface requirements to conform to the controls they can buy off the shelf, rather than spend weeks writing code that would provide slightly different behavior.

end sidebar




Introducing Microsoft. NET
Introducing Microsoft .NET (Pro-Developer)
ISBN: 0735619182
EAN: 2147483647
Year: 2003
Pages: 110

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net