共用方式為


PostBackTrigger 類別

定義

將位於 UpdatePanel 控制項內部的控制項定義為回傳控制項。

public ref class PostBackTrigger : System::Web::UI::UpdatePanelControlTrigger
public class PostBackTrigger : System.Web.UI.UpdatePanelControlTrigger
type PostBackTrigger = class
    inherit UpdatePanelControlTrigger
Public Class PostBackTrigger
Inherits UpdatePanelControlTrigger
繼承

範例

下列範例示範如何以宣告方式定義 PostBackTrigger 控制項的 UpdatePanel 控制項。 在面板中,控制項 FileUpload 可讓使用者上傳檔案。 使用者必須先檢查要上傳的檔案是否存在。 Button呼叫事件處理常式以檢查檔案名的控制項會導致非同步回傳。 不過, Button 上傳檔案的控制項會註冊為 PostBackTrigger ,因為無法以非同步方式上傳檔案。

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

    private string saveDir = @"Uploads\";
    
    protected void UploadButton_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile && FileUpload1.FileBytes.Length < 10000 &&
            !CheckForFileName())
        {
            string savePath = Request.PhysicalApplicationPath + saveDir +
                Server.HtmlEncode(FileName.Text);
            //Remove comment from the next line to upload file.
            //FileUpload1.SaveAs(savePath);
            UploadStatusLabel.Text = "The file was processed successfully.";
        }
        else
        {
            UploadStatusLabel.Text = "You did not specify a file to upload, or a file name, or the file was too large. Please try again.";
        }
    }

    protected void CheckButton_Click(object sender, EventArgs e)
    {
        if (FileName.Text.Length > 0)
        {
            string s = CheckForFileName() ? "exists already." : "does not exist.";
            UploadStatusLabel.Text = "The file name choosen " + s;
        }
        else
        {
            UploadStatusLabel.Text = "Specify a file name to check.";
        }
    }
    private Boolean CheckForFileName()
    {
        System.IO.FileInfo fi = new System.IO.FileInfo(Request.PhysicalApplicationPath + 
            saveDir + Server.HtmlEncode(FileName.Text));
            return fi.Exists;
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>PostBackTrigger Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    The upload button is defined as a PostBackTrigger.<br/>
    <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
    <fieldset>
    <legend>FileUpload in an UpdatePanel</legend>
       First, enter a file name to upload your file to: 
       <asp:TextBox ID="FileName" runat="server" />
       <asp:Button ID="CheckButton" Text="Check" runat="server" OnClick="CheckButton_Click" />
       <br />
       Then, browse and find the file to upload:
       <asp:FileUpload id="FileUpload1"                 
           runat="server">
       </asp:FileUpload>
       <br />
       <asp:Button id="UploadButton" 
           Text="Upload file"
           OnClick="UploadButton_Click"
           runat="server">
       </asp:Button>    
       <br />
       <asp:Label id="UploadStatusLabel"
           runat="server" style="color:red;">
       </asp:Label>           
    </fieldset>
    </ContentTemplate>
    <Triggers>
    <asp:PostBackTrigger ControlID="UploadButton" />
    </Triggers>
    </asp:UpdatePanel>
    </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">

    Private saveDir As String = "Uploads\\"

    Protected Sub UploadButton_Click(ByVal sender As Object, ByVal e As EventArgs)
        
        If (FileUpload1.HasFile AndAlso FileUpload1.FileBytes.Length < 10000 AndAlso _
           Not (CheckForFileName())) Then
            Dim savePath As String = Request.PhysicalApplicationPath & saveDir & _
               Server.HtmlEncode(FileName.Text)
            'Remove comment from the next line to upload file.
            'FileUpload1.SaveAs(savePath)
            UploadStatusLabel.Text = "The file was processed successfully."
        Else
            UploadStatusLabel.Text = "You did not specify a file to upload, or a file name, or the file was too large. Please try again."
        End If
        
    End Sub

    Protected Sub CheckButton_Click(ByVal sender As Object, ByVal e As EventArgs)
        If (FileName.Text.Length > 0) Then
            Dim s As String
            If (CheckForFileName()) Then
                s = "exists already."
            Else
                s = "does not exist."
            End If
            UploadStatusLabel.Text = "The file name choosen " & s
        Else
            UploadStatusLabel.Text = "Specify a file name to check."
        End If
    End Sub

    Private Function CheckForFileName() As Boolean
        Dim fi As New System.IO.FileInfo(Request.PhysicalApplicationPath & _
           saveDir & Server.HtmlEncode(FileName.Text))
        Return fi.Exists
    End Function

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>PostBackTrigger Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    The upload button is defined as a PostBackTrigger.<br/>
    <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
    <fieldset>
    <legend>FileUpload in an UpdatePanel</legend>
       First, enter a file name to upload your file to: 
       <asp:TextBox ID="FileName" runat="server" />
       <asp:Button ID="CheckButton" Text="Check" runat="server" OnClick="CheckButton_Click" />
       <br />
       Then, browse and find the file to upload:
       <asp:FileUpload id="FileUpload1"                 
           runat="server">
       </asp:FileUpload>
       <br />
       <asp:Button id="UploadButton" 
           Text="Upload file"
           OnClick="UploadButton_Click"
           runat="server">
       </asp:Button>    
       <br />
       <asp:Label id="UploadStatusLabel"
           runat="server" style="color:red;">
       </asp:Label>           
    </fieldset>
    </ContentTemplate>
    <Triggers>
    <asp:PostBackTrigger ControlID="UploadButton" />
    </Triggers>
    </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

備註

PostBackTrigger使用 控制項可讓 控制項內部 UpdatePanel 的控制項造成回傳,而不是執行非同步回傳。

RegisterPostBackControl使用 控制項的 ScriptManager 方法,以程式設計方式註冊回傳控制項。 然後,當觸發程式控制項執行回傳時,您可以呼叫 Update 控制項的 UpdatePanel 方法。

注意

不支援以程式設計方式新增 PostBackTrigger 控制項。

如果控制項同時設定為 PostBackTriggerAsyncPostBackTrigger 控制項,則會擲回例外狀況。

建構函式

PostBackTrigger()

初始化 PostBackTrigger 類別的新執行個體。

屬性

ControlID

取得或設定控制項的名稱,其為 PostBackTrigger 控制項的 UpdatePanel 控制項。

Owner

取得對 UpdatePanel 設為目標之 UpdatePanelTrigger 控制項的參考。

(繼承來源 UpdatePanelTrigger)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
FindTargetControl(Boolean)

搜尋在 ControlID 屬性中指定的控制項。

(繼承來源 UpdatePanelControlTrigger)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
HasTriggered()

傳回值以指出是否要啟動觸發程序。

Initialize()

初始化 PostBackTrigger 物件。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回表示目前 PostBackTrigger 物件的字串。

適用於

另請參閱