1

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.

Community
  • 1
  • 1
Tim
  • 1,755
  • 2
  • 22
  • 34
  • Assuming you understood what Inherits is then think ClassName as grand-child. It's name of class generated from your ASPX code. Given your page then Inherits is his parent and ClassName his son. – Adriano Repetti Dec 19 '13 at 23:19
  • OK, if I understood you correctly, Inherits actually specifies the superclass of the class that represents the master page? That's what the term would mean to me intuitively. Then as asp.net master page would normally specify System.Web.UI.MasterPage for its Inherits attribute? – Tim Dec 19 '13 at 23:25
  • Take a look to [this](http://msdn.microsoft.com/en-us/library/c8y19k6h.ASPX), IMO it's pretty explanatory. Anyway yes, most of time (but not Always, of course) you'll use MasterPage (or you'll omit). – Adriano Repetti Dec 19 '13 at 23:34

1 Answers1

2

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" %>
R.C
  • 10,417
  • 2
  • 35
  • 48
  • Thank you for your thoughtful answer. Discussing "Inherits", you said "...a new class is generated...derives from the class identified in the inherits keyword." So the .ASPX page becomes a class that is a subclass the class identified in "Inherits". How is _that_ class generated (in your example, "LINQ2Entities")? – Tim Dec 23 '13 at 19:38