Export (0) Print
Expand All
2 out of 3 rated this helpful - Rate this topic

Code Declaration Blocks

Defines member variables and methods compiled in the dynamically generated Page or UserControl classes that represent the ASP.NET page and the user control, respectively.

<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-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 on an ASP.NET page or user control. If you specify the language in multiple places, such as in the opening tag of a code-declaration block and in the @ Page or @ Control directive, they must match.
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.

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).

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 example demonstrates how you can define event-handling logic for the EnterBtn_Click event.

<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>
[Visual Basic]
<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

Introduction to Web Forms Pages | ASP.NET Web Forms Syntax

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.