Your First Web Forms Application


Because this is a kick start book, let's get started in actually using the product. Of course, being a technology, Hello World is required. So let's see how a simple Hello World application works in ASP.NET (see Listing 8.1).

Listing 8.1 Say Hello to ASP.NET
 <%@ Page Language="C#" %> <script runat="server">     void submitButton_Click(object sender, EventArgs e) {         message.Text = "Hello "+name.Text;     }     void clearButton_Click(object sender, EventArgs e) {         message.Text = "";         name.Text = "";     } </script> <html> <head> </head> <body>     <form runat="server">         <table border="1">             <tbody>                 <tr>                     <td>                         Name</td>                     <td>                         <asp:TextBox id="name" runat="server"></asp:TextBox>                     </td>                 </tr>                 <tr>                     <td>                         Message</td>                     <td>                       <asp:Label id="message" runat="server"></asp:Label></td>                 </tr>                 <tr>                   <td colspan="2">                    <p align="center">                     <asp:Button id="submitButton" onclick="submitButton_Click"                       runat="server" Text="Submit"></asp:Button>                     <asp:Button id="clearButton" onclick="clearButton_Click"                       runat="server" Text="Clear"></asp:Button>                    </p>                   </td>                 </tr>             </tbody>         </table>     </form> </body> </html> 

Open up your favorite notepad application, type the preceding code, and save it as HelloWorld.aspx in your Web root directory (typically C:\inetpub\ wwwroot , assuming that Internet Information Services was installed in the C: drive of your computer). Next, open a Web browser and go to http://localhost/HelloWorld.aspx and you should see the output similar to Figure 8.1 if you enter your name (I won't mind if you enter mine) and click the Submit button.

Figure 8.1. Say Hello to ASP.NET.

Voila! You have just created your first ASP.NET application. Now you can take a closer look at the various aspects of this application. The first line of the page specifies to the ASP.NET engine that you are going to be using C# as your choice for developing the ASP.NET page.

 
 <%@ Page Language="C#" %> 

Next is some script that looks like a Click event handler for some buttons . You'll also notice a runat="server" attribute to this script, which specifies that this is actually server-side script. That is good because you don't have to worry about browser compatibilities. The capability to provide server-side scripting logic isn't new to ASP.NET; it has existed all along with ASP itself. However, what is new is a clear and well-abstracted event model that goes with the rich set of controls that ASP.NET provides.

 
 <script runat="server">     void submitButton_Click(object sender, EventArgs e) {         ...     }     ... </script> 

Furthermore, the ASP.NET page contains what looks similar to an HTML form, but also contains the runat="server" attribute. Next is a set of TextBox/Label controls that again look like HTML form controls, but have the prefix asp: attached to them. Finally, you'll see a couple of buttons, which refer to the event handlers that you saw toward the beginning of the page. Actually, ASP.NET supports the same runat="server" for classic HTML controls. I have chosen to illustrate the ASP.NET set of controls because you'll soon realize that these new ASP.NET provided controls, because of the extended set of features provided by them, will typically become the preferred set. As the names probably suggest, TextBox is a control to solicit input from the user (similar to the HTML <input> control), Label is a placeholder for displaying dynamic text, and the Button control extends the HTML button (also known as <input type="submit">) functionality.

 
 <form runat="server">   <asp:TextBox id="name" runat="server"></asp:TextBox>   <asp:Label id="message" runat="server"></asp:Label></td>    <asp:Button id="submitButton" onclick="submitButton_Click"         runat="server" Text="Submit"></asp:Button> </form> 

A key observation to make here is that even though this is a Web application that you have developed using a Web technology (ASP.NET), the code looks very similar to that of a typical Windows Forms application. This is not a coincidence , but a key requirement that developers of ASP.NET technology had put before them ”unification of the development model, whether you are building Web or Windows forms applications.

Now that you have analyzed the source code of an ASP.NET Web page, take a look at what happened behind the scenes. If you had developed Web applications before, you would have realized that Web-based applications work on the classic request/response model of HTTP (Hypertext Transfer Protocol) using HTML (Hypertext Markup Language). However, what you have developed is actually an ASP.NET page, which the browser doesn't understand because the ASP.NET code is for the server side.

When the request was made by the Web browser to the Web server to the URL (Universal Resource Locator) http://localhost/HelloWorld.aspx (which is IIS in this case), the Web server found the ASP.NET script and understood that a Web server extension had been registered with the .aspx extension. This extension, which is really what manages the whole ASP.NET execution model, then parses the source code for any syntax errors and compiles it dynamically into .NET MSIL instructions. The resulting MSIL code is then Just-in-Time complied into actual machine code, like any other .NET assembly. After the execution is done on the server, the generated HTML is sent to the client as a response. Then the user types his or her name into the text box and this time, the compiled code is executed directly (assuming the code hasn't been modified since). The user's request parameters (in this case, "name" ) are queried, and an appropriate response is sent back to the user. Figure 8.2 shows this execution model.

