Question

Based on my reading, ClassName and Inherits attributes on the @ Master directive seem to do almost the same thing. I have read this page on MSDN and many other sources, and I'm still confused. This forum page seems to indicate that the Inherits attribute specifies the name of the class that will represent the master page at run time. The MSDN page, more vaguely, seems to say the same thing. This, then, would also be the name of the partial class specified in the code-behind file.

Reading about the ClassName attribute on MSDN, it seems to mean the same thing. I've tried many experiments in my code but still can't get things to work.

What, exactly, do these two attributes do, and what is the difference between them?

Ultimately, what I'm hope to accomplish is to allow my .aspx pages to access a public method in the code-behind file of the master page, as described on several posts including this one.

Was it helpful?

Solution

ClassName is used for giving a name to the page, when we use inline code[ meaning all of the HTML and server-side code is included in the single page]. Therefore this attribute should be used when we have the server side code within the aspx page only using Script blocks, like:

<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
           welcomeLabel.Text = "Hi There";
     }
</script>

This can be used for both Pages or user controls.

At run time, a single-file page is treated as a class that derives from the Page class. AND THIS CLASS SHOULD HAVE A NAME

Since there is no seperate C# code file [ Also known as Code Behind file ], there is no explicitly defined class. In order to give the page's class a name, the ClassName attribute is being used.

Inherits Attribute::

If we place the server side code for the page in a .cs file, then only we should use Inherits attribute.

When a web form is compiled, its page is parsed and a new class is generated and compiled. This new class derives from the class identified in the inherits keyword.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LINQ2Entities.aspx.cs"
         Inherits="LINQ2Entities" %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top