Capturing the Postback via IPostBackEventHandler

As part of our design, we had the requirement of rendering the button as either a normal button or as a specially configured hyperlink to submit the web form. With events in hand, we now move on to hooking the button click into the postback process through implementation of the IPostBackEventHandler interface. To achieve this, we next implement the single method of the postback interface, RaisePostBackEvent:

public void RaisePostBackEvent(string argument);

RaisePostBackEvent takes a single argument as a means to retrieve a value from the form submission. When a Button submits a web form, it always passes a blank value for this argument to RaisePostBackEvent. Our hyperlink-rendering code has a choice of what information to pass via the Page.ClientScript.GetPostBackClientHyperlink method call. The following code snippet submits a blank value to keep things in line with our button rendering:

writer.Write(Page.ClientScript.GetPostBackClientHyperlink(this,"")); writer.Write("\">" + Text + "</A>");

The RaisePostBackEvent implementation in our SuperButton control has very little work to do, as we encapsulated the bulk of our event-generating code in the OnClick and OnCommand methods:

public void RaisePostBackEvent(string argument) {

OnCommand(new CommandEventArgs(CommandName, CommandArgument)); OnClick(EventArgs.Empty);

Completing the RaisePostBackEvent method brings our SuperButton control to fruition. Listing 5-9 is the class file for the control and its related enumeration. The control needs a using import for the System.Web.UI.WebControls namespace, because it takes advantage of Command events.

Listing 5-9. The SuperButton Control Class File using System;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace ControlsBook2Lib.Ch05

public enum ButtonDisplay {

[ToolboxData("<{0}:superbutton runat=server></{0}:superbutton>")]

public class SuperButton : Control, IPostBackEventHandler {

public virtual ButtonDisplay Display {

object display = ViewState["Display"]; if (display == null) return ButtonDisplay.Button;

else return (ButtonDisplay)display;

ViewState["Display"] = value;

public virtual string Text {

object text = ViewState["Text"]; if (text == null)

return string.Empty; else return (string)text;

ViewState["Text"] = value;

private static readonly object ClickKey = new object();

public event EventHandler Click {

Events.AddHandler(ClickKey, value);

remove {

Events.RemoveHandler(ClickKey, value);

protected virtual void OnClick(EventArgs e) {

EventHandler clickEventDelegate = (EventHandler)Events[ClickKey];

if (clickEventDelegate != null) {

clickEventDelegate(this, e);

private static readonly object CommandKey = new object();

public event CommandEventHandler Command {

Events.AddHandler(CommandKey, value);

remove {

Events.RemoveHandler(CommandKey, value);

public virtual string CommandName {

object name = ViewState["CommandName"]; if (name == null)

return string.Empty; else return (string)name;

ViewState["CommandName"] = value;

public virtual string CommandArgument {

object arg = ViewState["CommandArgument"]; if (arg == null)

return string.Empty; else return (string)arg;

ViewState["CommandArgument"] = value;

protected virtual void OnCommand(CommandEventArgs ce) {

CommandEventHandler commandEventDelegate = (CommandEventHandler)Events[CommandKey];

if (commandEventDelegate != null) {

commandEventDelegate(this, ce);

RaiseBubbleEvent(this, ce);

public void RaisePostBackEvent(string argument) {

OnCommand(new CommandEventArgs(CommandName, CommandArgument)); OnClick(EventArgs.Empty);

protected override void Render(HtmlTextWriter writer) {

base.Render(writer); Page.VerifyRenderingInServerForm(this);

if (Display == ButtonDisplay.Button) {

writer.Write("<INPUT type=\"submit\""); writer.Write(" name=\"" + this.UniqueID + "\""); writer.Write(" id=\"" + this.UniqueID + "\""); writer.Write(" value=\"" + Text + "\""); writer.Write(" />");

else if (Display == ButtonDisplay.Hyperlink) {

writer.Write(Page.ClientScript.GetPostBackClientHyperlink(this, "")); writer.Write("\">" + Text + "</A>");

0 0

Post a comment

  • Receive news updates via email from this site