ASP.NET: How To Easily Reference MasterPage Members
I’ll lay it out very simply:
- On your .aspx page, include the following:
<%@ MasterType virtualpath="~/myMasterPage.master" %> - To reference the members, simply type
Master.CustomerorMaster.YourProperty
That’s it! It’s better than casting a new master page object for every function, that’s for sure.
NOTE: Content pages load BEFORE master pages, so if you’re going to reference something on your master page, be sure you don’t need it during your content page’s Page_Load event!
RSS Feed
[...] I created this abstraction very easily in about five minutes in an application that was using MasterPages already, so it’s not hard to retrofit if you need to. Remember also that a child can only reference its direct parent (and that MasterPages render LAST!), so if you need pass info to your wrapper MasterPage, create properties in the MasterPages up the line and pass them along in the Page_Load subroutine (don’t forget to set up MasterPage referencing). [...]
Pingback by ASP.NET, PayPal, and Google Checkout: The Solution! « .NET Discussion | September 28, 2007 |
[...] read my articles ASP.NET: How To Easily Reference MasterPage Members and my ASP.NET and AJAX: So Freaking Easy series for more tips on how to get yourself set up even [...]
Pingback by ASP.NET: How to Convert Your Site To A MasterPage Site in 3 Steps « .NET Discussion | January 11, 2008 |
Hi
How can i reference an item on a MasterPage from another Form using VS 2005.
LIKE……………
IF Masterpage.lblName.visible = TRUE Then
btnSave.visible = TRUE
end if
Please Help
Etienne
Etienne –
You must remember that your MasterPage gets rendered AFTER your .aspx page, so what you’re doing may not be possible. The only thing I can think of is to do
Dim ctl As Label = Master.FindControl(“lblName”)
If Master.Controls(Master.Controls.IndexOf(ctl)).Visible() Then
‘do code here
End If
Try that and see if it works. I haven’t tested it, but it seems like it should.
Hope that helps!
Hi. thank you so much for the reply!!
I appreciate it!
But the .Visible() part in the If statement does not work.
“Visible is not a member of Integer”…thats the error
This is what i am doing.
Dim ctl As Label = Master.FindControl(“lblName”)
If Master.Controls(Master.Controls.IndexOf(ctl).visible() THEN
btnSave.visible = true
End If
Try:
Dim ctl As Label = Master.FindControl(”lblName”)
If ctl.visible() THEN
btnSave.visible = true
End If
Well it does work.
But this is what i am doing……….
My Page loads and when i Login then on my MasterPage some Label properties are set to True…Like “Welcome Etienne”.
But if you are not logged in the propery of that label is set to False.
But now once you are logged in i want to be able to Make some Buttons on some of my forms visible. So if the “Welcome Etienne” lable is visible i want to change other things on other forms.
Hope you understand what i mean?
Etienne
It works meaning…….if you have a label on your Masterpage and the property is set to TRUE….but if its false and your make it TRUE by an event in code, the code you gave me does not work.
Etienne –
Are you using authentication to log in? If so, you can achieve whatever effect you’re looking to accomplish by using:
If User.IsAuthenticated Then
Also, you should not base what happens in your .aspx page on the status of controls in your Masterpage since it gets rendered AFTER your .aspx page. Basically, all the code in your .aspx.vb (or .cs) page runs before your .master page.
Yes i am using Authentication. But the User.IsAuthenticated does not work..
Must i replace the User part with somehting ells then?
I want to make some buttons visible as soon as anyone is Logged in.
Etienne
‘boolean, returns true if login successful, false if not
Private Function authenticate(ByVal uname As String, ByVal password As String) As Boolean
‘Rater check for email and password……………………………………………………………………..
Try
Dim checkList As List(Of List(Of String)) = DBFunctions.queryRows(“SELECT COUNT(Email) FROM Rater WHERE Email = ‘” + uname + “‘ and Password = ‘” + password + “‘”)
If checkList(0)(0) = “1″ Then
Return True
End If
Catch ex As Exception
End Try
‘Product check for email and password…………………………………………………………………..
Try
Dim checkList As List(Of List(Of String)) = DBFunctions.queryRows(“SELECT COUNT(Email) FROM Product WHERE Email = ‘” + uname + “‘ and Password = ‘” + password + “‘”)
If checkList(0)(0) = “1″ Then
Return True
End If
Catch ex As Exception
End Try
Return False
End Function
Protected Sub dlgLogin_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles dlgLogin.Authenticate
e.Authenticated = authenticate(dlgLogin.UserName, dlgLogin.Password)
End Sub
YAY……..i found the solution.
If Not Session(“Email”) Is Nothing Then
btnUpdate.visible = true
End If
Thank you so much for your help, you made me understood how things work!
Will keep your site on my Favorite list.
Might need some more help with other topics later!!
Etienne
Etienne –
Glad you figured it out!
Now how do i created a MessageBox in ASP.NET?
When a User click’s on the “Delete Profile” link i want a MessageBox to pop up that will ask the User….”Are you sure you want to Delete your Profile?” OK CANCEL
Looks like its not as easy to do so in ASP.NET compare to a regular enviroment.
Etienne –
MessageBoxes are impossible to make in ASP.NET, despite being possible in VB/C# client programming. However, if you download the AJAX Toolkit (The CodePlex link on the right under “Resources”), there’s a message box link extender control that you can use to hook up to a link/button/etc. that you can customize and get a yes/no confirmation from your client before action is taken.
Hey
I do have the AJAX Control Toolkit………but where will i find this “(The CodePlex link on the right under “Resources”)”.
Sorry i am kinda new with ASP.NET.
Etienne
Etienne –
On the right side of my blog, there’s a section called “Resources”. Click the “CodePlex” link.
But i all ready have it installed….can you give me an example of code that i need to use now to get a Messagebox when one click on a Button.
Etienne
Please Help.
Etienne –
Please see this link: ConfirmButton Demonstration
WOW………thanks thats a great link!!
Hey
I would like to have a Page where people can leave Comments just like this one. Can you please be so kind and give me an example?
Please
Etienne
Etienne –
I am using WordPress as my blog engine. There are several places you can go to use a free blog engine, such as http://www.wordpress.com (or .org), http://www.blogengine.net (a .NET engine), http://www.blogger.com, and several others.
Well….i dont want to use a free blog engine. I want to implement the code to do what you are doing on this Page into my own website.
Your best bet then is to download the BlogEngine.NET software and pick apart the comments section of it. Honestly I haven’t done anything with the programming of this site… I would prefer a blog that just works so that I may focus on my job
You may also go to http://www.wordpress.org and download their software and see how they do it. I haven’t really looked at their code (or even know if it is open source, really) but maybe there will be something there for you.
Or simply come up with your own comment engine. I can’t imagine it would be too much work. Either save the comments in an XML file or in a database and connect them to the ID of the post they are submitted to.
K thanks
Will see what i can do.
Maybe you can give me advise on something ells then.
I have a Image Button on my form.
In the Properties Window i set the Postback Url to http://www.iafrica.com
But when i click on the ImageButton the iafrica website open in the same window.
What must i do so that it will open in a new window or new Tab?
So far as I know, you can’t open another window from the server side. I wish someone would tell me how to do this, because I don’t know. Your best bet is to create a javascript function and attach it to the OnClientClick event of the button.
YaY i figured it out…This is how its done….
function Navigate()
{
javascript:window.open(“http://www.iafrica.com”);
}
And then on the ImageButton OnClientClick u say..Navigate()
Etienne
i have created the master page and included that page in the default.aspx page. i have used only one contentplaceholder in the master page. while run the project i got an error like this
Error: you can have only one head runat server in the content page.
pls give me the solution for this.
Prem –
It sounds like your masterpage has a element and your default.aspx has a element. You can only have one. Take one out and try again.
i found the solutions for this while i removed the html,body and form tags in the default.aspx. its running i didnt get any errors but again i inserted these tags i got an error.the only problem i have faced that i cant able to insert style sheet link in the default page when i removed the tags like html ,body and form
Prem –
I’m so sorry… I just looked at my last comment and saw that WP didn’t convert my symbols… You need to get rid of your HEAD elements if you have more than one, but you absolutely need only one as well as one HTML and BODY tag.
hi
In my website i want to create the online ordering page for my products. if my clients register and order for my products. i want to see that orders in my site.
this is my concept.how can i do this. i dont know the codings for this pls help me
Prem –
I would suggest that you do some research and learn how to code it or purchase a pre-built shopping cart. I feel like dropping the code for a site-wide shopping cart/order tracking system in a comment is probably not the best place for that. There are many good carts out there, and a simple Google search should yield several high-quality results.
Good luck!