IPostBackEventHandler.RaisePostBackEvent(String) 方法
本文内容
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当由类实现时,使服务器控件能够处理将窗体发送到服务器时引发的事件。
public:
void RaisePostBackEvent(System::String ^ eventArgument);
public void RaisePostBackEvent(string eventArgument);
abstract member RaisePostBackEvent : string -> unit
Public Sub RaisePostBackEvent (eventArgument As 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' />");
}
}
}
Imports System.Web.UI
Imports System.Collections
Imports System.Collections.Specialized
Namespace CustomControls
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> Public Class MyButton
Inherits Control
Implements IPostBackEventHandler
' Define the Click event.
Public Event Click As EventHandler
' Invoke delegates registered with the Click event.
Protected Overridable Sub OnClick(e As EventArgs)
RaiseEvent Click(Me, e)
End Sub
' Define the method of IPostBackEventHandler that raises change events.
Public Sub RaisePostBackEvent(eventArgument As String) _
Implements IPostBackEventHandler.RaisePostBackEvent
OnClick(New EventArgs())
End Sub
Protected Overrides Sub Render(output As HtmlTextWriter)
output.Write("<INPUT TYPE = submit name = " & Me.UniqueID & _
" Value = 'Click Me' />")
End Sub
End Class
End Namespace
页面将 参数的值 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 |