CSDN博客

img colayungnew

服务器控件的学习-处理回发事件

发表于2004/10/22 12:03:00  682人阅读

分类: 学习笔记

要使控件捕获回发事件,控件必须实现 System.Web.UI.IPostBackEventHandler 接口。此接口的协定允许控件在服务器上引发事件来响应来自客户端的回发。

public InterFace IPostbackEventhandler
    sub RaisePostBackEvent(eventArgument as string)
end interface

回发后,页框架就会搜索发送的内容并确定发送的名称是否与实现IPostbackEventHandler的服务器控件的UniqueID对应,如果对应,页框架就会在该控件上调用RaisePostBackEvent方法

Public Overridable Sub RaisePostDataChangedEvent() Implements IPostBackDataHandler.RaisePostDataChangedEvent
   OnTextChanged(EventArgs.Empty)
End Sub

 呈现逻辑必须为控件的名称特性分配 UniqueID,如下面的示例所示。如果客户端的控件名称特性与其 UniqueID 不匹配,则页框架无法将回发事件传送给该控件。

Protected Overrides Sub Render(output As HtmlTextWriter)
   output.Write("<INPUT type=submit name=" & Me.UniqueID &     " Value='Click Me' />")
End Sub

全部代码:
Option Explicit
Option Strict

Imports System
Imports System.Web.UI

Namespace CustomControls
   Public Class MyButton
      Inherits Control
      Implements IPostBackEventHandler
      ' Defines the Click event.
      Public Event Click As EventHandler
     
      ' Invokes delegates registered with the Click event.
      Protected Overridable Sub OnClick(e As EventArgs)
         RaiseEvent Click(Me, e)
      End Sub
     
      ' Method of IPostBackEventHandler that raises change events.
      Public Sub RaisePostBackEvent(eventArgument As String) Implements IPostBackEventHandler.RaisePostBackEvent
         OnClick(EventArgs.Empty)
      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

阅读全文
0 0

相关文章推荐

img
取 消
img