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.
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.
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="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>