Passing a variable from a content page to a master page

I want to pass a string variable from a content page to a master page so that I can use it to change the Visible switch on and off in specific content tags.

I’ve accomplished it using a Session variable but I am wondering if there is a better way to pass the variable by making the variable set in the content page available to the master page?

(PS is anyone else having trouble with the search function returning a blank page?)

don’t do that.

You can:

  1. make a public property / method on the master page
  2. use the @MasterType directive on the content page

This will allow the content page to invoke the property/method using its Master property, like Master.[I]Method/I;

Ah, thank you. I knew there was a better way. You rock!

Do you happen to have an example of string in the content page passed to the masterpage?

I’ma gonna go google some. :slight_smile:

Honeymonster is [as usual] correct. Another option would be to use a set of interfaces to set up your “plumbing” to pass data between master pages and clients. Or from user controls to master pages. See this blog post for an example.

OK, here’s what I came up with. Please tell me if it is acceptable:

in default.master.cs with if-then-else testing against pageTypeVar:

    public string pageTypeVar;

in default.aspx using MasterType directive:

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Master.pageTypeVar = "home";
    }
</script>

Seems to work well, I just wanted to make sure it was technically correct.

Thank you again! :slight_smile:

Yup. I would make it a public property backed by a private field, though.

Could you explain that a bit? :slight_smile:

It’s not considered to be a good idea to directly change a variable that belongs to some other object, so instead usually use wrap a private variable with a public property - so you’ve better control over changes, coming from outside.

In C# 3.0 you can use Automatic Properties if you need a simple - and most often required - get/set accessor:

public string PageTypeVar { get; set; }

Or you can go the old way:

		private string _pageTypeVar;
		public string PageTypeVar
		{
			get { return _pageTypeVar; }
			set { _pageTypeVar = value; }
		}

One other thing to note–that private member will not persist it’s values across requests. If you want to keep the value, you need to “back” it with one of several options depending on how you want the storage to work. Options being:

  1. Application: this variable is shared throughout the entire application. Effectively the same as declaring it “static”
  2. Session: this means there is one shared value across a user’s session. If a user is using multiple browser windows, this could lead to race conditions.
  3. ViewState: this lands the value in the page itself. Downside is it is serialized to the client on every request.
  4. Context.Items: this does not live across requests, but it is handy for things like HTTP modules where you can “pass the buck” down the request pipeline.

In any case, wrapping the property in a real Property is the way to go as you can change the “backing” without effecting client code.

Thank you all for the assistance. :slight_smile: