Alex
Alex

Reputation: 3998

Access variable from master page on ascx page

I have the following setup on an aspx page:

<%@ Page Title="" Language="C#" MasterPageFile="~/tt.master" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="tt_login" %>
<%@ MasterType VirtualPath="~/tt.master" %>

and this on the aspx.cs page:

public partial class tt_login : System.Web.UI.Page
{
    protected new ttUser User
    {
        get { return Master.User; }
        set { Master.User = value; }
    }

In this case, the the ttUser is able to access the variable from the master page.

All fine so far.

Next, I have an ascx page, with the following setup:

<%@ Control Language="C#" MasterPageFile="~/tt.master" AutoEventWireup="true" CodeFile="ttRightNav.ascx.cs" Inherits="Controls_ttRightNav" %>
<%@ MasterType VirtualPath="~/tt.master" %>

and on the same page:

protected new ###.##.ttUser User
{
    get { return Master.User; }
    set { Master.User = value; }
}

Only this time it says the name master does not exist in the current context

A far as I can see the setup is exactly the same- I cannot see why it is not working.

A couple of thoughts:

The ascx page is not in the root folder - but as it uses ~ it is referencing the root so this should not matter?

Is it because this is an ascx page?

Have I missed anything?

I have tried referencing the master variable from the ascx.cs page, but still have the same problem.

Basically, how can I reference a variable from the master page on an ascx page?

I have seen a few questions about casting from the master page,but so far have not been able to get it to work.

Any help would be greatly appreciated - I am completely stuck!!!

EDIT:

This is not a duplicate of the question mentioned above - the question above is about a control - I am trying to access an object

Upvotes: 0

Views: 1093

Answers (1)

Emanuele Greco
Emanuele Greco

Reputation: 12741

  1. Make ttuser public
  2. Try casting your Master Page to the specific class and then get the nedded fields

tt_login loginPage = Page as tt_login;

or

tt_login loginPage = Master as tt_login;

Upvotes: 3

Related Questions