ASP.NET: Modal Dialogs
There are often times when your Web design includes a modal dialog. For example: a login form, a dialog for entering basic information, a search form, or a data entry form. This post covers how to build a modal dialog in ASP.NET.
There are several advantages to using a modal dialog instead of another Web page:
- The user does not have to navigate away from the current page. This allows the user to focus on the main information, yet enter criteria or other parameters in a modal dialog.
- The code has better control over the dialog operations. That is to say that the user must provide some response to the modal dialog. However, the user can still navigate to another page or close the browser.
I mentioned in a recent post that after being away from ASP.NET for a while, I was surprise at how little had changed. But there is one area where ASP.NET has changed significantly: AJAX. If you have not downloaded the ASP.NET AJAX toolkit, check it out here.
This post focuses on the ModalPopupExtender control that is part of the AJAX toolkit. In this example, the user sets a security question and answer in a modal dialog. If the user clicks on the Security Question link in the upper right corner, the Security Question modal dialog appears for entry of the question and answer.
As with most modal dialogs, this one implements validation. If the user clicks on OK without entering the required data, validation messages appear:
I wrote this example in both VB and C#. But since the only line of code that is different is the Page tag that defines the language, I am only showing the ASP.NET code one time.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SampleWebCSharp._Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Sample Web Page</title>
<body style="background-image: url(Images/Gradient2.jpg);background-repeat: repeat-x;">
<form id="form1" runat="server">
<asp:ScriptManager ID="sm" runat="server"/>
<!– Right side Link bar –>
<!– Signin Area –>
<asp:HyperLink ID="SigninLink" runat="server"
ToolTip="Click to sign into the application"
Text="Sign in" />
ToolTip="Click to set the security question"
Text="Security Question" />
<!– Security Question Popup –>
<asp:Label ID="Label1" runat="server"
Text="Security Question" width="300px"/><br />
<table style="width: 100%;">
ErrorMessage="Please enter your security question.<br/>"/>
ErrorMessage="Please enter the answer to your security question.<br/>"/>
<!– Bottom Buttons –>
<asp:Button ID="OKButton" runat="server"
ToolTip="Click to save the entered question/answer"/>
<asp:Button ID="CancelButton" runat="server"
ToolTip="Click to cancel any changes"/>
<!– Define the modal dialog –>
Notice at the very top of the code the Register tag registers the Ajax Control Toolkit. This line is added to your code automatically if you drag an Ajax control onto your WebForm.
The body tag uses a gradient image as shown in this prior post.
The ScriptManager control manages all of the Ajax features and is required on any page that uses Ajax controls.
The first div element contains the code to display the links in the upper right corner. No special code is required on these elements. The HyperLink control does not have an attribute to display the modal dialog. Rather, the ModalPopupExtender identifies the control that will cause it to popup. (More on this later in this post.)
The Panel control contains all of the controls that appear in the modal dialog. It also defines the style of the popup including its background color, foreground color, and border. Notice that the display style is set to none. This ensures that the popup does not display when the page is first rendered.
In this example, the modal dialog contains a title at the top, a table to layout the Labels and TextBoxes in the middle, and OK and Cancel buttons on the bottom. Notice that the Cancel button has its CausesValidation attribute set to false. This ensures that pressing the Cancel button won’t perform the validation and display the validation messages.
An additional row in the table provides an area to display validation messages. The validation controls have their Display attribute set to Dynamic so that space is not allocated to the message text unless there is a validation error.
Finally, the key part of this code: the ModalPopupExtender. The key attributes of this control in this example are:
- TargetControlID: Id of the control that causes display of the modal dialog. In this case, it is the Security Question Hyperlink control at the top right of the page.
- PopupControlID: Id of the control that is the modal dialog. In this case, it is the Panel control.
- CancelControlID: Id of the button on the modal dialog that causes a cancel operation. By default, this closes the modal dialog.
Notice that the OKControlID attribute is not set for this control. By default, setting the OKControlID also closes the modal dialog, so validation messages won’t appear.
If you don’t set the OKControlID attribute, you can instead write code for the OK button in the code behind. This allows for the display of the validation messages. You can also perform processing, such as additional server-side validation or saving the entered values.
public partial class _Default : System.Web.UI.Page
protected void OKButton_Click(object sender, EventArgs e)
// Do any additional processing, such as saving the values
//Close the popup
Partial Public Class _Default
Private Sub OKButton_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles OKButton.Click
‘ Do any additional processing, such as saving the values
‘ Close the popup
NOTE: The technique you use to set up the events is very different in VB than in C#.
- Double-click on the control.
In this example, this is not an easy task because the panel containing the button is hidden. So to use this method, you would need to temporarily make the panel not hidden, double-click on the button, and make the panel hidden again.
- OR, manually type in the event signature in the code behind file.
With the first technique, Visual Studio automatically changes the Button attributes in your ASP.NET code as follows:
<asp:Button ID="OKButton" runat="server"
Text="OK" ToolTip="Click to save the entered question/answer"
With the second technique, you need to add the onclick attribute yourself.
- Double-click on the control.
Again, not so easy because the control is hidden.
- OR, open the code behind file and use the two ComboBoxes at the top of the code editor. Select the control from the first ComboBox on the left. Select the event from the second ComboBox on the right. This is the best technique to use in this example.
NOTE: VB does NOT require a change in the ASP.NET code.
Use this technique any time you want to display a modal dialog from your Web page.