access a master page variable through content page

I have a master page:

<%@ Master Language="C#" AutoEventWireup="true" Codefile="AdminMaster.master.cs" Inherits="AlphaPackSite.MasterPages.AdminMaster" %>

Then I have a public variable:

public partial class AdminMaster : System.Web.UI.MasterPage
    protected bool blnShowDialogue = false;

In my content page I would like to set this variable:

blnShowDialogue = true;

So that in my master page I can have the code:

    $(function() {
    <%if(blnShowDialogue == true){%>
    <% } %>

Does this make sense? When I try combinations of Master.blnShowDialogue, or blnShowDialogue = , etc etc nothing seems to work.

The name ‘blnShowDialogue’ does not
exist in the current context

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Use @MasterType directive, as explained here:

Solution 2

You need to cast the Master page to the actual type.

((AdminMaster)Master).blnShowDialogue = "Foo";

Otherwise Master will simply be referring to the base class Master – you’re trying to access a property in your actual class which derives from the Master class.

The error you are getting is because a property called blnShowDialogue does not exist in the class System.Web.UI.MasterPage – which makes sense, as you’re not telling it which specific MasterPage instance you are trying to refer to.

Hope that helps.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply