UpdatePanel.UpdateMode 属性

定义

获取或设置一个值,该值指示何时更新 UpdatePanel 控件的内容。Gets or sets a value that indicates when an UpdatePanel control's content is updated.

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

UpdatePanelUpdateMode 值之一。One of the UpdatePanelUpdateMode values. 默认值为 AlwaysThe default is Always.

例外

指定的类型不是 UpdatePanelUpdateMode 值之一。The specified type is not one of the UpdatePanelUpdateMode values.

示例

下面的示例声明了两个 UpdatePanel 控件。The following example declares two UpdatePanel controls. 在第一个面板中, UpdateMode 属性设置为 ConditionalIn the first panel, the UpdateMode property is set to Conditional. 在第二个面板中,将 UpdateMode 设置为 AlwaysIn the second panel, UpdateMode is set to Always. 通过调用控件的方法,将两个面板外的按钮注册为异步回发控件 RegisterAsyncPostBackControl ScriptManagerA button outside both panels is registered as an asynchronous postback control by calling the RegisterAsyncPostBackControl method of the ScriptManager control. 在按钮的 Click 事件处理程序中, Update 如果自上次更新后超过5秒,则会调用第一个面板的方法。In the button's Click event handler, the Update method of the first panel is called if more than five seconds have elapsed since its last update. 在这种情况下,仅当最后一次刷新面板超过5秒之前,才会更新面板的内容。In this scenario, the panel's content is updated only if the last panel refresh was more than five seconds ago. 第二个面板的内容始终更新。The content of the second panel is always updated.


<%@ 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 Not ViewState("LastUpdate") Is 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>

注解

UpdatePanel 控件不在另一个控件内时 UpdatePanel ,面板将随 UpdateMode 和属性的设置 ChildrenAsTriggers (连同触发器的集合)的设置进行更新。When an UpdatePanel control is not inside another UpdatePanel control, the panel is updated as determined by the settings of the UpdateMode and ChildrenAsTriggers properties, together with the collection of triggers. UpdatePanel 控件位于另一个控件内时 UpdatePanel ,子面板会在父面板更新时自动更新。When an UpdatePanel control is inside another UpdatePanel control, the child panel is automatically updated when the parent panel is updated.

UpdatePanel在下列情况下,将更新控件的内容:The content of an UpdatePanel control is updated in the following circumstances:

  • 如果将 UpdateMode 属性设置为 Always ,则 UpdatePanel 会在源自页面上任何位置的每个回发中更新控件的内容。If the UpdateMode property is set to Always, the UpdatePanel control's content is updated on every postback that originates from anywhere on the page. 这包括来自其他控件内的控件的异步回发 UpdatePanel 以及来自不在控件内的控件的回发 UpdatePanelThis includes asynchronous postbacks from controls inside other UpdatePanel controls and postbacks from controls that are not inside UpdatePanel controls.

  • 如果 UpdatePanel 控件嵌套在另一个 UpdatePanel 控件中,并且更新了父更新面板,则为。If the UpdatePanel control is nested inside another UpdatePanel control and the parent update panel is updated.

  • 如果 UpdateMode 属性设置为 Conditional ,则会出现下列情况之一:If the UpdateMode property is set to Conditional, and one of the following conditions occurs:

    • Update显式调用控件的方法 UpdatePanelYou call the Update method of the UpdatePanel control explicitly.

    • 使用控件的属性定义为触发器的控件将导致回发 Triggers UpdatePanelThe postback is caused by a control defined as a trigger by using the Triggers property of the UpdatePanel control. 在此方案中,控件显式触发面板内容更新。In this scenario, the control explicitly triggers an update of the panel content. 控件可以在定义触发器的控件内部或外部 UpdatePanelThe control can be either inside or outside the UpdatePanel control that defines the trigger.

    • ChildrenAsTriggers属性设置为 true ,并且控件的子控件 UpdatePanel 导致回发。The ChildrenAsTriggers property is set to true and a child control of the UpdatePanel control causes a postback. 嵌套控件的子控件不 UpdatePanel 会导致对外部控件的更新, UpdatePanel 除非将其显式定义为触发器。A child control of a nested UpdatePanel control does not cause an update to the outer UpdatePanel control unless it is explicitly defined as a trigger.

适用于

另请参阅