Practical ASP.NET

Integrating Master Pages + Content Pages + a Little AJAX

Getting your content and master pages to work side-by-side is easy, thanks to UpdatePanel.

Sometimes you need to have code in your content page update controls in your master page. It's not hard to do and, thanks to the default behavior of the UpdatePanel, getting it to work in AJAX is a snap.

Master pages provide you with an easy-to-use mechanism for ensuring that all your pages have the same structure. Master pages also handle content common to all pages by ensuring the same thing appears in the same place on every page. Problems arise, however, when that "same thing" changes from one page to another -- that is, when you have page-specific content for every page which you still want to appear on the same place on every page.

As an example, assume that you want a label that displays a page title on every page. To have this page title appear in the same place on every page, you'll put the label control on your master page. Unfortunately, the text that's being displayed is controlled by the content page because only code in the page can provide a "business-friendly/human-readable" title.

The simplest way to implement this is to think of your master page as an object that you can extend with new members. For instance, after dropping a label to display the page title onto the master page, you can add this code to the master page's code file to update that label:

Public Property PageTitle() As String
Get
Return Me.PageTitleLabel.Text
End Get
Set(ByVal value As String)
Me.PageTitleLabel.Text = value
End Set
End Property

After doing a recompile, in any of your content pages you'll be able to use this new property. First, in the code file for a content page, retrieve a reference to the master page through the page's master property. Your new property won't appear in the IntelliSense list for the master page until you cast the master property to the type of your master page:

Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Dim mym As MyMaster

mym = CType(Me.Master, MyMaster)
mym.PageTitle = "Sales Information"

End Sub

If you're using properties on your master page frequently on your site, constantly writing this code will get very boring. You can simplify the code by adding a MasterType directive that references the master page in the content page's source view:

<%@ MasterType TypeName="MyMaster" %>

Now, from the content page's code you can access the members you added to the master without having to do a cast (again, after doing a recompile):

Me.Master.PageTitle = "Sales Information"

Incorporating AJAX
Let's go to a more complicated problem. Let's say that your content page is busy adding items to the user's shopping cart and you want to display the status of the user's shopping cart on each page. You'll want to put that shopping cart status display in the same place on every page but the changes to the shopping cart will be made by the content page.

If you're not using AJAX, you can use the solution I've already described: As the user adds items to the shopping card, the entire page will postback to the server, giving you a chance to update the master page property. But you've gone one step further and put all of the content page's control inside an UpdatePanel. As a result, only changes to the portion of the page inside the content page's UpdatePanel will be displayed.

The answer is: Use the solution I described previously, but put the shopping cart status control into an UpdatePanel also (I'm assuming that you've put a ScriptManager control on your site's master page so that it's available on every page.) Whenever the UpdatePanel in the content page is refreshed, the UpdatePanel on the master page will also be refreshed so -- provided you update the status value in your server-side code -- the new status value will be displayed.

This behavior is the result of two features. First, the default behavior for ASP.NET is to refresh all UpdatePanels on the page if any one of them is refreshed. Second, all of the UpdatePanel processing happens on the client, inside the browser. Once your page gets down to the browser, it's all just HTML. There are no ASP.NET master pages, no content pages, no user controls -- the browser just sees a single page of HTML. So, from the page's point of view, there isn't an UpdatePanel on the master page and an UpdatePanel on the content page: There's just two UpdatePanels and they'll be refreshed together.

See? Sometimes, a new feature actually makes your life simpler.

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube