I am trying to access a function placed in master page code-behind from another ASPX page as follows.
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();
}
}
This results in a compiler error:
Compiler Error Message: CS0176: Member 'Main.test()' cannot be accessed with an instance reference; qualify it with a type name instead
What is wrong in this scenario?
Thank you.
Try this: