Compartir a través de


ClassName Vs Inherits

ClassName is to give a name to the page, when we use inline code to define the page (using server-side script blocks in .aspx pages).
It works for both Pages and user controls (which use the Control directive instead of Page directive).

Consider a file called ClassnameVsInherits.aspx that contains:

<%@ Page Language="C#" ClassName="ClassnameVsInherits" %>
<html>
<body>Hello <asp:Label id="lblLoginUserName" runat="server"></asp:Label>

<script runat="server">
void Page_Load(object sender, EventArgs e)
{
lblLoginUserName.Text = "Geek";
}
</script>
</body>
</html>

This page is completely self-contained and there is no ClassnameVsInherits.aspx.cs file required. All of the HTML and server-side code is included in the single page. Since there is no C# code file, there is no explicitly defined class. In order to give the page's class a name, the ClassName attribute is being used.

If the server side code for the page is not defined inline, the source can be specified in 2 ways:

  1. using the Inherits attribute - the code will be compiled into a DLL before deployment. The Inherits attribute specifies which class contains the code-behind for the given page.

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

  2. using the Src attribute - the code will be compiled dynamically at runtime.

    <%@ Page Language="C#" Inherits="MyBlog.HelloWorld" src="HelloWorld.cs" mce_src="HelloWorld.cs" trace="true" %>

Comments