Tom Gullen
Tom Gullen

Reputation: 61773

Accessing a master page public variable from user control

public partial class MasterPages_Main : System.Web.UI.MasterPage
{
    public LoggedInUser ThisUser;

This is in my master page, and my user control is running on the page. In my user control however, I can't seem to reference ThisUser, do I have to pass it in to the control as a parameter? Is there any way to directly reference it?

Upvotes: 1

Views: 1299

Answers (2)

MAW74656
MAW74656

Reputation: 3549

Absolutely! Take a look at the methods and properties of an asp.net page. I've done similiar using:

HtmlForm mainform = (HtmlForm)Master.FindControl("form1");

Where the important part is the Master.FindControl();

This is a part of the Page class. Page.Master will get you to the master page of the current page.

Upvotes: 1

Brian Driscoll
Brian Driscoll

Reputation: 19635

I'm sure that there's some way to avoid passing an instance of LoggedInUser to your control, however it's probably best to pass it as a parameter as doing so promotes reuse of the control because it will be more loosely coupled.

Upvotes: 1

Related Questions