Code Blocks in ASP.NET

All ASP.NET procedures and global variables should be declared within <script runat="server"> blocks, not between ASP <%...%> style delimiters. You can still declare variables in <%...%> render blocks but they will be accessible only to other render blocks on the page, not globally to other functions or procedures.

For instance, to declare variables and procedures, the script and render blocks would look like the following.

   <script language=VB runat=server>
      ' The following variables are visible to all procedures 
      ' within the <script> block.
      Dim str As String
      Dim i, i2 As Integer

      Function DoubleIt(inpt As Integer) As Integer
         ' The following variable is visible only within 
         ' the DoubleIt procedure.
         Dim factor As Integer = 2

         DoubleIt = inpt * factor
      End Function
   </script>

   <% 
      ' The following local variable is visible only within this
      ' and other render blocks on the page.
      Dim myVar 

      myVar = Request.QueryString
      Response.Write("The querystring is " _
         & Server.HtmlEncode(myVar.ToString()))
   %>

[C#]
   <script language=C# runat=server>
      // The following variables are visible to all procedures 
      // within the <script> block.
      String str;
      int i;
      int i2;

      int DoubleIt(int inpt) 
      {
         // The following variable is visible only within 
         // the DoubleIt procedure.
         int factor = 2;

         return inpt * factor;
      }
   </script>

   <% 
      // The following local variable is visible only within this
      // and other render blocks on the page.
      object myVar;

      myVar = Request.QueryString;
      Response.Write("The querystring is " + 
         Server.HtmlEncode(myVar.ToString()));
   %>

A page can contain more than one <script> block, but the programming language must be the same in all blocks on a page.

Render functions are not supported in ASP.NET. Using older versions of ASP, you could insert literal HTML within a procedure body, as shown in the following example.

   <%Sub SomeProcedure()%>
   <H3> Render this line of bold text. </H3>
   <%End Sub%>

This code would generate an error in an ASP.NET page. For ASP.NET, you would have to write the code as follows.

   <script runat=server>
      Sub SomeProcedure()
         Response.Write("<H3> Render this line in bold text.</H3>")
      End Sub
   </script>

All code that is included within the <script>...</script> tags, except global variable declarations, must be encapsulated within procedures.

With ASP, code was enclosed within <%...%> tags and page processing began with the first statement following the first <%> tag. With ASP.NET, any code that is to be processed as soon as the page is loaded must be included within the Page_Load intrinsic event. You can still write code in <%...%> blocks, but it will be executed at render time (in top-down fashion, as in ASP) after the page is loaded. If you need to execute initialization code, it should appear in the Page_Load event, which is raised immediately after the page is loaded by the ASP.NET engine.

   Sub Page_Load(Source As EventArgs, E As EventArgs)
      ' Place all startup code here, including initialization of 
      ' variables,  preloading of controls, and loading of 
      ' data from a database.
   End Sub
[C#]
   void Page_Load(EventArgs Source, EventArgs E) 
   {
      // Place all startup code here, including initialization of 
      // variables,  preloading of controls, and loading of 
      // data from a database.
   }

A related Page_Unload intrinsic event is always the last event to be raised during the page execution lifecycle and can be used to execute any page cleanup code.

See Also

Code Declaration Blocks | Code Render Blocks | Migrating ASP Pages to ASP.NET