ASP.NET
  Home arrow ASP.NET arrow Page 4 - 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 a public property of the master page from a content page


    (Page 4 of 6 )

    Figure 3-9 shows how you can access a public property in a master page from a content page. As you can see in the top part of this figure, you use the MasterType directive in the aspx file of the content page to specify the name of the type used for the master page. The value you name in this directive specifies the type of the object returned by the content page’s Master property. So in this example, the Master property will return an object of type MasterPage. If you look at the class declaration in the previous figure, you’ll see that MasterPage is the name of the class that defines the master page.

    The second part of this figure shows two procedures from the code-behind file for the Order.aspx content page. As you can see, the Page_Load procedure calls a procedure named DisplayCartMessage. This procedure determines the number of items currently in the shopping cart and sets the Text property of the label exposed by the master page’s MessageLabel property accordingly. But note that no value is assigned to the message label if the shopping cart is empty. In that case, the Page_Load procedure for the master page will set the label to the number of days remaining until Halloween.

    Although using the MasterType directive in the content page’s aspx file makes it easier to access the properties of the master page, you should realize that this directive isn’t necessary. If you don’t specify the MasterType directive, the Master property will return an object of type Master. You can then cast this object to the actual type of your master page to access any properties you’ve created.

    For example, you could use code like this to assign text to the MessageLabel property:

    If Cart.Count = 1 Then
      CType(Me.Master, MasterPage).MessageLabel.Text _
        = "There is one item in your cart."
    ElseIf Cart.Count > 1 Then
      CType(Me.Master, MasterPage).MessageLabel.Text _
        = "There are " & Cart.Count & " items in your cart."
    End If

    Here, the Master object is cast to MasterPage so its MessageLabel property can be accessed. The purpose of the MasterType directive is to avoid this awkward casting.

    A portion of the Order.aspx page (figure 3-9) 

    <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Order.aspx.vb" Inherits="Order" %>

    <%@ MasterType TypeName="MasterPage" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="server">
    .
    .
    .
    </asp:Content>

    Two procedures from the code-behind file for the Order.aspx page

    Protected Sub Page_Load(ByVal sender As Object, _
           ByVal e As System.EventArgs) Handles Me.Load
      If Not IsPostBack Then
        ddlProducts.DataBind()
        Me.DisplayCartMessage()
      End If
      SelectedProduct = GetSelectedProduct()
      lblName.Text = SelectedProduct.Name
      lblShortDescription.Text = _
        SelectedProduct.ShortDescription
      lblLongDescription.Text = _
        SelectedProduct.LongDescription
      lblUnitPrice.Text = FormatCurrency(SelectedProduct.UnitPrice)
      imgProduct.ImageUrl = "Images\Products\" _
        & SelectedProduct.ImageFile
    End Sub

    Private Sub DisplayCartMessage()
      Dim Cart As SortedList = CType(Session("cart"), SortedList)
      If Not Cart Is Nothing Then
        If Cart.Count = 1 Then
          Me.Master.MessageLabel.Text _
            = "There is one item in your cart."
        ElseIf Cart.Count > 1 Then
          Me.Master.MessageLabel.Text _
            = "There are " & Cart.Count & " items in your cart."
        End If
      End If
    End Sub

    Description

    • The MasterType directive in an aspx file specifies the name of the master page type. If you include this directive in a content page, you can use the Master property in the code-behind file to access the exposed property of the master page.

    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 3 hosted by Hostway
    Stay green...Green IT