tags:

views:

28

answers:

3

I am to access a method on my master page. I have an error label which I want to update based on error messages I get from my site.

public string ErrorText
{
    get { return this.infoLabel.Text; }
    set { this.infoLabel.Text = value; }
}

How can I access this from my user control or classes that I set up?

+2  A: 

To access the masterpage:

this.Page.Master

then you might need to cast to the actual type of the master page so that you could get the ErrorText property or make your master page implement an interface containing this property.

Darin Dimitrov
How can I use this.Page.Master from a class. It does not seem to work for me.
Kenyana
You could use this from a user control, not any class. Also `does not seem to work for me` is a phrase more commonly employed by common users that hardly know what a computer is and how it works than software developers. What have you tried? What error message are you getting? ...
Darin Dimitrov
A: 

I agree with @Darin. One thing to add is, Also look at some good techniques to avoid casting to master page

lakhlaniprashant.blogspot.com
A: 

Page should contain next markup:

<%@ MasterType VirtualPath="~/Site.master" %>

then Page.Master will have not a type of MasterPage but your master page's type, i.e.:

public partial class MySiteMaster : MasterPage
{
    public string ErrorText { get; set; }
}

Page code-behind:

this.Master.ErrorText = ...;

Another way:

public interface IMyMasterPage
{
    string ErrorText { get; set; }
}

(put it to App_Code or better - into class library)

public partial class MySiteMaster : MasterPage, IMyMasterPage { }

Usage:

((IMyMasterPage )this.Page.Master).ErrorText = ...;
abatishchev