@Pageディレクティブでは、ページ(.aspx)のさまざまな設定を定義する。
Inherits属性とCodeFile属性について試してみる。
この2つのファイルを用意する。
PageDirective.aspx.csを、
public partial class PageDirective : System.Web.UI.Page { public string Method1() { return "Method1 Called!"; } }
こうして、
PageDirective.aspxを、
<%@ Page Language="C#" Inherits="PageDirective" %> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title id="title_index">PageDirective</title> </head> <body> <% Response.Write(Method1()); %> </body> </html>
Inheritsだけにする。
クラス名があれば、自分のファイル名 + ".cs"だからね。
Inheritsだけじゃダメか。
では、
<%@ Page Language="C#" CodeFile="PageDirective.aspx.cs" %> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title id="title_index">PageDirective</title> </head> <body> <% Response.Write(Method1()); %> </body> </html>
CodeFileだけの場合どうだろ。
両方必要なのか。
最後に、
PageDirective.aspx.csなのに、
クラス名はPage1で、
PageDirective.aspxの側もInheritsはPage1にしている場合。
何の問題もない。
InheritsもCodeFileも存在していて正しく指定していればいいのか。
(考えてみれば、Javaと違って、C#はソース名とクラス名違ってもいいんだっけ。)
Sample/aspnet/PageDirective/PageDirective/src/PageDirective at master · bg1bgst333/Sample · GitHub