UpdatePanel.UpdateMode Propiedad

Definición

Obtiene o establece un valor que indica cuándo se actualiza un control de contenido UpdatePanel.

public:
 property System::Web::UI::UpdatePanelUpdateMode UpdateMode { System::Web::UI::UpdatePanelUpdateMode get(); void set(System::Web::UI::UpdatePanelUpdateMode value); };
public System.Web.UI.UpdatePanelUpdateMode UpdateMode { get; set; }
member this.UpdateMode : System.Web.UI.UpdatePanelUpdateMode with get, set
Public Property UpdateMode As UpdatePanelUpdateMode

Valor de propiedad

Uno de los valores de UpdatePanelUpdateMode. De manera predeterminada, es Always.

Excepciones

El tipo especificado no es ninguno de los valores de UpdatePanelUpdateMode.

Ejemplos

En el ejemplo siguiente se declaran dos UpdatePanel controles. En el primer panel, la UpdateMode propiedad se establece en Conditional. En el segundo panel, UpdateMode se establece en Always. Un botón fuera de ambos paneles se registra como un control de postback asincrónico llamando al RegisterAsyncPostBackControl método del ScriptManager control. En el controlador de eventos del Click botón, se llama al Update método del primer panel si han transcurrido más de cinco segundos desde su última actualización. En este escenario, el contenido del panel solo se actualiza si la última actualización del panel fue hace más de cinco segundos. El contenido del segundo panel siempre se actualiza.


<%@ Page 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">
    protected DateTime LastUpdate
    {
        get
        {
            return (DateTime)(ViewState["LastUpdate"] ?? DateTime.Now);
        }
        set
        {
            ViewState["LastUpdate"] = value;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (LastUpdate.AddSeconds(5.0) < DateTime.Now)
        {
            UpdatePanel1.Update();
            LastUpdate = DateTime.Now;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        ScriptManager1.RegisterAsyncPostBackControl(Button1);   
        if (!IsPostBack)
        {
            LastUpdate = DateTime.Now;
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>UpdatePanelUpdateMode Example</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1"
                               runat="server" />
            <asp:Panel ID="Panel1"
                       GroupingText="UpdatePanel1"
                       runat="server">
                <asp:UpdatePanel ID="UpdatePanel1"
                                 UpdateMode="Conditional"
                                 runat="server">
                    <ContentTemplate>
                        <p>
                            The content in this UpdatePanel only refreshes if five or more
                            seconds have passed since the last refresh and the button in
                            UpdatePanel2 was clicked. The time is checked
                            server-side and the UpdatePanel.Update() method is called. Last
                            updated: <strong>
                                <%= LastUpdate.ToString() %>
                            </strong>
                        </p>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </asp:Panel>
            <asp:Panel ID="Panel2"
                       GroupingText="UpdatePanel2"
                       runat="server">
                <asp:UpdatePanel ID="UpdatePanel2"
                                 runat="server">
                    <ContentTemplate>
                        <p>
                            This UpdatePanel always refreshes if the button is clicked.
                            Last updated: <strong>
                                <%= DateTime.Now.ToString() %>
                            </strong>
                        </p>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </asp:Panel>
            <asp:Button ID="Button1" Text="Button1" runat="server" OnClick="Button1_Click" />
        </div>
    </form>
</body>
</html>

<%@ Page 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">
    Protected Property LastUpdate() As DateTime
        Get
            If ViewState("LastUpdate") IsNot Nothing Then
                Return ViewState("LastUpdate")
            Else : Return DateTime.Now()
            End If
        End Get
        Set(ByVal Value As DateTime)
            ViewState("LastUpdate") = Value
        End Set
    End Property

    Protected Sub Button1_Click(ByVal Sender As Object, ByVal E As EventArgs)
        If (LastUpdate.AddSeconds(5.0) < DateTime.Now) Then
            UpdatePanel1.Update()
            LastUpdate = DateTime.Now
        End If
    End Sub

    Protected Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
        ScriptManager1.RegisterAsyncPostBackControl(Button1)
        If Not IsPostBack Then
            LastUpdate = DateTime.Now
        End If
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>UpdatePanelUpdateMode Example</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1"
                               runat="server" />
            <asp:Panel ID="Panel1"
                       GroupingText="UpdatePanel1"
                       runat="server">
                <asp:UpdatePanel ID="UpdatePanel1"
                                   runat="server"
                                   UpdateMode="Conditional">
                    <ContentTemplate>
                        <p>
                            The content in this UpdatePanel only refreshes if five or more
                            seconds have passed since the last refresh and the button in
                            UpdatePanel2 was clicked. The time is checked
                            server-side and the UpdatePanel.Update() method is called. Last
                            updated: <strong>
                                <%= LastUpdate.ToString() %>
                            </strong>
                        </p>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </asp:Panel>
            <asp:Panel ID="Panel2"
                       GroupingText="UpdatePanel2"
                       runat="server">
                <asp:UpdatePanel ID="UpdatePanel2"
                                 runat="server">
                    <ContentTemplate>
                        <p>
                            This UpdatePanel always refreshes if the button is clicked.
                            Last updated: <strong>
                                <%= DateTime.Now.ToString() %>
                            </strong>
                        </p>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </asp:Panel>
            <asp:Button ID="Button1" Text="Button1" runat="server" OnClick="Button1_Click" />
        </div>
    </form>
</body>
</html>

Comentarios

Cuando un UpdatePanel control no está dentro de otro UpdatePanel control, el panel se actualiza según lo determinado por la configuración de las UpdateMode propiedades y ChildrenAsTriggers , junto con la colección de desencadenadores. Cuando un UpdatePanel control está dentro de otro UpdatePanel control, el panel secundario se actualiza automáticamente cuando se actualiza el panel primario.

El contenido de un UpdatePanel control se actualiza en las siguientes circunstancias:

  • Si la UpdateMode propiedad se establece Alwaysen , el UpdatePanel contenido del control se actualiza en cada postback que se origina desde cualquier lugar de la página. Esto incluye postbacks asincrónicos de controles dentro de otros UpdatePanel controles y postbacks de controles que no están dentro UpdatePanel de los controles.

  • Si el UpdatePanel control está anidado dentro de otro UpdatePanel control y se actualiza el panel de actualización primario.

  • Si la UpdateMode propiedad se establece Conditionalen y se produce una de las condiciones siguientes:

    • Se llama al Update método del UpdatePanel control explícitamente.

    • El postback se debe a un control definido como desencadenador mediante la Triggers propiedad del UpdatePanel control . En este escenario, el control desencadena explícitamente una actualización del contenido del panel. El control puede estar dentro o fuera del UpdatePanel control que define el desencadenador.

    • La ChildrenAsTriggers propiedad se establece en true y un control secundario del UpdatePanel control provoca un postback. Un control secundario de un control anidado UpdatePanel no provoca una actualización al control externo UpdatePanel a menos que se defina explícitamente como desencadenador.

Se aplica a

Consulte también