Skip to main content

What is Diffrence between Src and Codebehind

You may have noticed that Visual Studio.NET adds an interesting keyword to the Page directive of an ASP.NET web form: CodeBehind. However, you will not find this documented in the .NET documentation for the Page directive. Visual Studio.NET implements CodeBehind by precompiling the base class, then uses the Inherits keyword to refer to that class. The CodeBehind keyword allows the IDE to remember the file containing your base class. However, what if you do not want to precompile your class into an assembly?

According to the .NET documentation, the Src attribute of the Page directive:

Specifies the source file name of the code-behind class to dynamically compile when the page is requested. Note: RAD designers, such as Visual Studio.NET, do not use this attribute. Instead, they precompile code-behind classes and then use the Inherits attribute.

So, by using the Src attribute to indicate the file, and the Inherits keyword to indicate the class, you can accomplish Codebehind. Here is an example Web Form Inheriting from the HelloWorld class in the HelloWorld.cs file:

<%@ Page Language="C#" Inherits="DotNetCoders.HelloWorld" Src="HelloWorld.cs" trace="true" %> <html> <body> <asp:Label id="ProgrammersMessage" runat="server" /> </body> </html>
As you can see, the differences in the page directives are as follows:

Visual Studio.NET

<%@ Page language="C#" Inherits="DotNetCoders.HelloWorld" Codebehind="HelloWorld.aspx.cs" %>
Inherits keyword


Popular posts from this blog