What is it anyway? It is a user control _could be prompted to a custom control later_ that is responsible for providing cross-browser compatible Modal Dialog support.
Dialog: It is any UI component that displays a message and is usually not part of the main window of the application.
Modal: Having a mode, that is the mode of being the only UI component that one can interact with while it is displayed. A modal dialog cannot be deactivated it can only be closed. One cannot interact with anything other than this dialog as long as the dialog exists. An example would be the javascript alert box that is displayed as a result of the following javascript statement:
window.alert(‘this
is an alert’);
Cross-Browser: By this we mean that it could display, and behave properly and correctly on the most common web browsers today. Of these we can mention Microsoft’s Internet Explorer and Mozilla’s Firefox, 2 of the most used Web browsers today. Other browsers include: Opera, Netscape Navigator, Safari, Konqueror, etc…
The need is the mother of invention, it is said so. Anyway, I needed to have a cross-browser Modal Dialog, and I already knew that window.ShowModalDialog is IE specific, so the search began, well it did not take that long until I was reading about this javascript control that a guy made. In the core all credit goes to that guy for the javascript he wrote and the idea he implemented. What I did is add some tweaks and configs to his javascript, _important tweaks in my opinion_ and wrapped the whole thing to be directly usable by ASP.NET.
The control is a build upon the work of the guy I mentioned previously, he calls it SubModal, his work can be found at http://www.subimage.com/sublog/subModal _case sensitive. The main idea in brief is based on the use of <div> elements to show layers with a high z-index. The visibility, content and style of the <div> are controlled using javascripts. The main <div> has an <iframe> element in whih the page is loaded and unloaded. For more detailed information on the inner workings of this javascript visit the above link.
Every thing that you need to know is mentioned in this page, no need to go into the details of the javascript to get it working. A moderate knowledge of what javascript is is required never the less.
First things first:
The files that are needed to run the sample are provided as a zip file. If you have VS.NET a project file is also provided. All you need to see the sample at work is to build the project. If you don’t have VS.NET then use csc.exe to compile the C# files into an assembly to get things working, if you still feel that you don’t know what to do consult either a brain doctor or a .NET course.
Important files include:
Please also note that although many of these files are optional, they are required in real applications, so they should be imitated and followed as templates.
At this point I should mention that I shall not go into the details of the js files since this is not the point of this article. Any information needed on these files can be found at the link I provided earlier.
protected
string PostBackEvent = "";
private
void Page_Load(object
sender, System.EventArgs e)
{
PostBackEvent = this.Page.GetPostBackClientEvent(this.refreshButton,
"");
}
private
string _DialogPage;
private
string _DialogWidth;
private string _DialogHeight;
following is the only event needed if we wish to do some post-dialog actions:
public
delegate void
DialogCommandEventHandler(object sender,EventArgs
e);
public event DialogCommandEventHandler DialogFinished;
protected
virtual void OnDialogFinished(EventArgs
e)
{
if(DialogFinished !=
null) DialogFinished(this, e);
}
Now the part that writes the necessary javascript that links to the engine:
protected
string PostBackEvent = "";
private
void Page_Load(object
sender, System.EventArgs e)
{
PostBackEvent = this.Page.GetPostBackClientEvent(this.refreshButton,
"");
}
public
void show()
{
Page.RegisterStartupScript("dialogScript", @"<script>
function popupLoader()
{
showPopWin('" + DialogPage + "', " + DialogWidth + ", " + DialogHeight +
@", PostBackMethod);
}
var prev_onload = window.onload;//careful not to have other vars with
same name
window.onload = function() {
initPopUp();
popupLoader();
if(prev_onload)
return prev_onload();
else
return true;
};
</script>");
}
private
void btnDialog_Click(object
sender, System.EventArgs e)
{
this.ModalDialogControl1.DialogHeight
= "100";
this.ModalDialogControl1.DialogWidth
= "500";
this.ModalDialogControl1.DialogPage
= "PopupForm.aspx";
this.ModalDialogControl1.show();
}
private
void btnAlert_Click(object
sender, System.EventArgs e)
{
this.ModalDialogControl1.ShowAlert("This
is a Modal Alert. Thank you...");
}
override
protected void OnInit(EventArgs
e)
{
//
// CODEGEN: This call is required by
the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
this.ModalDialogControl1.DialogFinished +=
new
IceShock.ModalDialog.ModalDialogControl.DialogCommandEventHandler(ModalDialogControl1_DialogFinished);
}
private
void ModalDialogControl1_DialogFinished(object
sender, EventArgs e)
{
RegisterStartupScript
("DialogFinished",
"<script>alert('this indicates that the event of the dialog has
fired');</script>");
}
I am also providing a file called DialogPage.cs containing a class called DialogPage. This page can be used as a base for any Dialog. The functionality it provides is summerized by:
Promoting to Custom Control...
We now have a cross-browser compatible Modialog User Control that can be added to any page. Better yet we can build a hirarchy of page classes where we can inherit from our own class rather than the System.Web.UI.Page class, and in our class we can add the user control to the page, so the user control will be available by default in every new page we create, no toil...
For feedback and questions you can post to my Blogs.