WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Writing ASP.NET Web Forms with C#

by Budi Kurniawan
06/21/2001

Web Forms are a programming model in ASP.NET, the next generation of Web programming technology from Microsoft.

This article discusses Web Forms by first comparing ASP.NET and the classic ASP. It then presents server controls and paints a big picture of how separation between business logic and presentation layer is achieved in Web Forms. In addition, this article presents the new session-management strategy and offers some programming experience in building Web Forms using the C# language. Web Forms are a Web development technology that allows you to build a robust and scalable Web application rapidly. Programming languages that you can use to build Web Forms are many; you can use C#, Visual Basic.Net and JScript.NET, to name a few. Like other server-side Web technologies, Web Forms output standard HTML that can be rendered by all browsers. However, the engine that runs Web Forms also detects user browsers, and can generate browser-specific HTML tags, if you so desire.

Web Forms are also object-oriented, even though they are still hosted in ASP-like pages. Web Forms also provides easy separation between presentation layer and business logic. The code can be written in the same page as the user interface or stored separately in a different file. Using a RAD tool like Visual Studio.NET, Web Forms programming is even easier and more rapid.

Among the benefits that the new technology brings, the most important is probably the session management strategy that allows user sessions to be maintained without depending on cookies and or sacrificing scalability. As you will see, the new session management can also be set to work in a Web farm. Better still, the programming part is still the same as in classic ASP.

To introduce Web Forms, first the comparison with ASP, something that you might already be familiar with.

Comparing ASP and ASP.NET

The first cosmetic difference between ASP and ASP.NET is the page extension. ASP pages have an .asp extension; ASP.NET has changed it to .aspx. For backward compatibility, however, ASP.NET still supports the programming model of ASP. Therefore, though your pages now have an .aspx extension, you can still use the old ASP style to build spaghetti-like code, interweaving code and HTML tags, like the following.

<%@ Page Language="C#" %>
<html>
<head>
<title>ASP.NET Using the old ASP style</title>
</head>
<body>
<IMG SRC="logo.jpg">
<BR>
<%
  for (int i=0; i<6; i++) {
    Response.Write(i + "<BR>");
  }
%>	
</body>
</html>

Note, though, that the default language in ASP.NET is Visual Basic.NET. So, if you are using C#, you need to include the Language attribute in the Page directive in the first line. With ASP.NET there is a new programming model: Web Forms. The most important difference from ASP is that in Web Forms you always separate the business layer and the presentation layer, the code from the user interface. If you are an ASP programmer and ASP is the only programming that you do, the new programming model is probably not familiar to you. In ASP, for one application you normally have multiple pages, where each page only handles a part of one particular task, and it is normal to make your user jump agilely from one page to another. Web forms, on the other hand, have only one page to process a particular task. Imagine something like a recursive page in classic ASP. Web Forms are like Windows forms, only on the Internet. If you have been programming with Visual Basic or Visual C++, you will find Web Forms very familiar. To illustrate this point, consider an ASP login page that checks the user name and password. In the old ASP, you would have two pages: Login.html and VerifyLogin.asp, as in Listings 1 and 2 respectively.

Listing 1: Login.html

<HTML>
<HEAD>
<TITLE>Login</TITLE>
</HEAD>
<BODY>
<FORM ACTION=VerifyLogin.asp METHOD=POST>
User Name: <INPUT TYPE=TEXT NAME=UserName>
<BR>
Password: <INPUT TYPE=PASSWORD Name=Password>
<BR>
<INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>

Listing 2: VerifyLogin.asp

<%
  Option Explicit
  Dim UserName, Password

  UserName = Request.Form("UserName")
  Password = Request.Form("Password")
  If UserName = "kittyhawk" AND Password = "toronto" Then
    Response.Write "Welcome Kitty Hawk"
  Else
    Response.Redirect "Login.html"
  End If

%>

See how you end up with two pages? With Web Forms, you will only have one page, whose code is given in Listing 3.

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow