Introduction

Do you remember the message box we used in Windows applications to notify a user when necessary? This message box increases user experience in a clever way.

save cofirmation

Unfortunately, in web applications, this is replaced with JavaScript alert() and confirm() which cannot be controlled by the designer.

confirm

Background

This article is based on the concepts discussed on Confirm Message Box with addition to some features from System.Windows.Forms.MessageBox class.

Using the Code

The control, just like System.Windows.Forms.MessageBox, provides Show() method overloads, and also it provides RegisterButtonForShowigMessage() with an additional parameter to hold the button reference.

The control also provides extra three parameters of type MessageBoxButtonAction which has four values:

  • Undefined: for doing nothing
  • AcceptDialog: for re-firing the post-back event of the calling button.
  • Close: for closing the dialog and raising ClientResultReturn event on the Client-Side.
  • PostBack: for closing the dialog and raising ResultReturn event on the Server-Side.

The control also provides eight properties of type String to customize the text on the buttons.

You can use the control just like:

MessageBox MessageBox1 = MessageBox.GetCurrent(this);
MessageBox1.OnClientResultReturn = "OnCloseDialig";

MessageBox1.RegisterButtonForShowigMessage
(cmdClose, "Do you want to save this transaction before exiting the page?",
"My Application", "Exit", MyControls.MessageBoxButtons.YesNoCancel,
MyControls.MessageBoxIcon.Exclamation, MyControls.MessageBoxDefaultButton.Button1,
MyControls.MessageBoxButtonAction.PostBack,
MyControls.MessageBoxButtonAction.PostBack, MyControls.MessageBoxButtonAction.Close);

You can also add a JavaScript function that handles ClientResultReturn event just like:

function OnCloseDialig(sender, e) {
    var args = sender.get_Tag().split(',');
    if (args[0] == 'Copy')
        if (e.get_DialogResult() == MyControls.DialogResult.Abort || 
        e.get_DialogResult() == MyControls.DialogResult.OK)
        $get('<%= loader.ClientID %>').style.display = 'none';
}

Points of Interest

Unlike Confirm Message Box, we don't need to keep more than one instance of the message box object. So we first add the object to Page.Items Collection within OnInit() method.

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    if (!base.DesignMode)
    {
        if (GetCurrent(this.Page) != null)
        {
            throw new InvalidOperationException
		("Only One MessageBox instance is allowed.");
        }
        Page iPage = this.Page;
        iPage.Items[typeof(MessageBox)] = this;
    }
}

After that, we use this collection from any content page that its master contains the control or even a control in the page by using MessageBox.GetCurrent() method.

public static MessageBox  GetCurrent(Page page)
{
    if (page == null)
    {
        throw new ArgumentNullException("page");
    }
    return (page.Items[typeof(MessageBox)] as MessageBox);
}    

History

  • Saturday, May 14, 2011: First version
  • Sunday, May 15, 2011: Updated article
  • Monday, June 6, 2011: Updated source code and images
推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架