次のように、別のASPXページからマスターページコードビハインドに配置された関数にアクセスしようとしています。ASPXマスタページ機能へのアクセス
Main.master.cs:
public partial class Main : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
...
}
public static bool test()
{
return true;
}
}
Product.aspx:
<%@ Page Language="C#" MasterPageFile="~/Main.master" EnableEventValidation="false"
AutoEventWireup="true" ValidateRequest="false" CodeFile="Product.aspx.cs" Inherits="Common_Product" Title="Product" %>
...
<asp:Label id="test123" runat="server" />
Product.aspx.cs:
using SiteABC.Accelerate;
public partial class Common_Product : SiteABC.Accelerate.SerializePageViewState
{
private void Page_Load(Object sender, EventArgs e)
{
Main cm = (Main)Page.Master;
test123.Text = "yo | " + cm.test();
}
}
これは、コンパイラエラーになり:
Compiler Error Message: CS0176: Member 'Main.test()' cannot be accessed with an instance reference; qualify it with a type name instead
このシナリオで何が問題になっていますか?
ありがとうございます。
あなた 'test'メソッドは' static'キーワードで定義されていなければなりません。 [コンパイラエラーCS0176](http://msdn.microsoft.com/en-us/library/zhcxt2bd(v = vs80).aspx)、[静的クラスと静的クラスのメンバ] (http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx) –
@CAbbott、私はそれがちょうどタイプミスだと思う、あなたは実際にこのようなメソッドを定義することはできません... – walther