UpdatePanel.UpdateMode 屬性

定義

取得或設定值,這個值表示何時更新 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

屬性值

其中一個 UpdatePanelUpdateMode 值。 預設值為 Always

例外狀況

指定的類型不是其中一個 UpdatePanelUpdateMode 值。

範例

下列範例會宣告兩個 UpdatePanel 控制項。 在第一個面板中, UpdateMode 屬性會設定為 Conditional 。 在第二個面板中, UpdateMode 設定為 Always 。 兩個面板外的按鈕會藉由呼叫 RegisterAsyncPostBackControl 控制項的 ScriptManager 方法,註冊為非同步回傳控制項。 在按鈕的 Click 事件處理常式中, 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>

備註

當控制項不在另一個 UpdatePanelUpdatePanel 控制項內時,面板會依 和 ChildrenAsTriggers 屬性的 UpdateMode 設定以及觸發程式的集合來更新。 UpdatePanel當控制項位於另一個 UpdatePanel 控制項內時,子面板會在父面板更新時自動更新。

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

  • UpdateMode如果 屬性設定為 Always ,則 UpdatePanel 控制項的內容會在來自頁面上任何位置的每個回傳上更新。 這包括來自其他 UpdatePanel 控制項內的控制項的非同步回傳,以及來自不在控制項內 UpdatePanel 之控制項的回傳。

  • UpdatePanel如果控制項位於另一個 UpdatePanel 控制項內,且父更新面板已更新。

  • UpdateMode如果 屬性設定為 Conditional ,則會發生下列其中一個狀況:

    • 您可以明確呼叫 Update 控制項的 UpdatePanel 方法。

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

    • 屬性 ChildrenAsTriggers 設定 true 為 ,而 控制項的 UpdatePanel 子控制項會導致回傳。 巢狀 UpdatePanel 控制項的子控制項不會造成外部 UpdatePanel 控制項的更新,除非明確定義為觸發程式。

適用於

另請參閱