Programming Microsoft .NET : Web Forms, Page 3
The Active Server Pages Solution
When an Active Server Page is requested, ASP parses the page and executes any scripts contained inside it. Scripts access the input accompanying the request by using the ASP Request object, and they write HTML to the HTTP response using the ASP Response object. Figure 5-5 shows the ASP version of Calc.html. The VBScript between <% and %> tags checks the incoming request for inputs named op1 and op2. If the inputs aren't present, an empty calculator is rendered back to the client. If the inputs are present-that is, if Request ("op1") and Request ("op2") evaluate to non-null strings-the server-side script converts the inputs to integers, adds them together, converts the result to a string, and writes the string to the HTTP response using Response.Write.
<input type="text" name="op1" value=""/><input type="text" name="op2" value=""/>
But when the form is rendered again following a postback, Request ("op1") and Request ("op2") return the values input by the user and are echoed to the client in the tags' Value attributes:
<input type="text" name="op1" value="2"/><input type="text" name="op2" value="2"/>
To verify that this is the case, drop Calc.asp into \Inetpub\wwwroot and bring it up by typing http://localhost/calc.asp. Then enter a couple of numbers, click the = button, and use the View/Source command in Internet Explorer to view the HTML returned by ASP.
The appeal of ASP-and the reason it caught on so quickly after its introduction in 1997-is that it provides an easy-to-use model for dynamically generating HTML on Web servers. ASP provides a higher level of abstraction than either CGI or ISAPI, which means a flatter learning curve and faster time to market. And ASP integrates seamlessly with ActiveX Data Objects (ADO), which makes it a great solution for writing Web apps that interact with back-end databases.
<%@ Language="VBScript" %><html> <body> <form> <input type="text" name="op1" value="<%= Request ("op1") %>"/> + <input type="text" name="op2" value="<%= Request ("op2") %>" /> <input type="submit" value=" = " /> <% If Request ("op1") <> "" And Request ("op2") <> "" Then a = CInt (Request ("op1")) b = CInt (Request ("op2")) Response.Write (CStr (a + b)) End If %> </form> </body></html>
Page 3 of 4