UpdatePanel.Update 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
導致更新 UpdatePanel 控制項的內容。
public:
void Update();
public void Update ();
member this.Update : unit -> unit
Public Sub Update ()
例外狀況
UpdateMode 屬性設定為 Always。
-或- 處理網頁的 Render(HtmlTextWriter) 事件時或處理完成之後,系統會呼叫 Update() 方法。
範例
下列範例會 Update 使用 方法,在自上次更新以來超過五秒時更新控制項的內容 UpdatePanel 。
<%@ 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>
備註
如果頁面已啟用部分頁面轉譯,當您叫 Update 用 方法時, UpdatePanel 控制項的內容會在瀏覽器中更新。 Update如果您有必須執行的伺服器程式碼來判斷是否 UpdatePanel 應該更新控制項,請呼叫 方法。 如果您打算使用 Update 方法,請將 UpdateMode 屬性設定為 Conditional 。 如果您想要在伺服器邏輯中更新面板,請確定 ChildrenAsTriggers 屬性為 false
,而且不會定義面板的明確觸發程式。
在一般頁面開發案例中,如果您定義觸發程式,或如果 ChildrenAsTriggers 屬性是 true
用於 UpdatePanel 控制項,則 Update 方法會在頁面生命週期期間自動呼叫。
ContentTemplate如果未為 UpdatePanel 控制項定義 屬性,則不會更新面板。