Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing a master page's variable from a user control

Status
Not open for further replies.

glyns

Programmer
Dec 15, 2008
125
GB
This is my master page
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class MasterPages_mymasterpage : System.Web.UI.MasterPage
{
  protected void Page_Load(object sender, EventArgs e)
  {
  }

  public string xyz
  {
    get { return "xyz"; }
  }
}
The code behind my default.aspx page is
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      string a = "";
      a = ((MasterPages_mymasterpage)this.Master).xyz;
The reference "MasterPages_mymasterpage" keeps appearing and disappearing out of the autocomplete context dropdown and has now stopped appearing all together.

I've tried adding
MasterPageFile = "~/MasterPages/mymasterpage.master";
to the c# and
<%@ MasterType VirtualPath="~/MasterPages/mymasterpage.master"%>
to the aspx file, but neither has any effect
Anybody have any ideas why MasterPages_mymasterpage keeps dropping out or knows of some better way to reference a variable in a master page from a sub page?

Also, I need it in a user control eventually, should the code that fixes default.aspx be used for mycontrol.ascx??


Thanks
 
I should, but for some reason it doesn't have xyz after "this.master." [surprise]

I think it might have something to do with the masterpage being built/compiled incorrectly as it works sometimes and "((MasterPages_mymasterpage)this.Master).xyz" is error free but at the moment, it says it "can nt be found"

I've put it into a new blank project and it doesn't work properly in there either :-(
 
Try adding this in your HTML of the page referecning the master page:
Code:
<%@ MasterType VirtualPath="~/PATH TO YOUR MASTER PAGE HERE" %>
 
Tried that one and variations on a theme with typename, that didn't work either :S
 
I have to ask, are you trying to reference a master page property from a page or a user control on the page?
 
At the moment I'm trying either. In the end I want it in the user control but as that wasn't working I decided to try it in the page following an example I found on the net. That's pretty much what I've got now and that's the same code that is in the other examples I found.

When copy/pasting it in originally the masterpage name "MasterPages_mymasterpage" kept appearing and disappearing from the context list in the webpage. I've tried recreating the sample site from scratch but the ways I've tried to build it won't get it to appear in the context list at all now.

 
A moment of madness/inspiration had me renaming the class

public partial class MasterPages_mymasterpage : System.Web.UI.MasterPage

public partial class abc : System.Web.UI.MasterPage

This popped up a little box saying
"Options to update references"
Clicking "Rename 'MasterPages_mymasterpage' to 'abc' and then renaming it back fixed it

Looks like that one of the references had got out of sync somewhere.

Now
a = ((MasterPages_mymasterpage)this.Master).xyz;
works in the webpage :)

So I'm just left with the problem I had before this problem, how to reference the master page on the user control so that
a = ((MasterPages_mymasterpage)this.Master).xyz;
works on the user control
 
I think I'm misunderstanding the masterpage/page relationship
according to

"we shouldn’t treat master pages as the "masters", but as just another control inside the page" which ties in to the problem I've having :-(

So I think I can't do what I'm trying to do, I'll have to go to plan B

Thanks for the help
 
What you should be doing is creating a public property on the usercontrol. Then from the master page, set the property value.
 
This is the way I have done it in the past. In the user control markup:
Code:
<%@ Reference Control="~ Path and masterpage.master HERE" %>
Then in the code behind of the user control:
Code:
Dim p As master page class here = CType(Page.Master, master page class here)
Dim s As String = p.property here
I wrote the code in VB, I am not a C# person, but you can easily convert it.

Hope it helps

Jim
 
Yeah that ties in with what I've found in other places. Think I'm going to have to have a rethink about all this
Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top