| MsgBox Web Control: Article |
|
MsgBox - A custom ASP.NET web-control to display message box
dialogs in a web-browser
Author: Willem J Fourie
For an overview see Installation and Usage.
Source Code for this Article
The following help file maybe downloaded:
-
MsgBox.chm - Complete source code for the
MsgBox custom web-control, a sample ASP.NET application, and reference
documentation for the MsgBox web-control.
The MsgBox
control is a custom ASP.NET web-control which provides a user-defined message
box to appear following a page postback to the browser. The message
boxes are generated by client-side javascript alert(), confirm(),
or prompt() function call. When the dialog is dismissed,
the page is then submitted via javascript from the client, and the message box
return value is available to the ASP.NET page code-behind assembly via an event
generated by the MsgBox web-control.
The following features make the
MsgBox web-control easy to use:
The MsgBox web-control works
in the following way:
-
-
Assuming that for a another web-control element, a button, a
message box response is required when the button is clicked, a OnClick
is defined and the following code will activate the message box:
private void btnPrompt_Click(object sender, System.EventArgs e)
{
this.MsgBox1.Execute(
MsgBox.MsgBoxType.Prompt,
"Prompt message shown in response to button click!",
"Default Prompt Value", sender as Control);
}
In the above example, the
Execute method is called, which overrides the properties of MsgBox1: a
prompt() type message box is displayed, with
Text set to the value of the second parameter, while
PromptValue is set to the third parameter. The fourth
parameter, if used, will return the control that caused the post-back in the
first instance. Later, when the message box return value is retrieved,
this control will be referenced so that the application has knowledge of which
control originally caused the message box to be displayed.
Note that the alternative
Execute method can be used, if the control properties have been set
already, as follows:
private void btnPrompt_Click(object sender, System.EventArgs e)
{
this.msg.Execute(sender as Control);
}
-
When the OnClick response method has executed,
the page is then posted back to the browser. During the rendering of the
page for post-back, the control registers a startup script using the Page.RegisterStartupScript
method, and it registers two hidden fields using the Page.RegisterHiddenField
method, namely a field for the message box return value (string), and another
for the ID of the control that originally caused the message box to be
displayed.
In the script generation (private method
CreateMessageScript), the appropriate javascript function is called to
display the message box. When the message box is dismissed, the return
value is saved in the hidden field for the message box return. Script is
also generated to cause a post-back when the message box is dismissed by
calling the
Page.GetPostBackEventReference method. Typically,
the generated script for the above code example looks like this:
<script>
document.getElementById('__MsgBox1_Return').value=
prompt('Prompt message shown in response to button click!','Default Prompt Value');
__doPostBack('MsgBox1','')
</script>
Note that the __doPostBack script function is
also generated by the Page.GetPostBackEventReference method.
The following are important things to remember:
-
The client javascript generated by the
MsgBox control causes a post-back to occur after the message box
is dismissed, as explained in the section
above. Obviously this incurs another round-trip to the web-server and
impacts performance.
-
The client javascript is generated as a startup script and it may interfere
with any other controls that work in the same way, or with client-side script
that may already exist in the web-form .aspx file.
-
The control has not been extensively tested. Placing the control in other
container controls has not been tested. It is recommended that the control
is placed directly on the form at all times.
-
The control has been developed using Visual Studio 2003 (7.1) and the .NET
Framework 1.1. It should work correctly with prior versions of VS and the
framework, provided that the control is recompiled in those environments.
The MsgBox control is a
custom ASP.NET web-control that provides an easy way to display message boxes
for user alerts, confirmations and prompts. The control provides
extensive client scripting functionality without the application program having
to write (or maintain) any client-side script.