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:
public partial class Main : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
...
}
public bool test()
{
return true;
}
}
Error said it quite clearly, you can't access static methods with an instance reference.
You need to do it like this:
test123.Text = "yo | " + Main.test();
However, I'm not sure if it's the best practice to put methods like this to your MasterPage... You should create a new class and use that instead.
Change your Test so that it's a property
public partial class Main : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
...
}
public Property bool test()
{
get { return true; }
}
}
You can not access static method using instance object.
It should be
Main.test();
Related
I want to call a method of a Content from a master page sending a Parameter to manipulate one label.
public partial class MasterCategoria : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSalada_Click(object sender, ImageClickEventArgs e)
{
produtosCategoria x = new produtosCategoria();
x.changeLabel("Salada");
}
}
Manipulating this button on this WebForm which is a Content
public partial class produtosCategoria : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void changeLabel(string name)
{
lblTexto.Text = name;
}
But this isn't working. How can I do this work?
Thank you guys, and sorry about my english.
The object of type produtosCategoria is already created and it can be accessed from your master page via this.Page.
So to change the label of your content page you can do as in the snippet below.
Also, I added a simple type check so you won't get an error if another content page is loaded
protected void btnSalada_Click(object sender, ImageClickEventArgs e)
{
// Check if it is the correct content page
if (this.Page.GetType() == typeof(produtosCategoria))
{
produtosCategoria x = (produtosCategoria)this.Page;
x.changeLabel("Salada");
}
}
Note: When code executes in the master page this is the master page and this.Page is the content page
I have a public variable in my content page that I need to access in my MasterPage. So I can set a Javascript variable... .
How can I reference a public content page variable from the masterpage?.
I supposed you want to say that you want to access to variable in the MasterPage from Contend Page, if is correct, use this example:
Declare your public or protected variable:
public partial class MasterPage : System.Web.UI.MasterPage
{
public string strEmpresa = "NS";
protected void Page_Load(object sender, EventArgs e)
{
}
}
Set the following directive at the beginning of your content page:
<%# MasterType virtualPath="~/MasterPage.Master"%>
then you can use the public variables of your MasterPage, using Master.NameVariable.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Text = Master.strEmpresa;
}
}
In other case if you really want access to variable in ContentPage from MasterPage, you just can set the value in Session and then read in MasterPage. For example:
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["myVariable"] != null)
{
TextBox1.Text = Session["myVariable"].ToString();
}
}
}
}
public partial class WebFormMP_TestPublicVariable : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["myVariable"] = "Test";
}
}
}
There are many ways that you can achieve this. check around internet ;).
I have been trying to find a good answer to this question, but can't seem to find one. I have an ASP.NET page that derives from a base page, like this:
public partial class MainPage : MyBasePage
{
protected void Page_Load(object sender, EventArgs e)
{
var loginTime = GetLoginTime(); // This works fine
}
}
And the base page:
public partial class MyBasePage: Page
{
}
protected DateTime GetLoginTime()
{
// Do stuff
return loginTime;
}
Now I have a user control on that page that needs to call my method...Like this:
public partial class TimeClock : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
var loginTime = GetLoginTime(); // This does not work!
}
}
As you can see, I cannot call my base method, for obvious reasons. My question is, how can I call this method from my user control? One work around I've found is like this:
var page = Parent as MyBasePage;
page.GetLoginTime(); // This works IF I make GetLoginTime() a public method
This works, if I make my function public instead of protected. Doing this doesn't seem like a very OOP way to tackle this solution, so if someone can offer me a better solution, I'd appreciate it!
TimeClock inherits from UserControl, not from MyBasePage so why should TimeClock see the Method GetLoginTime()?
You should keep your UserControl out of your Page stuff. It should be decoupled in OOP speak. Add properties to set values and delegates to hook into events:
public partial class TimeClock : UserControl
{
public DateTime LoginTime{ get; set; }
public event UserControlActionHandler ActionEvent;
public delegate void UserControlActionHandler (object sender, EventArgs e);
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button_Click(object sender, EventArgs e)
{
if (this.ActionEvent!= null)
{
this.ActionEvent(sender, e);
}
}
}
Page
public partial class MainPage : MyBasePage
{
protected void Page_Load(object sender, EventArgs e)
{
var loginTime = GetLoginTime();
TimeClock1.LoginTime = loginTime;
TimeClock1.ActionEvent += [tab][tab]...
}
}
(this.Page as BasePage).MethodName()
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
content page class method calling from master page class
I need to access Content Page Method from Master page Event. How can I do this?
Content Page:
public partial class Call_Center_Main : System.Web.UI.Page
{
Page_Load(object sender, EventArgs e)
{
}
public void MenuClick(string ClkMenu)
{
// Some Code
}
}
MasterPage:
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{
//How Can I call MenuClick method from Content Page from Here ???
}
}
This answer is taken from Interacting with the Content Page from the Master Page
You can do this using Delegates.
For Example, you have a button in MasterPage and you want to call a Method in Content Page from Master Page. Here is the Code in Master Page.
Master Page:
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (contentCallEvent != null)
contentCallEvent(this, EventArgs.Empty);
}
public event EventHandler contentCallEvent;
}
Content Page:
public partial class Content_1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void Master_ButtonClick(object sender, EventArgs e)
{
// This Method will be Called.
}
protected void Page_PreInit(object sender, EventArgs e)
{
// Create an event handler for the master page's contentCallEvent event
Master.contentCallEvent += new EventHandler(Master_ButtonClick);
}
}
And Also add the Below Line Specifying you MasterPage Path in VirtualPath
<%# MasterType VirtualPath="~/MasterPage.master" %>
// This is Strongly Typed Reference
I have a page that contains a static method like this:
public partial class _Default : System.Web.UI.Page
{
[WebMethod(EnableSession = true)]
public static string Aware()
{
return DateTime.Now.ToString();
}
}
How I can call this method from a Master Page?
_Default.Aware() as for any other static method
EX:
public partial class About : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
_Default.Aware();
}
}