Is there a way to get access to a puplic property on a user control from a nested master page?
Let me explain further
I have 3 deep master pages
global.master
LargeTopNav.master (inherits global.master)
LargeTopNav25-50-25.master (inherits LargeTopNav.master)
feature.aspx - This page is where I would like to access a custom user control I have on LargeTopNav.master to be able to set a property.
I'm fairly new to .net so any help is appreciated.
There is a MasterType property you could set on the feature.aspx page like e.g.:
<%# MasterType VirtualPath="~/masters/LargeTopNav.master" %>
Then, in turn, provide access to the control's property through a property you create in your LargeTopNav.master master page class:
public partial class LargeTopNavMaster : MasterPage
{
// ...
public string ThePropertyOfTheContainedControl
{
get { return MyContainedControl.TheProperty; }
set { MyContainedControl.TheProperty = value; }
}
// ...
}
Last, in your feature.aspx page, access the property of the master page that provides access to the underlying control:
public partial class Feature : Page
{
// ...
protected void Page_Load( object sender, EventArgs e )
{
Master.ThePropertyOfTheContainedControl = "Some nice text.";
}
// ...
}
Generally what I do in this scenario is make your master page implement an interface (which will have the property for your user control), and then from your page feature.aspx use this.Master (or this.Master.Master, make sure to check for null) to get a reference to the master page.
Then, just typecast the master page to your interface, and access the property.
Related
Apologies if this is too trivial but I seem to be running into an issue where I cannot access an object from a nested master page.
I'm having trouble passing down a User object from my main master to my nested master. By the time it gets to my nested master page it's null. I don't ever remember having trouble getting master pages talking but in this instance it just doesn't want to work.
So the set up, Some code omitted:
public partial class RWCMSMain : System.Web.UI.MasterPage
{
public Business.BusinessEntities.User LoggedOnUser { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
//Get the user
var loggedOnUser = userRepo.GetUser(securityToken);
//Now set the property
this.LoggedOnUser = loggedOnUser;
}
}
If I was to access this from a content page that inherits the main master all is great but the idea was to have a master page that contains side bar.
Now my side.master inherits from main master:
public partial class RWCMSSide : System.Web.UI.MasterPage
{
//Create property for nested master
public Business.BusinessEntities.User LoggedOnUser { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
//Assign main masters user to the new property
//Master.LoggedOnUser is always null
this.LoggedOnUser = Master.LoggedOnUser;
}
}
You could argue that I could recreate the user on the nested master page but then that would create two hits to the database when it could be done as one.
Whilst debugging the main master the user exists, it's just not being passed down.
Any pointers would be greatly appreciated!
Regards,
Problem
The Master's Load is executed after the Child's Load:
...
Master page Init event.
Content page Init event.
Content page Load event.
Master page Load event.
Master page controls Load event.
...
That's is why the property is null.
Solution
1. You can set it in the Init event - protected void Page_Load(object sender, EventArgs e)... if it is not dependent on the page being loaded (as it is probably).
2. Or you can turn the property into the lazy property(no need for actual Lazy<T> if it is not (and it probably should not be) intended to be used in multithreaded code):
public partial class RWCMSMain : System.Web.UI.MasterPage
{
private Business.BusinessEntities.User m_LoggedOnUse;
public Business.BusinessEntities.User LoggedOnUser
{
get
{
if (this.m_LoggedOnUse == null)
this.m_LoggedOnUse = userRepo.GetUser(securityToken);
return this.m_LoggedOnUse;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
I need to be able to make an object that is instantiated in a ASP.NET Master Page to be visible to all of the pages in the application that are based on that Master page. At the moment I'm defining the object here:
public partial class Control : System.Web.UI.MasterPage
{
public User TheUser;
protected void Page_Load(object sender, EventArgs e)
... but I think that's where I'm going wrong. Can anyone help?
Are you trying to create a control that inherits from a MasterPage? What you really want to do is create a MasterPage and add a property to it:
public partial class MyMasterPage: System.Web.UI.MasterPage
{
public string MyProperty {get;set;}
}
Now, from a Page that uses that MasterPage, you can just reference the MasterPage and access its members directly:
protected void Page_Load(object sender, EventArgs e)
{
MyMasterPage m = Master as MyMasterPage;
string masterProperty = m.MyProperty;
}
You'd be better off creating a base page (that inherits from System.Web.UI.Page) that all your pages inherit from, add your TheUser object to the base page.
Please note, pages do not inherit their master pages.
I have a button (view state enabled) in Master web page and set it to visible=false in one of the child web pages. If a second child page is opened, the button state (visible=false) is not persisting.
It seems viewstate is only valid for one page and is not transferred to other web pages. Is there some kind of trick to make viewstate global for all web pages?
No, viewstate is page specific. You will need to use something like a session variable or a querystring parameter to pass your state between pages.
No, You cannot make view state global, they are page specific. I would suggest to use cookies if you really want to make it client side otherwise you can use session.
If you need to store on a "global" level, you should be using the Application State. You could also use Cache Object.
You may be wanting to pass values from one page to another, you can achieve this by using the Context object in combination with the Server.Transfer.
1) You need a public property on the source page returning the Value to pass
namespace SomeNameSpace
{
public partial class SourcePage: System.Web.UI.Page
{
public string ValueToPass
{
get
{
if (Context.Items["ValueToPass"] == null)
Context.Items["ValueToPass"] = string.Empty;
return (string)Context.Items["ValueToPass"];
}
set
{
Context.Items["ValueToPass"] = value;
}
}
........
}
}
2) Do a Server.Transfer(DestinationPage.aspx)
3) In the Page_Load event of the destination page
namespace SomeNameSpace
{
public partial class SourcePage: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var value = this.Context.Items["ValueToPass"];
}
}
}
Hope this helps
I have the master page code behind like this:
public partial class TheMasterPage : System.Web.UI.MasterPage
{
string test = null;
protected void Page_Init(object sender, EventArgs e)
{}
}
When I'm in the Page_Load function in the code behind of the content page, I don't see the variable test as being available. Am I declaring it wrong?
Thanks.
Your property should be public and should have get, set
public string test
{
get; set;
}
I already answered it a little early Accessing property of master page inside content page
Having a master Page is not the same as inheriting a page class. What you would want to do is have a public property on the master page, and then access that from the content page.
Problem:
A control that shows each user which quizzes they have passed out of a possible four.
My solution:
Create user control that lists the name of the quizzes and has a checkmark at the end of each quiz name that I would like to make visible when they pass a quiz.
The actual user control is inside of my master page.
From reading other posts, I understand that I need to make the image.visible property public in the control code behind. I have tried this several ways and haven't had much luck.
So how do I expose the .visible property of an image inside of my user control?
Thanks for any advice.
Try something like this:
public Boolean ImageIsVisible
{
set { this.yourImage.Visible = value; }
get { return this.yourImage.Visible; }
}
hree ways I can think of...
In the USER control, just set the image as public instead of default - private, but that exposes ALL the elements.
Another is to create a property at the user control level that passes on to the ex:
public Boolean ImgVisible
{
get { return this.YourImageControl.Visible; }
set { this.YourImageControl.Visible = value; }
}
Or, just create as a function in your user control...
public void ImgVisible( Boolean ShowIt )
{
this.YourImageControl.Visible = ShowIt;
}
Sorry, missed part about Master Page... As a web control, as long as control is visible from the IDE (visual designer) of your form, you can refer to it directly in the CONTROL's code-behind partial class definition by the explicit name reference...
public Boolean ImgVisible
{
get { return ImgControl.Visible; }
set { ImgControl.Visible = value; }
}
I'm having a problem accessing this on the content page. Is this right?
I went to codebehind on control and added the following property:
public Boolean ImgVisible
{
get { return this.imgModule1Passed.Visible; }
set { this. imgModule1Passed.Visible = value; }
}
on the content page code behind, I am using the following:
UserControl control = (usercontrol)this.Page.Master.FindControl("QuizzesPassed1");
Shouldn't I be able to find the property by:
Control.ImgVisible
?
I can't seem to find the control at all.