Figure 8.2. ASP.NET execution model.

To understand better, it might be useful to take a behind-the-scenes look at HTML that has been generated by ASP.NET dynamically for a Web browser. (You can do this by opening the browser and selecting the View Source menu item.)

 
 <html> <head> </head> <body>     <form name="_ctl0" method="post" action="HelloWorld.aspx" id="_ctl0">     <input type="hidden" name="__VIEWSTATE" value="..." />     ...     <input name="name" type="text" id="name" />     ...     <span id="message"></span></td>     ...     <input type="submit" name="submitButton" value="Submit" id="submitButton" />     <input type="submit" name="clearButton" value="Clear" id="clearButton" />     ...     </form> </body> </html> 

SERVER CONTROLS VERSUS REQUEST AND RESPONSE OBJECTS

A key thing to point out for ASP Classic developers is that although ASP.NET still supports the programming model of Classic ASP where request parameters are explicitly parsed and manipulated using the Request and Response objects, the classic approach is deprecated in lieu of the enhanced programming model that is provided by the ASP.NET Server controls.


As expected, you don't see any ASP.NET “specific controls; what you see is standard HTML code for the corresponding ASP.NET Server controls with one exception. You will realize that ASP.NET runtime dynamically inserted a hidden form parameter __VIEWSTATE, which contains encrypted information. This parameter is really what connects the session information between requests and responses so that a stateless HTTP-based application appears stateful for the developer, simplifying the logic that the developer has to explicitly program.

Using Visual Basic .NET

By now you know that .NET supports multiple programming languages. Does that apply to ASP.NET? The answer is yes. ASP.NET retains the flexibility of using any Microsoft-supported programming language for development of Web-based applications (and those of other third parties). To prove it, take a look at a version of Hello World written to satisfy Visual Basic .NET programmers (see Listing 8.2).

Listing 8.2 Using Visual Basic.NET to Create ASP.NET Applications
 <%@ Page Language="VB" %> <script runat="server">     Sub submitButton_Click(sender as Object, e As EventArgs)         message.Text = "Hello "+name.Text     End Sub     Sub clearButton_Click(sender as Object, e As EventArgs)         message.Text = ""         name.Text = ""     End Sub </script> <html> <head> </head> <body>     <form runat="server">         <table border="1">             <tbody>                 <tr>                     <td>                         Name</td>                     <td>                         <asp:TextBox id="name" runat="server"></asp:TextBox>                     </td>                 </tr>                 <tr>                     <td>                         Message</td>                     <td>                       <asp:Label id="message" runat="server"></asp:Label></td>                 </tr>                 <tr>                  <td colspan="2">                   <p align="center">                     <asp:Button id="submitButton" onclick="submitButton_Click"                         runat="server" Text="Submit"></asp:Button>                      <asp:Button id="clearButton" onclick="clearButton_Click"                         runat="server" Text="Clear"></asp:Button>                   </p>                  </td>                 </tr>             </tbody>         </table>     </form> </body> </html> 

As you might have expected, the HTML and even ASP Server controls stay the same; what changed is the event-handling logic and the fact that you are using Visual Basic .NET as the programming language, as specified by the page directive.

 
 <%@ Page Language="VB" %> 

Using Visual J#

If you have been a Java programmer, the support for Visual J# in ASP.NET will probably work the best for you. (C# actually looks and feels similar to Java.) See Listing 8.3.

Listing 8.3 Using Visual J# to Create ASP.NET Applications
  <%@ Page Language="VJ#" %>  <script runat="server">     void submitButton_Click(Object sender, EventArgs e) {         message.set_Text("Hello "+name.get_Text());     }     void clearButton_Click(Object sender, EventArgs e) {         message.set_Text("");         name.set_Text("");     } </script> <html> <head> </head> <body>     <form runat="server">         <table border="1">             <tbody>                 <tr>                     <td>                         Name</td>                     <td>                         <asp:TextBox id="name" runat="server"></asp:TextBox>                     </td>                 </tr>                 <tr>                     <td>                         Message</td>                     <td>                         <asp:Label id="message" runat="server"></asp:Label></td>                 </tr>                 <tr>                  <td colspan="2">                   <p align="center">                    <asp:Button id="submitButton" onclick="submitButton_Click"                         runat="server" Text="Submit"></asp:Button>                    <asp:Button id="clearButton" onclick="clearButton_Click"                         runat="server" Text="Clear"></asp:Button>                  </p>                 </td>                 </tr>             </tbody>         </table>     </form> </body> </html> 


Microsoft.Net Kick Start
Microsoft .NET Kick Start
ISBN: 0672325748
EAN: 2147483647
Year: 2003
Pages: 195
Authors: Hitesh Seth

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