Share via


Using the ASP.NET UpdatePanel Control with Master Pages

Any ASP.NET page that includes an UpdatePanel control also requires a ScriptManager control. To use UpdatePanel controls with master pages, you can put a ScriptManager control on the master page. In this scenario, the master page provides a ScriptManager control for every content page. If you do not want partial-page updates enabled for individual content pages, you can disable partial-page updates for those pages.

Alternatively, you can put the ScriptManager control on each content page. You might do this if only some of the content pages will contain UpdatePanel controls.

Prerequisites

To implement the procedures in your own development environment you need:

  • Visual Web Developer Express or Microsoft Visual Studio 2005.

  • An AJAX-enabled ASP.NET Web site.

To add an UpdatePanel control to a content page

Refreshing the UpdatePanel from the Master Page

In this part of the tutorial you will add controls to the master page that cause an asynchronous postback and then refresh the UpdatePanel control on the content page.

To enable partial-page updates for all content pages

  1. In the master page, switch to Design view.

  2. Add text and two buttons after the ScriptManager control on the master page.

  3. Set the ID of one button to DecrementButton and its Text value to "-". Set the ID of the other button to IncrementButton and set its Text value to "+".

    UpdatePanel Tutorial

    The buttons will increment and decrement the selected date on the calendar in the content page.

  4. Select the + (plus) button, and then in the toolbar of the Properties window, click the Events button and enter MasterButton_Click in the Click box.

    UpdatePanel Tutorial

  5. Repeat the previous step for the - (minus) button.

  6. Double-click the page outside the controls to create a Page_Load event handler.

  7. Add the following code in the handler to register the two buttons as asynchronous postback controls:

    ProtectedSub Page_Load(ByVal Sender AsObject, ByVal E As EventArgs)
        ScriptManager1.RegisterAsyncPostBackControl(DecrementButton)
        ScriptManager1.RegisterAsyncPostBackControl(IncrementButton)
    EndSub
    
    protectedvoid Page_Load(object sender, EventArgs e)
    {
        ScriptManager1.RegisterAsyncPostBackControl(DecrementButton);
        ScriptManager1.RegisterAsyncPostBackControl(IncrementButton);
    }
    
  8. Add the following code to create a Click handler named MasterButton_Click:

    ProtectedSub MasterButton_Click(ByVal Sender AsObject, ByVal E As EventArgs)
    
        SelectCase Sender.ID
    
            Case"IncrementButton"Me.Offset = Me.Offset + 1
            Case"DecrementButton"Me.Offset = Me.Offset - 1
        EndSelectDim upl As UpdatePanel
        upl = ContentPlaceHolder1.FindControl("UpdatePanel1")
        upl.Update()
        Dim cal As Calendar
        cal = ContentPlaceHolder1.FindControl("Calendar1")
        Dim newDateTime As DateTime
        newDateTime = DateTime.Today.Add(New TimeSpan(Offset, 0, 0, 0))
        cal.SelectedDate = newDateTime
    EndSub
    
    protectedvoid MasterButton_Click(object sender, EventArgs e)
    {   
        switch (((Control)sender).ID)
        {
            case"IncrementButton":
                this.Offset = this.Offset + 1;
                break;
            case"DecrementButton":
                this.Offset = this.Offset - 1;
                break;
        }
        ((UpdatePanel)ContentPlaceHolder1.FindControl("UpdatePanel1")).Update();
        Calendar cal = ((Calendar)ContentPlaceHolder1.FindControl("Calendar1"));
        DateTime newDateTime = DateTime.Today.Add(new TimeSpan(Offset, 0, 0, 0));
        cal.SelectedDate = newDateTime;
    }
    
  9. Add the following code to create a public property named Offset in the master page that tracks the difference between today's date and the selected date.

    PublicProperty Offset() AsIntegerGetIf ViewState("Offset") IsNothingThenReturn 0
            Else : Return ViewState("Offset")
            EndIfEndGetSet(ByVal value AsInteger)
            ViewState("Offset") = value
        EndSetEndProperty
    
    public Int32 Offset
    {
        get
        { return (Int32)(ViewState["Offset"] ?? 0);}
        set
        { ViewState["Offset"] = value;}
    }
    
  10. In the content page, switch to Design view and then double-click the Calendar control to create an event handler for the SelectionChanged event.

  11. Add the following code to the SelectionChanged event handler to set the Offset property when the user selects a date.

    ProtectedSub Calendar1_SelectionChanged(ByVal Sender AsObject, ByVal E As EventArgs)
        Dim selectedDate As DateTime
        selectedDate = Calendar1.SelectedDate
        Master.Offset = _
           Calendar1.SelectedDate.Subtract(DateTime.Today).Days
    
    EndSub
    
    protectedvoid Calendar1_SelectionChanged(object sender, 
        EventArgs e)
    {
        DateTime selectedDate = Calendar1.SelectedDate;
        Master.Offset =
           ((TimeSpan)Calendar1.SelectedDate.Subtract(
           DateTime.Today)).Days;
    }
    
  12. Switch to the content page and add a Page_Load event handler that sets the selected date of the calendar to the current date.

    ProtectedSub Page_Load(ByVal Sender AsObject, ByVal E As EventArgs)
        Dim newDateTime As DateTime
        newDateTime = DateTime.Today.Add(New TimeSpan(Master.Offset, 0, 0, 0))
        Calendar1.SelectedDate = newDateTime
    EndSub
    
    protectedvoid Page_Load(object sender, EventArgs e)
    {
        DateTime newDateTime = 
            DateTime.Today.Add(new 
            TimeSpan(Master.Offset, 0, 0, 0));
        Calendar1.SelectedDate = newDateTime;
    }
    
  13. Add an @ MasterType directive to the page so that you can refer to the master page Offset property as a strongly typed property.

    <%@ MasterType VirtualPath="MasterPage.master" %>
    
    <%@ MasterType VirtualPath="MasterPage.master" %>
    
  14. In the content page, switch to Design view and select the UpdatePanel control.

  15. In the Properties window, set the UpdatePanelUpdateMode property to Conditional.

    UpdatePanel Tutorial

  16. Save your changes and then press CTRL+F5 view the page in a browser.

  17. Click the next and previous month controls on the calendar.

    The calendar changes without refreshing the whole page.

  18. Select a date on the calendar and then click the buttons on the master page to change the selected date.

    These changes occur without refreshing the whole page.

    The example is styled to better show the region of the page that the UpdatePanel control represents.

    <%@ Page Language="VB" MasterPageFile="MasterPage.master" Title="UpdatePanel in Master Pages" %>
    <%@ MasterType VirtualPath="MasterPage.master" %>
    
    <script runat="server">
        ProtectedSub Page_Load(ByVal Sender AsObject, ByVal E As EventArgs)
            Dim newDateTime As DateTime
            newDateTime = DateTime.Today.Add(New TimeSpan(Master.Offset, 0, 0, 0))
            Calendar1.SelectedDate = newDateTime
        EndSubProtectedSub Calendar1_SelectionChanged(ByVal Sender AsObject, ByVal E As EventArgs)
            Dim selectedDate As DateTime
            selectedDate = Calendar1.SelectedDate
            Master.Offset = _
               Calendar1.SelectedDate.Subtract(DateTime.Today).Days
    
        EndSub
    </script>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        Content Page<br />
        <asp:UpdatePanel id="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
            <fieldset>
            <legend>UpdatePanel</legend>
               <asp:Calendar id="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar> 
            </fieldset>
            </ContentTemplate>
        </asp:UpdatePanel>
    </asp:Content>
    
    <%@ Page Language="C#" MasterPageFile="MasterPage.master" Title="UpdatePanel in Master Pages" %>
    <%@ MasterType VirtualPath="MasterPage.master" %>
    
    <script runat="server">
        protectedvoid Page_Load(object sender, EventArgs e)
        {
            DateTime newDateTime = 
                DateTime.Today.Add(new 
                TimeSpan(Master.Offset, 0, 0, 0));
            Calendar1.SelectedDate = newDateTime;
        }
        protectedvoid Calendar1_SelectionChanged(object sender, 
            EventArgs e)
        {
            DateTime selectedDate = Calendar1.SelectedDate;
            Master.Offset =
               ((TimeSpan)Calendar1.SelectedDate.Subtract(
               DateTime.Today)).Days;
        }
    </script>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        Content Page<br />
        <asp:UpdatePanel id="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
            <fieldset>
            <legend>UpdatePanel</legend>
               <asp:Calendar id="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar> 
            </fieldset>
            </ContentTemplate>
        </asp:UpdatePanel>
    </asp:Content>
    
    <%@ Master Language="VB" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
        PublicProperty Offset() AsIntegerGetIf ViewState("Offset") IsNothingThenReturn 0
                Else : Return ViewState("Offset")
                EndIfEndGetSet(ByVal value AsInteger)
                ViewState("Offset") = value
            EndSetEndPropertyProtectedSub MasterButton_Click(ByVal Sender AsObject, ByVal E As EventArgs)
    
            SelectCase Sender.ID
    
                Case"IncrementButton"Me.Offset = Me.Offset + 1
                Case"DecrementButton"Me.Offset = Me.Offset - 1
            EndSelectDim upl As UpdatePanel
            upl = ContentPlaceHolder1.FindControl("UpdatePanel1")
            upl.Update()
            Dim cal As Calendar
            cal = ContentPlaceHolder1.FindControl("Calendar1")
            Dim newDateTime As DateTime
            newDateTime = DateTime.Today.Add(New TimeSpan(Offset, 0, 0, 0))
            cal.SelectedDate = newDateTime
        EndSubProtectedSub Page_Load(ByVal Sender AsObject, ByVal E As EventArgs)
            ScriptManager1.RegisterAsyncPostBackControl(DecrementButton)
            ScriptManager1.RegisterAsyncPostBackControl(IncrementButton)
        EndSub
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>UpdatePanel in Master Pages</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            Master Page<br />
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            Change date &nbsp;
            <asp:Button ID="DecrementButton" runat="server" Text="-" OnClick="MasterButton_Click" />
            <asp:Button ID="IncrementButton" runat="server" Text="+" OnClick="MasterButton_Click" />
            <hr />
            <br />
            <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
            </asp:contentplaceholder>
        </div>
        </form>
    </body>
    </html>
    
    <%@ Master Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
        public Int32 Offset
        {
            get
            { return (Int32)(ViewState["Offset"] ?? 0);}
            set
            { ViewState["Offset"] = value;}
        }
        protectedvoid MasterButton_Click(object sender, EventArgs e)
        {   
            switch (((Control)sender).ID)
            {
                case"IncrementButton":
                    this.Offset = this.Offset + 1;
                    break;
                case"DecrementButton":
                    this.Offset = this.Offset - 1;
                    break;
            }
            ((UpdatePanel)ContentPlaceHolder1.FindControl("UpdatePanel1")).Update();
            Calendar cal = ((Calendar)ContentPlaceHolder1.FindControl("Calendar1"));
            DateTime newDateTime = DateTime.Today.Add(new TimeSpan(Offset, 0, 0, 0));
            cal.SelectedDate = newDateTime;
        }
        protectedvoid Page_Load(object sender, EventArgs e)
        {
            ScriptManager1.RegisterAsyncPostBackControl(DecrementButton);
            ScriptManager1.RegisterAsyncPostBackControl(IncrementButton);
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>UpdatePanel in Master Pages</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            Master Page<br />
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            Change date &nbsp;
            <asp:Button ID="DecrementButton" runat="server" Text="-" OnClick="MasterButton_Click" />
            <asp:Button ID="IncrementButton" runat="server" Text="+" OnClick="MasterButton_Click" />
            <hr />
            <br />
            <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
            </asp:contentplaceholder>
        </div>
        </form>
    </body>
    </html>
    

Disabling Partial-Page Updates for a Content Page

If you add a ScriptManager control to a master page, by default partial-page updates are enabled for all content pages. If do not want to enable partial updates for an individual content page, you can disable that capability. You might do this is if your content page contains custom controls that are not compatible with partial-page updates.

To disable partial-page updates for a content page

Review

This tutorial shows how to use an UpdatePanel control in master pages. If there is a ScriptManager control on the master page, you can use UpdatePanel controls on content pages without declaring a ScriptManager control in the content page.

To disable partial-page rendering for an individual content page whose master page has partial-page rendering enabled, programmatically disable partial-page rendering for the content page.

See Also

Tasks

Introduction to the UpdatePanel Control

Creating a Simple ASP.NET Page with Multiple UpdatePanel Controls

Concepts

ASP.NET Master Pages Overview

Using the ASP.NET UpdatePanel Control with Data-Bound Controls

Reference

UpdatePanel

ScriptManager