.NET Discussion

.NET Issues, Problems, Code Samples, and Fixes

ASP.NET: How To Easily Reference MasterPage Members


I’ll lay it out very simply:

  1. On your .aspx page, include the following: <%@ MasterType virtualpath="~/myMasterPage.master" %>
  2. To reference the members, simply type Master.Customer or Master.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!

Advertisements

August 16, 2007 - Posted by | ASP.NET, MasterPages, Tips & Tricks

35 Comments »

  1. […] 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 | Reply

  2. […] 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 | Reply

  3. 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

    Comment by Etienne | February 18, 2008 | Reply

  4. 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!

    Comment by Some.Net(Guy) | February 18, 2008 | Reply

  5. 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

    Comment by Etienne | February 19, 2008 | Reply

  6. Try:

    Dim ctl As Label = Master.FindControl(”lblName”)

    If ctl.visible() THEN
    btnSave.visible = true

    End If

    Comment by Some.Net(Guy) | February 19, 2008 | Reply

  7. 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

    Comment by Etienne | February 19, 2008 | Reply

  8. 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.

    Comment by Etienne | February 19, 2008 | Reply

  9. 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

    Comment by Some.Net(Guy) | February 19, 2008 | Reply

  10. 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.

    Comment by Some.Net(Guy) | February 19, 2008 | Reply

  11. 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

    Comment by Etienne | February 20, 2008 | Reply

  12. ‘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

    Comment by Etienne | February 20, 2008 | Reply

  13. 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

    Comment by Etienne | February 20, 2008 | Reply

  14. Etienne –

    Glad you figured it out!

    Comment by Some.Net(Guy) | February 20, 2008 | Reply

  15. 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.

    Comment by Etienne | February 21, 2008 | Reply

  16. 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.

    Comment by Some.Net(Guy) | February 21, 2008 | Reply

  17. 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

    Comment by Etienne | February 22, 2008 | Reply

  18. Etienne –

    On the right side of my blog, there’s a section called “Resources”. Click the “CodePlex” link.

    Comment by Some.Net(Guy) | February 22, 2008 | Reply

  19. 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

    Comment by Etienne | February 25, 2008 | Reply

  20. Please Help.

    Comment by Etienne | February 29, 2008 | Reply

  21. Etienne –

    Please see this link: ConfirmButton Demonstration

    Comment by Some.Net(Guy) | February 29, 2008 | Reply

  22. WOW………thanks thats a great link!!

    Comment by Etienne | March 3, 2008 | Reply

  23. 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

    Comment by Etienne | April 15, 2008 | Reply

  24. 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.

    Comment by Some.Net(Guy) | April 15, 2008 | Reply

  25. 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.

    Comment by Etienne | April 15, 2008 | Reply

  26. 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.

    Comment by Some.Net(Guy) | April 15, 2008 | Reply

  27. 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?

    Comment by Etienne | April 15, 2008 | Reply

  28. 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.

    Comment by Some.Net(Guy) | April 16, 2008 | Reply

  29. 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

    Comment by Etienne | April 16, 2008 | Reply

  30. 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.

    Comment by prem | August 8, 2008 | Reply

  31. 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.

    Comment by Some.Net(Guy) | August 8, 2008 | Reply

  32. 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

    Comment by prem | August 11, 2008 | Reply

  33. 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.

    Comment by Some.Net(Guy) | August 12, 2008 | Reply

  34. 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

    Comment by prem | August 19, 2008 | Reply

  35. 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!

    Comment by Some.Net(Guy) | August 19, 2008 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: