PostBackTrigger 类

定义

UpdatePanel 控件内部的控件定义为回发控件。Defines a control inside a UpdatePanel control as a postback control.

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控件定义控件。The following example shows how to declaratively define a PostBackTrigger control for an UpdatePanel control. 在面板中, FileUpload控件使用户能够上传文件。In the panel, a FileUpload control enables users to upload a file. 用户必须首先检查要上传的文件是否存在。Users must first check whether the file to upload exists. 调用事件处理程序以检查文件名的控件将导致异步回发。ButtonThe Button control that calls the event handler to check the file name causes an asynchronous postback. 但是, Button上传文件的控件将注册PostBackTrigger为, 因为无法以异步方式上载文件。However, the Button control that uploads the file is registered as a PostBackTrigger, because files cannot be uploaded asynchronously.

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

注解

使用控件可以在中启用控件UpdatePanel , 从而导致回发, 而不是执行异步回发。 PostBackTriggerUse the PostBackTrigger control to enable controls inside an UpdatePanel to cause a postback instead of performing an asynchronous postback.

使用ScriptManager控件RegisterPostBackControl的方法以编程方式注册回发控件。Use the RegisterPostBackControl method of the ScriptManager control to programmatically register a postback control. 然后, 当触发器控件Update执行回发UpdatePanel时, 可以调用控件的方法。You can then call the Update method of the UpdatePanel control when the trigger control performs a postback.

备注

不支持PostBackTrigger以编程方式添加控件。Programmatically adding PostBackTrigger controls is not supported.

如果控件设置为PostBackTriggerAsyncPostBackTrigger控件, 则将引发异常。If a control is set as both a PostBackTrigger and AsyncPostBackTrigger control, an exception is thrown.

构造函数

PostBackTrigger()

初始化 PostBackTrigger 类的新实例。Initializes a new instance of the PostBackTrigger class.

属性

ControlID

获取或设置一个控件的名称,该控件充当 UpdatePanel 控件的 PostBackTrigger 控件。Gets or sets the name of the control that is an PostBackTrigger control for an UpdatePanel control.

Owner

获取对 UpdatePanel 所面向的 UpdatePanelTrigger 控件的引用。Gets a reference to the UpdatePanel control that the UpdatePanelTrigger targets.

(继承自 UpdatePanelTrigger)

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
FindTargetControl(Boolean)

搜索在 ControlID 属性中指定的控件。Searches for the control specified in the ControlID property.

(继承自 UpdatePanelControlTrigger)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
HasTriggered()

返回一个指示是否已激活触发器的值。Returns a value that indicates whether the trigger was activated.

Initialize()

初始化 PostBackTrigger 对象。Initializes the PostBackTrigger object.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前 PostBackTrigger 对象的字符串。Returns a string that represents the current PostBackTrigger object.

适用于

另请参阅