ASP.Net Application - Code-Behind and Class Files
(Page 8 of 8 )
In addition to the file types mentioned here, you’ll also frequently deal with code-behind and/or class files. A code-behind file, also known as a code-behind class file, is a file containing .NET managed code (such as VB.NET or C#) that defines a class from which an ASP.NET page file, web service file, or application file inherits. This inherited relationship is indicated by the codebehind or src attribute, which indicates the file containing the code-behind class, and the inherits attribute, which indicates the namespace name (if any) and the class name of the class to inherit. Example 2-4 shows these attributes in action. At runtime, when the page, the web service, or the application is initialized for the first time, the ASP.NET runtime locates the code-behind file and either executes the compiled assembly associated with it (in the case of the codebehind attribute, which is used when the class will be precompiled) or compiles the class into an assembly dynamically (in the case of thesrcattribute). We’ll discuss the use of code-behind classes and the choice of which attribute to use in greater detail in Chapters 3 and 4.
Class files are simply source code files containing .NET managed code that is organized into namespaces and classes and that has been compiled before deployment, using either the Visual Studio .NET environment or the appropriate command-line compiler, into a .NET managed assembly. Class files are typically kept separate from the webapplication in which their assemblies are used, just as the source code for COM components used in classic ASP applications is typically kept separate from the web tree.
.vb extension
The .vb extension indicates source code files written in Visual Basic .NET. By default, code-behind classes created by the Visual Studio .NET environment use the naming convention filename.parentfileextension.languageextension. Thus, a VB.NET code-behind file for an ASP.NET page might have the name WebForm1.aspx.vb. This naming convention clearly conveys the relationship between the code-behind file and the page that inherits from it, as well as the language used in the code-behind file, so you can adopt this naming convention or use a similar one, even when not developing in the Visual Studio .NET environment.
.cs extension
The .csextension indicates source code files written in Microsoft’s new C# (pronounced “C Sharp”) language. These files, when created by Visual Studio .NET, use the same naming convention as the one just described.
parentfileextension.languageextension. Thus, a VB.NET code-behind file for an ASP.NET page might have the name