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>
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" />.
The following code example demonstrates how you can define event-handling logic for the EnterBtn_Click event.
Security 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. |
Note