Functions and Subroutines Must Be Declared in <Script> TagsIn ASP Classic, you can declare functions and subroutines within <% and %> code render blocks (except in the case of the Global.asa file). For example, the following script works fine in ASP Classic: <% Function AddNums( intVal1, intVal2 ) AddNums = intVal1 + intVal2 End Function Response.Write( AddNums( 12, 34 ) ) %> If you try to execute this script in an ASP.NET page, however, you receive an error. You must declare functions and subroutines in a <Script> code declaration block like this: <Script runat="Server"> Function AddNums( intVal1, intVal2 ) AddNums = intVal1 + intVal2 End Function </Script> <% Response.Write( AddNums( 12, 34 ) ) %> One implication of this new requirement is that you cannot render content directly from within a subroutine or function. For example, in ASP Classic, the following script works fine: <% Sub ShowError %> <font color="red">Error!</font> <% End Sub ShowError %> However, because you cannot define a function or subroutine in a code render block within ASP.NET, you would need to rewrite the code like this: <Script runat="Server"> Sub ShowError Response.Write( "<font color=""red"">Error!</font>" ) End Sub </Script> <% ShowError %> |