UpdatePanel.Update 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
會導致控制項內容 UpdatePanel 更新。
public:
void Update();
public void Update();
member this.Update : unit -> unit
Public Sub 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 控制項內容會在瀏覽器中更新。 如果你有伺服器程式碼必須執行來判斷控制項是否UpdatePanel需要更新,請呼叫該Update方法。 如果你打算使用此 Update 方法,請將屬性設 UpdateMode 為 Conditional。 如果你想在伺服器邏輯中決定更新面板,請確保 ChildrenAsTriggers 屬性是更新 false 的,且面板沒有明確的觸發條件。
在典型的頁面開發情境中,若你定義觸發器或該ChildrenAsTriggers屬性是true控制UpdatePanelUpdate項,該方法會在頁面生命週期中自動呼叫。
若控制組未定義UpdatePanel該ContentTemplate屬性,面板將不會進行更新。