Share via


Code Declaration Blocks

Code-declaration blocks define sections of server code that are embedded in ASP.NET application files within <script> blocks marked with a runat="server" attribute.

<script runat="server" language="codelanguage" Src="pathname">
   Code goes here.
</script>

Attributes

  • language
    Specifies the language used in this code declaration block. This value can represent any .NET Framework-compatible language, such as Visual Basic (VB), C#, or JScript .NET. If no language is specified, this value defaults to that specified in the @ Page or @ Control directive. If no language is specified in the directive, the default is VB, unless you have changed the default in your application's Web.config file.

    Note

    Only one language can be used for an ASP.NET page or user control. If you specify the language in multiple places (which is unnecessary), such as in the opening tag of a code declaration block and in the @ Page or @ Control directive, they must match.

  • runat
    When the value is runat="server", this attribute specifies that the code contained within the script block runs on the server and not the client. This attribute is required for server-side code blocks.
  • src
    Specifies the path and file name of an external script file to load. When this attribute is used, any other code in the declaration block is ignored.

Remarks

Code declaration blocks are defined using <script> tags that contain a runat attribute value set to server. The <script> element can optionally use a language attribute to specify the language of its inner code. If none is specified, ASP.NET defaults to the language configured for the base page or user control (controlled using the @ Page and @ Control directives). These blocks of server-side code can be declared within a number of ASP.NET application files, including Web pages, user controls, master pages, and Global.asax files. When a given ASP.NET application file is compiled, the embedded code block is compiled with the particular object that is associated with the given ASP.NET file type. For example, when a page is compiled, any embedded code declaration blocks are compiled along with the Page class into a single page object on the server.

You can also use the <script> element to specify an external script file by using the src attribute. When you define the src attribute, all content between the opening and closing tags of the <script> element is ignored. In this case, use a closing slash at the end of the opening tag. For example: <script runat="server" src="myFile.cs" />.

Example

The following code example demonstrates how you can define event-handling logic for the EnterBtn_Click event.

Security noteSecurity Note

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

<html>
  <script language="C#" runat="server">
      void EnterBtn_Click(Object Src, EventArgs E) {
          Message.Text = "Hi " + Name.Text + ", welcome to ASP.NET!";
      }
  </script>

  <body>
   <form runat="server">
    Enter your name: <asp:textbox id="Name" runat=server/> 
                     <asp:button text="Enter" Onclick="EnterBtn_Click" runat="server"/>
        <p>
        <asp:label id="Message" runat=server/>
    </form>
  </body>
</html>
<html>
  <script language="VB" runat="server">
      Sub EnterBtn_Click(Src As Object, e As EventArgs)
         Message.Text = "Hi " & Name.Text & ", welcome to ASP.NET!"
      End Sub
  </script>

  <body>
   <form runat="server">
    Enter your name: <asp:textbox id="Name" runat=server/> 
                     <asp:button text="Enter" Onclick="EnterBtn_Click" 
                       runat="server"/>
        <p>
        <asp:label id="Message" runat=server/>
    </form>
  </body>
</html>

See Also

Concepts

ASP.NET Web Page Syntax Overview