ASP.net access a master page variable through content page
Asked Answered
T

2

13

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){%>
        $("#dialog").dialog();
    <% } %>
    }

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

Tarbes answered 6/9, 2010 at 12:12 Comment(0)
J
14

Use @MasterType directive, as explained here:

http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx

Josettejosey answered 6/9, 2010 at 12:27 Comment(5)
Youre right, didn't even notice that was missing in his declaration. +1.Envelope
Thanks for the answer, but I'm still a bit lost with this! Where do I put that directive?Tarbes
Hi Tom, you have to put it under your @page directive, in your .aspx pages.Josettejosey
Thanks, I do that <%@ MasterType TypeName="AdminMaster" %> on my content page but ((AdminMaster)Master).blnShowDialogue = true; doesn't work still , type or namespace not foundTarbes
@Tom - get rid of the casting to (AdminMaster). Just use "Master.blnShowDialogue". I assume the "AdminMaster" class is in the same namespace as the page you're trying to access it from? If not, you will need to use the "VirtualPath" attribute (as opposed to TypeName).Envelope
E
6

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.

Envelope answered 6/9, 2010 at 12:25 Comment(2)
Thanks, but it gives the error, The type or namespace name 'AdminMaster' could not be found (are you missing a using directive or an assembly reference?)Tarbes
@Tom - @mamoo's answer is correct (didnt look at your Master page directive closely enough), give his way a go.Envelope

© 2022 - 2024 — McMap. All rights reserved.