Hi all, I have a requirement the properties of Master Page from an user control which is inside the aspx which user Master page. For this I know that i need to register the Master page inside the user control and I have done this long before. Now I forgot
as to how I did it and did not get anything after browsing for sometime. Now I am in a hurry completing a task and need it ASAP. Please help me
Regards,
Karthikeyan
Please remember to click "Mark as Answer" on the post that helps you.
Did u try it out before posting it? Cos the Master page's type (class name) itself isn't visible in the namespace. Then how could u cast it as u said?
I believe that the master page should be referenced in the user control's markup as i have already done something similar to it long back and forgot it completely and lost the code.
u people have any other idea?
Regards,
Karthikeyan
Please remember to click "Mark as Answer" on the post that helps you.
From your requirements I figure out that there is a Master Page (lets say MasterPage) and a Web Page (say Default.aspx) which is deriving from this MasterPage. This Default.aspx has a user control. Now you want to access a property of a MasterPage from this
user control.
Lets say in the MasterPage there is a property called name like
public string Name
{
get{return "ABC";}
}
Now you want to access this property from the UserControl.
For this purpose you'll first have to register the master page in the user control like this.
Now you'll first have to get the reference of the page this user control is residing in and then get the Master Page of that page. The code will be like this.
Bingo.. u got it. I've been trying the same thing for days in my solution but it did not work. I created a new website and tried again and now it works.
I gotta figure out why it does not work in the former one. My wild guess is that there could be some circular references(which we get frequently get) as the website solution is a big one and has around 85 folders and hundreds of pages and user controls
referenving each other.
Regards,
Karthikeyan
Please remember to click "Mark as Answer" on the post that helps you.
Gmercier, to avoid such kind of scenario you'll have to follow a different approach. Please create an abstract base class and put the properties you want to access from the User Control into that. And then from the User Control access the properties of the
base class. As we know from the Object Oriented principles that an instance of child class is also an instance of its base class :-)
Here I explain how.
Lets suppose we've an abstract base class like this
abstract
public
class
MyMasterPageBase:System.Web.UI.MasterPage
{
public
string Name
{
get
{
return
"ABC";
}
}
}
So this is a simple class, just one property returning a value. Now your master page should inherit from this class. Like
public
partial
class
MasterPage1 :
MyMasterPageBase
{
...
...
}
and now lets say you want to show that property value in a label control. Do something like this
MyMasterPageBase
masterPage1 = (MyMasterPageBase)this.Page.Master;
lbl1.Text=masterPage1.Title;
So here even if you override the properties in the MasterPage itself you'll be able to access them in the User Control.
karthipec_gm...
Participant
1158 Points
219 Posts
Access Master page properties from User Control
Feb 05, 2009 12:43 PM|LINK
Hi all, I have a requirement the properties of Master Page from an user control which is inside the aspx which user Master page. For this I know that i need to register the Master page inside the user control and I have done this long before. Now I forgot as to how I did it and did not get anything after browsing for sometime. Now I am in a hurry completing a task and need it ASAP. Please help me
Karthikeyan
Please remember to click "Mark as Answer" on the post that helps you.
Disclaimer:
All Code is provided AS IS
MetalAsp.Net
All-Star
112247 Points
18271 Posts
Moderator
Re: Access Master page properties from User Control
Feb 05, 2009 12:52 PM|LINK
is this what you're looking for:
<%
@ MasterType etc... %>http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx
karthipec_gm...
Participant
1158 Points
219 Posts
Re: Access Master page properties from User Control
Feb 05, 2009 01:10 PM|LINK
No dude. It only tells u how to access master page properties from "Page" and not from user controls
Karthikeyan
Please remember to click "Mark as Answer" on the post that helps you.
Disclaimer:
All Code is provided AS IS
ivanpro
Member
450 Points
70 Posts
Re: Access Master page properties from User Control
Feb 06, 2009 10:07 PM|LINK
You can do it using this simple code:
Master page (let's call it MyMaster) property:
public bool dispaySomething
{
get { return something.Visible; }
set {something.Visible = value;}
}
Content page code to access master page property:
( (MyMaster)Page.Master).dispaySomething = false;
karthipec_gm...
Participant
1158 Points
219 Posts
Re: Access Master page properties from User Control
Feb 07, 2009 02:28 PM|LINK
Did u try it out before posting it? Cos the Master page's type (class name) itself isn't visible in the namespace. Then how could u cast it as u said?
I believe that the master page should be referenced in the user control's markup as i have already done something similar to it long back and forgot it completely and lost the code.
u people have any other idea?
Karthikeyan
Please remember to click "Mark as Answer" on the post that helps you.
Disclaimer:
All Code is provided AS IS
deepthoughts
Contributor
7288 Points
1051 Posts
Re: Access Master page properties from User Control
Feb 07, 2009 06:00 PM|LINK
From your requirements I figure out that there is a Master Page (lets say MasterPage) and a Web Page (say Default.aspx) which is deriving from this MasterPage. This Default.aspx has a user control. Now you want to access a property of a MasterPage from this user control.
Lets say in the MasterPage there is a property called name like
public string Name
{
get{return "ABC";}
}
Now you want to access this property from the UserControl.
For this purpose you'll first have to register the master page in the user control like this.
<%
@ Register TagPrefix="mp" TagName="MyMP" Src="~/MasterPage.master" %>Now you'll first have to get the reference of the page this user control is residing in and then get the Master Page of that page. The code will be like this.
System.Web.UI.Page page = (System.Web.UI.Page)this.Page; MasterPage1 mp1 = (MasterPage1)page.Master;Now you have the reference to the MasterPage. So you can access the properties of this Master Page (e.g. displaying it in a label)
lbl1.Text=mp1.Name;
Hope it helps.
karthipec_gm...
Participant
1158 Points
219 Posts
Re: Access Master page properties from User Control
Feb 08, 2009 08:26 AM|LINK
Bingo.. u got it. I've been trying the same thing for days in my solution but it did not work. I created a new website and tried again and now it works.
I gotta figure out why it does not work in the former one. My wild guess is that there could be some circular references(which we get frequently get) as the website solution is a big one and has around 85 folders and hundreds of pages and user controls referenving each other.
Karthikeyan
Please remember to click "Mark as Answer" on the post that helps you.
Disclaimer:
All Code is provided AS IS
kiran_forums...
Member
2 Points
1 Post
Re: Access Master page properties from User Control
Mar 22, 2009 05:08 AM|LINK
Thanks a lot for the answer. I was also looking for the same!
<@Reference is enough instead of Register
gmercier
Member
4 Points
6 Posts
Re: Access Master page properties from User Control
Jun 04, 2009 07:25 AM|LINK
That is working if the usercontrol is in an aspx page but that doesn't work if the usercontrol is directly in the masterpage.
In this last case, I get a "circular reference" compil error. Do you have any ideas ?
deepthoughts
Contributor
7288 Points
1051 Posts
Re: Access Master page properties from User Control
Jun 04, 2009 09:12 AM|LINK
Gmercier, to avoid such kind of scenario you'll have to follow a different approach. Please create an abstract base class and put the properties you want to access from the User Control into that. And then from the User Control access the properties of the base class. As we know from the Object Oriented principles that an instance of child class is also an instance of its base class :-)
Here I explain how.
Lets suppose we've an abstract base class like this
abstract
public class MyMasterPageBase:System.Web.UI.MasterPage{
public string Name{
get{
return "ABC";}
}
}
So this is a simple class, just one property returning a value. Now your master page should inherit from this class. Like
public
partial class MasterPage1 : MyMasterPageBase{
...
...
}
and now lets say you want to show that property value in a label control. Do something like this
MyMasterPageBase
masterPage1 = (MyMasterPageBase)this.Page.Master;lbl1.Text=masterPage1.Title;
So here even if you override the properties in the MasterPage itself you'll be able to access them in the User Control.
Hope it will be helpful.
Thanks.