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 はサポートされていません。

コントロールが コントロールと AsyncPostBackTrigger コントロールの両方PostBackTriggerとして設定されている場合は、例外がスローされます。

コンストラクター

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 オブジェクトを表す文字列を返します。

適用対象

こちらもご覧ください