UpdatePanelUpdateMode 列舉

定義

表示 UpdatePanel 控制項之內容的可能更新模式。

public enum class UpdatePanelUpdateMode
public enum UpdatePanelUpdateMode
type UpdatePanelUpdateMode = 
Public Enum UpdatePanelUpdateMode
繼承
UpdatePanelUpdateMode

欄位

Always 0

UpdatePanel 控制項的內容會針對來自網頁的所有回傳而更新。 這包括非同步回傳。

Conditional 1

指定更新 UpdatePanel 控制項內容的條件數目;請參閱<備註>一節以取得詳細資訊。

範例

下列範例會宣告兩個 UpdatePanel 控制項。 第一個面板會將 UpdatePanel.UpdateMode 屬性設定為 Conditional 。 第二個面板預設已 UpdatePanel.UpdateMode 設定為 Always 。 這兩個面板外的按鈕會使用 ScriptManager.RegisterAsyncPostBackControl 方法註冊為非同步回傳控制項。 在按鈕的 Click 事件處理常式中, UpdatePanel.Update 如果自上次更新以來超過五秒,就會呼叫第一個面板的 方法。 在此案例中,只有在上次面板更新超過五秒前,面板的內容才會更新。 第二個面板的內容一律會更新。


<%@ 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>

備註

列舉 UpdatePanelUpdateMode 是由 UpdatePanel.UpdateMode 屬性使用,並定義控制項內容的 UpdatePanel 可能更新模式。 控制項 UpdatePanel 要求 ScriptManager.EnablePartialRendering 屬性必須 true 進行部分頁面轉譯。

屬性的 UpdatePanel.UpdateMode 預設值為 Always

UpdatePanel如果控制項位於另一個 UpdatePanel 控制項內,且父面板已更新,則不論屬性值為何 UpdateMode ,巢狀面板也會更新。

Conditional 會在下列情況下更新 控制項的內容 UpdatePanel

  • 系統會 UpdatePanel.Update 明確呼叫 方法。

  • 控制項會使用 UpdatePanel.Triggers 屬性定義為觸發程式,並造成回傳。 在此案例中,控制項是更新面板內容的明確觸發程式。 觸發程式控制項可以是定義觸發程式的控制項內部或外部 UpdatePanel

  • 屬性 UpdatePanel.ChildrenAsTriggers 設定 true 為 ,而 控制項的 UpdatePanel 子控制項會導致回傳。 在此案例中,控制項的 UpdatePanel 子控制項是用於更新面板的隱含觸發程式。 巢狀 UpdatePanel 控制項的子控制項不會造成外部 UpdatePanel 控制項更新,除非它們明確定義為觸發程式。

適用於

另請參閱