Functions and Subroutines Must Be Declared in Script Tags


Functions and Subroutines Must Be Declared in <Script> Tags

In 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 %> 


ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

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