Capturing Postback Events
For a control to capture a postback event, it must implement the System.Web.UI.IPostBackEventHandler interface. The contract of this interface allows a control to raise events on the server in response to postback from the client. The IPostBackEventHandler interface contains one method.
public interface IPostBackEventHandler{
void RaisePostBackEvent(string eventArgument);
}
[Visual Basic]
Public Interface IPostBackEventHandler
Sub RaisePostBackEvent(eventArgument As String)
End Interface
Upon postback, the page framework searches the posted content and determines whether a posted name corresponds to the UniqueID of a server control that implements IPostBackEventHandler. If so, it invokes the RaisePostBackEvent method on that control (after raising change events).
The following code fragment shows an implementation of RaisePostBackEvent that raises a Click
event on the server.
public void RaisePostBackEvent(String eventArgument){
OnClick(EventArgs.Empty);
}
[Visual Basic]
Public Overridable Sub RaisePostDataChangedEvent() Implements IPostBackDataHandler.RaisePostDataChangedEvent
OnTextChanged(EventArgs.Empty)
End Sub
Note It is essential that the rendering logic assign the UniqueID to the name attribute of the control as in the following example. The page framework is unable to route a postback event to your control if its name attribute on the client does not match its UniqueID.
protected override void Render(HtmlTextWriter output) {
output.Write("<INPUT TYPE=submit name=" + this.UniqueID +
" Value='Click Me' />");
}
[Visual Basic]
Protected Overrides Sub Render(output As HtmlTextWriter)
output.Write("<INPUT type=submit name=" & Me.UniqueID & _
" Value='Click Me' />")
End Sub
For a sample of a control that receives postback notifications and raises events on the server, see Postback Event Sample.