asp.net.ph

Skip Navigation LinksHome > Getting Started > ASP.NET Syntax > ASP.NET Page Syntax > Code Declaration Blocks

Code Declaration Blocks


A code declaration block is a container for application logic used by an ASP.NET page.

It defines the member variables, event handlers, and custom functions that are to be used by the page.

Code declaration blocks are defined using <script> tags that contain the mandatory runat="server" declaration.

<script runat="server">
   Code goes here...
</script>

Attributes

The <script> tag accepts two optional attributes.

language Specifies the language used in this code declaration block. This value can represent any .NET-compatible language, such as Visual Basic ( VB ), C#, or JScript.NET.
src Specifies the path and file name of a script file to load. When this attribute is used, any other code in the declaration block is ignored.
<script runat="server" language="codelanguage" src="filepathname">
   Code goes here...
</script>

Remarks

The language attribute is used to specify the language of its contained code.

If no language is specified, this value defaults to the language configured for the base page or user control, set by using the @ Page and @ Control directives.

If no language is specified in the directive, ASP.NET defaults to VB.

<script runat="server" language="C#">

A <script> element can also be used 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.

<script runat="server" src="myScript.cs" />

With this method, the code declaration block is written in a separate file, usually referred to as the code-behind file.

This is particularly useful in cases where the external script file can be called by any page needing the code contained in the script.

Syntax Example

The below demonstrates using a code declaration block to define event-handling logic for the enter button click event.

<html>
<script language="C#" runat="server">
   void greetUser ( 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 = "greetUser " runat="server" />

      <p><asp:label id="message" runat=server />
   </form>
</body>
</html>
  C# VB
Handling Control Action Events
Run Sample | View Source

The below shows using the src attribute to link a page to an external script file.

<script language="C#" runat="server" src="today.cs" />

<div align="center">
   <p id="msg">Today is <%= now.ToString ( "f" ) %>.

   <%= strGreeting %></p>
</div>
Using Code Behind Script Blocks
Run Sample | View Source
See Also

Code Render Blocks   Authoring an ASP.NET Page



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note