IPostBackEventHandler.RaisePostBackEvent(String) 方法

定义

当由类实现时,使服务器控件能够处理将窗体发送到服务器时引发的事件。

public void RaisePostBackEvent(string eventArgument);

参数

eventArgument
String

表示要传递到事件处理程序的可选事件参数的 String

示例

下面的代码示例定义一个自定义按钮服务器控件,该控件会导致回发,使用 RaisePostBackEvent 方法捕获回发事件,并在服务器上引发事件 Click

using System;
using System.Web.UI;
using System.Collections;
using System.Collections.Specialized;

namespace CustomControls {

   [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
   public class MyButton: Control, IPostBackEventHandler {

      // Defines the Click event.
      public event EventHandler Click;

      //Invoke delegates registered with the Click event.
      protected virtual void OnClick(EventArgs e) {

         if (Click != null) {
            Click(this, e);
         }
      }

      // Define the method of IPostBackEventHandler that raises change events.
      public void RaisePostBackEvent(string eventArgument){

         OnClick(new EventArgs());
      }

      protected override void Render(HtmlTextWriter output) {
         output.Write("<INPUT TYPE = submit name = " + this.UniqueID +
            " Value = 'Click Me' />");
      }
   }
}

注解

页面将 参数的值 eventArgument 传递给 RaisePostBackEvent 实现 IPostBackEventHandler 接口的控件的 方法。 此控件还呈现导致回发的 HTML 元素。 如果 控件呈现用于回发的客户端脚本,则脚本中的 参数将传递到 参数 eventArgument 中。 如果回发是由简单的提交操作引起的,则 eventArgument 参数为 null

此方法为 HTML 和 Web 服务器控件实现的许多事件提供功能。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

另请参阅