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.

Pré-requisitos

Para implementar os procedimentos no seu próprio ambiente de desenvolvimento você precisa:

  • Visual Web Developer Express Edition ou Microsoft Visual Studio 2005.

  • Um site da Web ASP.NET habilitado para AJAX.

To add an UpdatePanel control to a content page

  1. Create a new master page and switch to Design view.

  2. Na guia Extensões AJAX da caixa de ferramentas, clique duas vezes no controle ScriptManager para adicioná-lo à página.Make sure that you add the ScriptManager control outside the ContentPlaceHolder control.

  3. Fora o ContentPlaceHolder controle, adicione o texto página mestra.

  4. From the HTML tab of the toolbox, drag a Horizontal Rule element after the text.

  5. Create a content page for the master page.

    In Solution Explorer, right-click the name of the project, and then click Add New Item.In the Add New Item dialog box, select the Select master page check box, and then click OK.

  6. Dentro de Content controlar, digite o texto de página de conteúdo e em seguida, na caixa de ferramentas, adicione um UpdatePanel controle.

  7. Add a Calendar control inside the UpdatePanel control.

  8. Salve suas alterações e, em seguida, pressione CTRL + F5 para exibir a página em um navegador.

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

    The calendar changes without refreshing the whole page.Esse comportamento é esperado quando o calendário estiver dentro de um UpdatePanel controle em uma página que não está associada a um mestre de página.

    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" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        Content Page<br />
        <asp:UpdatePanel id="UpdatePanel1" >
            <contenttemplate>
            <fieldset>
            <legend>UpdatePanel</legend>
               <asp:Calendar id="Calendar1" ></asp:Calendar>
            </fieldset>
            </contenttemplate>
        </asp:UpdatePanel>
    </asp:Content>
    
    <%@ Page Language="C#" MasterPageFile="MasterPage.master" Title="UpdatePanel in Master Pages" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        Content Page<br />
        <asp:UpdatePanel id="UpdatePanel1" >
            <contenttemplate>
            <fieldset>
            <legend>UpdatePanel</legend>
               <asp:Calendar id="Calendar1" ></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 >
    
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head >
        <title>UpdatePanel in Master Pages</title>
    </head>
    <body>
        <form id="form1" >
        <div>
            Master Page<br />
            <hr />
            <asp:ScriptManager ID="ScriptManager1" >
            </asp:ScriptManager>
            <br />
            <asp:contentplaceholder id="ContentPlaceHolder1" >
            </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 >
    
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head >
        <title>UpdatePanel in Master Pages</title>
    </head>
    <body>
        <form id="form1" >
        <div>
            Master Page<br />
            <hr />
            <asp:ScriptManager ID="ScriptManager1" >
            </asp:ScriptManager>
            <br />
            <asp:contentplaceholder id="ContentPlaceHolder1" >
            </asp:contentplaceholder>
        </div>
        </form>
    </body>
    </html>
    

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. conjunto a ID de um botão para DecrementButton e seus Textvalor para "-". Defina a ID do Outros botão como IncrementButton e defina sua Textvalor para "+".

    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.

  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:

    Protected Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
        ScriptManager1.RegisterAsyncPostBackControl(DecrementButton)
        ScriptManager1.RegisterAsyncPostBackControl(IncrementButton)
    End Sub
    
    protected void 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:

    Protected Sub MasterButton_Click(ByVal Sender As Object, ByVal E As EventArgs)
    
        Select Case Sender.ID
    
            Case "IncrementButton"
                Me.Offset = Me.Offset + 1
            Case "DecrementButton"
                Me.Offset = Me.Offset - 1
        End Select
        Dim 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
    End Sub
    
    protected void 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.

    Public Property Offset() As Integer
        Get
            If ViewState("Offset") Is Nothing Then
                Return 0
            Else : Return ViewState("Offset")
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("Offset") = value
        End Set
    End Property
    
    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.

    Protected Sub Calendar1_SelectionChanged(ByVal Sender As Object, ByVal E As EventArgs)
        Dim selectedDate As DateTime
        selectedDate = Calendar1.SelectedDate
        Master.Offset = _
           Calendar1.SelectedDate.Subtract(DateTime.Today).Days
    
    End Sub
    
    protected void 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.

    Protected Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
        Dim newDateTime As DateTime
        newDateTime = DateTime.Today.Add(New TimeSpan(Master.Offset, 0, 0, 0))
        Calendar1.SelectedDate = newDateTime
    End Sub
    
    protected void 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. Na janela Propriedades, defina as propriedades UpdatePanelUpdateMode para Conditional.

  16. Salve suas alterações e, em seguida, pressione CTRL + F5 para exibir a página em um navegador.

  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 >
        Protected Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
            Dim newDateTime As DateTime
            newDateTime = DateTime.Today.Add(New TimeSpan(Master.Offset, 0, 0, 0))
            Calendar1.SelectedDate = newDateTime
        End Sub
        Protected Sub Calendar1_SelectionChanged(ByVal Sender As Object, ByVal E As EventArgs)
            Dim selectedDate As DateTime
            selectedDate = Calendar1.SelectedDate
            Master.Offset = _
               Calendar1.SelectedDate.Subtract(DateTime.Today).Days
    
        End Sub
    </script>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        Content Page<br />
        <asp:UpdatePanel id="UpdatePanel1"  UpdateMode="Conditional">
            <ContentTemplate>
            <fieldset>
            <legend>UpdatePanel</legend>
               <asp:Calendar id="Calendar1"  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 >
        protected void Page_Load(object sender, EventArgs e)
        {
            DateTime newDateTime = 
                DateTime.Today.Add(new 
                TimeSpan(Master.Offset, 0, 0, 0));
            Calendar1.SelectedDate = newDateTime;
        }
        protected void 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"  UpdateMode="Conditional">
            <ContentTemplate>
            <fieldset>
            <legend>UpdatePanel</legend>
               <asp:Calendar id="Calendar1"  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 >
        Public Property Offset() As Integer
            Get
                If ViewState("Offset") Is Nothing Then
                    Return 0
                Else : Return ViewState("Offset")
                End If
            End Get
            Set(ByVal value As Integer)
                ViewState("Offset") = value
            End Set
        End Property
        Protected Sub MasterButton_Click(ByVal Sender As Object, ByVal E As EventArgs)
    
            Select Case Sender.ID
    
                Case "IncrementButton"
                    Me.Offset = Me.Offset + 1
                Case "DecrementButton"
                    Me.Offset = Me.Offset - 1
            End Select
            Dim 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
        End Sub
        Protected Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
            ScriptManager1.RegisterAsyncPostBackControl(DecrementButton)
            ScriptManager1.RegisterAsyncPostBackControl(IncrementButton)
        End Sub
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" >
        <title>UpdatePanel in Master Pages</title>
    </head>
    <body>
        <form id="form1" >
        <div>
            Master Page<br />
            <asp:ScriptManager ID="ScriptManager1" >
            </asp:ScriptManager>
            Change date &nbsp;
            <asp:Button ID="DecrementButton"  Text="-" OnClick="MasterButton_Click" />
            <asp:Button ID="IncrementButton"  Text="+" OnClick="MasterButton_Click" />
            <hr />
            <br />
            <asp:contentplaceholder id="ContentPlaceHolder1" >
            </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 >
        public Int32 Offset
        {
            get
            { return (Int32)(ViewState["Offset"] ?? 0);}
            set
            { ViewState["Offset"] = value;}
        }
        protected void 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;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            ScriptManager1.RegisterAsyncPostBackControl(DecrementButton);
            ScriptManager1.RegisterAsyncPostBackControl(IncrementButton);
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" >
        <title>UpdatePanel in Master Pages</title>
    </head>
    <body>
        <form id="form1" >
        <div>
            Master Page<br />
            <asp:ScriptManager ID="ScriptManager1" >
            </asp:ScriptManager>
            Change date &nbsp;
            <asp:Button ID="DecrementButton"  Text="-" OnClick="MasterButton_Click" />
            <asp:Button ID="IncrementButton"  Text="+" OnClick="MasterButton_Click" />
            <hr />
            <br />
            <asp:contentplaceholder id="ContentPlaceHolder1" >
            </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

Revisão

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.

Consulte também

Tarefas

Introdução ao Controle UpdatePanel

Creating a Simple ASP.NET Page with Multiple UpdatePanel Controls

Conceitos

Visão Geral de Páginas Mestras ASP.NET

Usando o controle ASP.NET UpdatePanel com controles associado a dados

Referência

UpdatePanel

ScriptManager