I am writing an ASP.NET 4 application with C#. I have a Master Page, inside of which I have the following method:
public void DisplayMessage(string input)
{
Label myMessageDisplayer = (Label)FindControl("uxMessageDisplayer");
myMessageDisplayer.Text = input;
}
Can I call this method from a content page?
At the moment, I use in Content Page this code:
Master.DisplayMessage("Item has been inserted.");
And I receive this error:
‘System.Web.UI.MasterPage’ does not contain a definition for ‘DisplayMessage’ and no extension method ‘DisplayMessage’ accepting a first argument of type ‘System.Web.UI.MasterPage’ could be found (are you missing a using directive or an assembly reference?)
Any help will be appreciated.
You can use casting to get your master page type, as others have shown, or you can add the
MasterTypedirective to your page markup (at the top, where the standard<%@ Page %>directive is):Then in your page code, you can have just what you have in your example:
The
MasterTypedirective is available from .NET 2 upwards.