Moving from ASP to ASP.NET

Although ASP.NET is based on the ASP technology, a lot of the fundamentals have changed. Here we explore some of the basic changes that you will encounter when migrating from ASP to ASP.NET.

<%%> Versus <script>

In ASP, all server-side code is written between <% and %> tags. This tells the ASP interpreter that everything between the two tags is program code and should be executed on the server.

In ASP.NET, the rules have changed a bit. Now, all variable and function declarations are placed between <script> and </script> tags, while implementation logic is contained in between the <% and %> tags. Listing A.1 shows an example ASP page, and Listing A.2 and Listing A.3 show the same page written in ASP.NET, using Visual Basic .NET and C#, respectively.

Listing A.1. ASP Page (C#)
<% 
    Sub MyFunction(psString) 
        If psString <> "" then 
            Response.Write psString 
        End If 
    End Sub 

    MyFunction Request.Form("txtText") 
%> 

<html> 
<body> 
    <form action="test.asp" method="POST"> 
        <input type="text" name="txtText"> 
        <input type="submit"> 
    </form> 
</body> 
</html> 

Listing A.2. ASP.NET Page (Visual Basic .NET)
<%@ Page Language="vb"%> 
<script language="vb" runat="server"> 
    Sub MyFunction(psString as String) 
        If psString <> "" then 
            Response.Write(psString) 
        End If 
    End Sub 
</script> 
<% 
    MyFunction(Request.Form.Get("txtText")) 
%> 

<html> 
<body> 
    <form action="test.aspx" method="POST"> 
        <input type="text" name="txtText"> 
        <input type="submit"> 
    </form> 
</body> 
</html> 

Listing A.3. ASP.NET Page (C#)
<%@ Page Language="c#"%> 
<script language="c#" runat="server"> 
    void MyFunction(String psString) 
    {
        if(psString != "") 
            Response.Write(psString); 
    } 
</script> 

<% 
    MyFunction(Request.Form.Get("txtText")); 
%> 

<html> 
<body> 
    <form action="test.aspx" method="POST"> 
        <input type="text" name="txtText"> 
        <input type="submit"> 
    </form> 
</body> 
</html> 

You should notice a few things here. First, note the format of the <script> tag, depending on which language you are using as your server-side code. You will want to use the language parameter to specify this. For Visual Basic .NET, you set the language parameter to vb; in C#, you set it to c#.

Second, notice that the function definition is placed between the <script> tags, as described previously, but the actual call to it, the implementation logic, is placed between the standard ASP <%%> tags. In ASP.NET, you must define all functions within <script> tags. However, calls to the function must be placed within the standard <%%> tags.

Finally, notice that instead of calling Request.Form("txtText") to get the value of the text box upon submission, you use a method of the Form object called Get. This method exists off the QueryString and Cookie collections also. Be sure to explicitly specify this in your code when trying to access any of the members of these collections; otherwise, your code will not compile.

Page Directives

The page directives that you are familiar with in ASP are still available in ASP.NET. However, a few new ones are worthy of mention. Table A.1 lists the new directives and what they can do for you.

Table A.1. ASP.NET Page Directives
DirectiveDescription
@ PageSpecifies page-specific attributes
@ ControlSpecifies control-specific attributes
@ ImportImports a namespace into the page
@ RegisterAssociates aliases with namespaces and class names for concise notation in Custom Server Control Syntax
@ AssemblyLinks an assembly with the current page
@ OutputCacheControls the caching of the page

Response.Redirect Versus Page.Navigate Versus Server.Transfer

To send the browser to a new page in ASP, the Redirect method of the Response object is used as shown:

Response.Redirect "OtherPage.aspx" 

In ASP.NET, you have two alternatives to this method. The first is the Navigate method of the Page object. The function takes the same parameter as Response.Redirect: the URL to redirect to. The difference between the two methods is that Page.Navigate calls Response.Redirect, unloads all controls in the tree, and then calls Response.End.

The second method, Server. Transfer terminates execution of the current ASP.NET page and then begins execution of the request on the page specified. It takes the following form:

Server. Transfer "OtherPage.aspx" 

At this point in the code, whatever script was being executed stops and then starts at the top of OtherPage.asp.

You should use Page.Navigate when you want to completely stop execution of one page and immediately move to the other, discarding the results of the current page. The Server. Transfer method should be used when you want to start execution on another page without discarding any of the previously computed information.

Cookies

The use of cookies is very different in ASP.NET. Instead of a cookie collection being part of the Response and Request objects, cookies are now objects that are manipulated individually and tossed into a master collection.

Previously, to add a cookie to the client machine, your code would look similar to Listing A.4.

Listing A.4. ASP Cookie Code
<% 
    Response.Cookies("MyCookie") = "Brian" 
%> 
<html> 
<body> 
The cookie is: 
<% 
    Response.Write(Request.Cookies("MyCookie")) 
%> 
</body> 
</html> 

The same code in ASP.NET is quite different.What makes it different is that cookies are now treated as objects.You create and instantiate a cookie object, set its value, and then append it onto the cookie collection. Requesting it back out of the collection is very similar to doing so in ASP. Just remember to use the Get method, as was shown in the previous section. Listing A.5 illustrates the procedure in Visual Basic .NET, and Listing A.6 illustrates the sequence in C#.

Listing A.5. Cookies (Visual Basic .NET)
<%@ Page Language="vb"%> 
<% 
   Dim oCookie As HttpCookie 
   oCookie = new HttpCookie("MyCookie") 
   oCookie.Values.Add("Name","Brian") 
   Response.AppendCookie(oCookie) 
%> 

<html> 
<body> 
<% 
    Response.Write(Request.Cookies.Get("MyCookie").Value) 
%> 
</body> 
</html> 

Listing A.6. Cookies (C#)
<%@ Page Language="c#"%> 
<% 
   HttpCookie oCookie; 
   oCookie = new HttpCookie("MyCookie"); 
   oCookie.Values.Add("Name","Brian"); 
   Response.AppendCookie(oCookie); 
%> 

<html> 
<body> 
<% 
    Response.Write(Request.Cookies.Get("MyCookie").Value); 
%> 
</body> 
</html> 

Events

Another major change to the way ASP.NET works is that it is based on an event model much like a typical Visual Basic program. Instead of your ASP script being executed from top to bottom, you can respond to events such as button clicks, text box changes, and so on. Of course, all these events will occur on the server side, so you will not be able to hook into every possible event, such as a mouse move or a key-down event.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset