PostBackTrigger PostBackTrigger PostBackTrigger PostBackTrigger Class

Definición

Define un control dentro de un control UpdatePanel como control de postback.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
Herencia

Ejemplos

El ejemplo siguiente muestra cómo se definen mediante declaración una PostBackTrigger controlar para un UpdatePanel control.The following example shows how to declaratively define a PostBackTrigger control for an UpdatePanel control. En el panel, un FileUpload control permite a los usuarios cargar un archivo.In the panel, a FileUpload control enables users to upload a file. Los usuarios en primer lugar deben comprobar si existe el archivo para cargar.Users must first check whether the file to upload exists. El Button un postback asincrónico hace que el control que llama el controlador de eventos para comprobar el nombre de archivo.The Button control that calls the event handler to check the file name causes an asynchronous postback. Sin embargo, el Button control que carga el archivo está registrado como un PostBackTrigger, ya que los archivos no se pueden cargar de forma asincrónica.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>

Comentarios

Use la PostBackTrigger control para habilitar los controles dentro de un UpdatePanel para hacer que una devolución de datos en lugar de realizar un postback asincrónico.Use the PostBackTrigger control to enable controls inside an UpdatePanel to cause a postback instead of performing an asynchronous postback.

Use la RegisterPostBackControl método de la ScriptManager control para registrar mediante programación un control de postback.Use the RegisterPostBackControl method of the ScriptManager control to programmatically register a postback control. A continuación, puede llamar a la Update método de la UpdatePanel controlar cuando el control del desencadenador realiza una devolución de datos.You can then call the Update method of the UpdatePanel control when the trigger control performs a postback.

Nota

Agregar mediante programación PostBackTrigger controles no se admite.Programmatically adding PostBackTrigger controls is not supported.

Si un control se establece como un PostBackTrigger y AsyncPostBackTrigger control, se produce una excepción.If a control is set as both a PostBackTrigger and AsyncPostBackTrigger control, an exception is thrown.

Constructores

PostBackTrigger() PostBackTrigger() PostBackTrigger() PostBackTrigger()

Inicializa una nueva instancia de la clase PostBackTrigger.Initializes a new instance of the PostBackTrigger class.

Propiedades

ControlID ControlID ControlID ControlID

Obtiene o establece el nombre del control que es un control PostBackTrigger de un control UpdatePanel.Gets or sets the name of the control that is an PostBackTrigger control for an UpdatePanel control.

Owner Owner Owner Owner

Obtiene una referencia al control UpdatePanel que tiene como destino UpdatePanelTrigger.Gets a reference to the UpdatePanel control that the UpdatePanelTrigger targets.

(Inherited from UpdatePanelTrigger)

Métodos

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determina si el objeto especificado es igual al objeto actual.Determines whether the specified object is equal to the current object.

(Inherited from Object)
FindTargetControl(Boolean) FindTargetControl(Boolean) FindTargetControl(Boolean) FindTargetControl(Boolean)

Busca el control especificado en la propiedad ControlID.Searches for the control specified in the ControlID property.

(Inherited from UpdatePanelControlTrigger)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Sirve como la función hash predeterminada.Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Obtiene el Type de la instancia actual.Gets the Type of the current instance.

(Inherited from Object)
HasTriggered() HasTriggered() HasTriggered() HasTriggered()

Devuelve un valor que indica si se activó el desencadenador.Returns a value that indicates whether the trigger was activated.

Initialize() Initialize() Initialize() Initialize()

Inicializa el objeto PostBackTrigger.Initializes the PostBackTrigger object.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Crea una copia superficial del Object actual.Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

Devuelve una cadena que representa el actual objeto PostBackTrigger.Returns a string that represents the current PostBackTrigger object.

Se aplica a

Consulte también: