@Page Attributes: ClassName vs. Inherits ??

G

Guest

Between ClassName and Inherits, which attribute is set to specify the class
that a page uses? I would think that would be inherits. Further, the
description for ClassName is:

Specifies the class name for the page that will be dynamically compiled
automatically when the page is requested. This value can be any valid class
name but should not include a namespace.

So this is what will decidw what gets compiled? Then what is the point of
the src attribute? Are these two attributes mutially exclusive?
 
J

Joshua Flanagan

You can use ClassName to give a name to the page, when you use inline
code to define the page (using server-side script blocks).

If the server side code for the page is not defined inline, the source
can be specified in 2 ways:
- using the Inherits attribute - the code will be compiled into a DLL
before deployement. The Inherits attribute specifies which class
contains the code-behind for the given page.
- using the Src attribute - the code will be compiled dynamically at
runtime.

Joshua Flanagan
http://flimflan.com/blog
 
G

Guest

Hi Joshua,

Thanks for your response. Src and Inherits are much clearer. Could you give
me a scenario where you'd make use of the ClassName attribute? Would it be if
you were making a user control and wanted to be able to import the class it
generated?

-Ben
 
J

Joshua Flanagan

You use the ClassName whn you use inline code. It works for Pages and
user controls (which use the Control directive instead of Page).

Consider a file called MyPage.aspx that contained:

<%@ Page Language="C#" ClassName="MyClassName" %>
<html>
<body>
Hello <asp:Label id="lblName" runat="server"></asp:Label>
<script runat="server">
void Page_Load(object sender, EventArgs e){
lblName.Text = "Josh";
}
</script>
</body>
</html>


This page is completely self-contained. Notice that there is no
MyPage.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, you are
not explicitly defining a class. In order to give the page's class a
name, you use the ClassName attribute.
 
G

Guest

Thanks for the detailed response, Josh...

-Ben

Joshua Flanagan said:
You use the ClassName whn you use inline code. It works for Pages and
user controls (which use the Control directive instead of Page).

Consider a file called MyPage.aspx that contained:

<%@ Page Language="C#" ClassName="MyClassName" %>
<html>
<body>
Hello <asp:Label id="lblName" runat="server"></asp:Label>
<script runat="server">
void Page_Load(object sender, EventArgs e){
lblName.Text = "Josh";
}
</script>
</body>
</html>


This page is completely self-contained. Notice that there is no
MyPage.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, you are
not explicitly defining a class. In order to give the page's class a
name, you use the ClassName attribute.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top