ASP.NET
  Home arrow ASP.NET arrow Page 3 - How to Use Master Pages (Conclusion)
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

How to Use Master Pages (Conclusion)
By: Murach Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2005-12-08

    Table of Contents:
  • How to Use Master Pages (Conclusion)
  • How to add content to a page
  • How to access master page controls from a content page
  • How to access a public property of the master page from a content page
  • How to use nested master pages
  • How to create nested master pages

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    How to Use Master Pages (Conclusion) - How to access master page controls from a content page


    (Page 3 of 6 )

    In many applications, you need to access one or more of the controls in a master page from one of the application’s content pages. For example, the master page shown earlier in this chapter has a label in the footer area that normally displays the number of days remaining until Halloween. But what if you want to display other information in this label when certain content pages are displayed?

    For example, when the user is shopping for products with the Order.aspx page, you may want to display the number of items currently in the shopping cart instead of the number of days left until Halloween. To do that, you can expose a master page control as a public property, and then access the property from the content page.

    How to expose a master page control as a public property

    The easiest way to access a control on a master page from a content page is to create a public property that provides access to the control. Figure 3-8 illustrates a code-behind file for a master page that shows you how to do that.

    First, you create a public property in the master page that identifies the control you want to be able to access. In this case, the property is a Label type, and the property is named MessageLabel. Then, you code get and set procedures for the property. Here, the get procedure returns the lblMessage label, and the set procedure assigns the property value to lblMessage.

    Please notice that I also added another If statement to the Page_Load procedure for the master page. Now, the lblMessage label is set to the number of days left until Halloween only if the value of the label’s Text property is empty. That way, if the content page has assigned a value to this label in its Page_Load procedure, the master page’s Page_Load procedure won’t overwrite the value. This works because the content page’s Page_Load procedure is called before the master page’s Page_Load procedure.

    The code-behind file for a master page that provides a public property (figure 3-8)

    Partial Class MasterPage
      Inherits System.Web.UI.MasterPage

      Public Property MessageLabel() As Label
        Get
          Return lblMessage
        End Get
        Set(ByVal value As Label)
          lblMessage = value
        End Set
      End Property

      Protected Sub Page_Load(ByVal sender As Object, _
          ByVal e As System.EventArgs) Handles Me.Load
        If lblMessage.Text = "" Then
          Dim iDaysUntil As Integer = DaysUntilHalloween()
          If iDaysUntil = 0 Then
            lblMessage.Text = "Happy Halloween!"
          ElseIf iDaysUntil = 1 Then
            lblMessage.Text = "Tomorrow is Halloween!"
          Else
            lblMessage.Text = "There are only " & iDaysUntil _
              & " days left until Halloween!"
          End If 
        End If
      End Sub

      Private Function DaysUntilHalloween() As Integer
        Dim dtmHalloween As Date = New DateTime(DateTime.Today.Year, 10, 31)
        If DateTime.Today > dtmHalloween Then
          dtmHalloween.AddYears(1)
        End If
        Dim tsTimeUntil As TimeSpan = dtmHalloween - DateTime.Today
        Return tsTimeUntil.Days 
      End Function

    End Class

    Description

    • A content page can access a control in the master page if you expose the control as a public property in the master page. To do that, you code a property procedure with get and set procedures.

    More ASP.NET Articles
    More By Murach Publishing


       · This article is an excerpt from the book "Murach’s ASP.NET 2.0 Upgrader’s Guide: VB...
     

    Buy this book now. This article is excerpted from chapter three of the book Murach’s ASP.NET 2.0 Upgrader’s Guide: VB Edition, written by Anne Boehm and Joel Murach (Murach, 2005; ISBN: 1-890774-36-7). Check it out today at your favorite bookstore. Buy this book now.

    ASP.NET ARTICLES

    - Developing a Mini ASP.NET AJAX Server Centri...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT