This article focuses on the following things.
- What is Modal Popup?
- Types of Modal Popups that can be create in Asp.Net.
- HTML, CSS and Javascript Code - Logic Explanation
- Step by Step Implementation of HTML, CSS and Javascript based Modal Popups.
What is Modal Popup?
Modal popup is a child window on the main window, that disables the main window functionality until the selection of a button in the child window.
In a sample image given below, I have shown the modal popup window.
Types of Modal Popups that can be created in ASP.NET
To the best of my knowledge, we can create three types of modal popups, through the following ways.
- HTML, CSS, and JavaScript.
- jQuery based (With different jQuery plugins)
- AjaxControlToolkit Modal Popup Extender.
HTML, CSS and JavaScript
It is very simple and very lightweight because this is a pure HTML and CSS based modal popup.
jQuery based
It is also light-weight but heavier than the HTML, CSS based one, because of the implementation of jQuery and plugins.
AjaxControlToolKit Modal Popup Extender
It runs through AjaxControlToolKit and is heavy weight.
In this article, we have created the following types of Modal Popups.
Where the popup receives confirmation from the user for whether to delete selected record or not. - Yes/No.
- HTML,CSS and JavaScript Code - Logic Explanation
Code Description:
For each friend, I have created a separate modal popup. To have unique id on div tag, I have attached FriendID. You can see the below mentioned code.
HTML CODE
Very simple and straight forward code of HTML.
- <%--Start Delete Modal Popup--%>
-
- <div id='overlay-<%# Eval("FriendID")%>'></div>
- <div id='modalMsg-<%# Eval("FriendID")%>' class="HideModal">
-
- <div class="popupbox">
- <br />
- <div class="popupttl bluetxt"><b>Delete Confirmation</b></div>
- <hr />
- <br />
- <br />
- <div class="popupdesc">
- <!-- START POPUP BLOCK -->
- <div class="popblock">
- <div class="poptxt ibvm">
- Are you sure you want to delete?
- </div>
- </div>
- <!-- END POPUP BLOCK -->
- <!-- START POPUP BLOCK -->
- <br />
- <div class="popblock popblocksubbtn">
- <asp:Button ID="btnDelete" runat="server" Text="Yes" CausesValidation="False" CommandName="DeleteItem" />
- <input type="submit" value="No" class="button bluebg ibvm" onclick="RemoveModal('<%# Eval("FriendID")%>')" />
- <asp:Button ID="btnHide" runat="server" Text="Back" Style="display: none" />
- </div>
- <!-- END POPUP BLOCK -->
- </div>
- </div>
- </div>
-
- <%--End of Delete Modal Poup--%>
CSS (StyleSheet Code)
- <style>
-
- .OverlayEffect {
- background-color: black;
- filter: alpha(opacity=70);
- opacity: 0.7;
- width: 100%;
- height: 100%;
- z-index: 400;
- position: absolute;
- top: 0;
- left: 0;
- }
-
-
- .HideModal {
- display: none;
- }
-
- .modalPopup {
- z-index: 1 !important;
- }
-
-
- .ShowModal {
- top: 200px;
- z-index: 1000;
- position: absolute;
- background-color: lightblue;
- text-align: center;
- width: 300px;
- height: 200px;
- }
- </style>
JavaScript
- <script type="text/javascript">
- t the class from DIV for activate a Modal.
- function DisplayModal(id) {
- document.getElementById("overlay-" + id).style.height = document.body.clientHeight + 'px';
- document.getElementById("overlay-" + id).className = "OverlayEffect";
- document.getElementById("modalMsg-" + id).className = "ShowModal";
- }
-
-
- move the class from DIV for disable Modal.
- function RemoveModal(id) {
- document.getElementById("overlay-" + id).style.height = "0px";
- document.getElementById("overlay-" + id).className = "";
- document.getElementById("modalMsg-" + id).className = "HideModal";
- }
- </script>
Step by Step implementation of HTML, CSS, and JavaScript based Modal Popup
Create an ASP.NET Empty Web Site project, named as “RepeaterModalPopup”.
To "Add" a new web form, right click on Solution Explorer or project and click on Add ---> Add new item option.
Set Web form file : Default.aspx
Drag and drop the Repeater control on the page.
Note - For data fetching, we have used the classic ADO.NET code that is SQL DataAdapter, DataSet, and assigned to repeater.
We have created a BindRepeater method that delivers the data to Repeater control.
- private void BindRepeater()
- {
- string constr = ConfigurationManager.ConnectionStrings["ModalConnectionString"].ConnectionString;
- SqlConnection _con = new SqlConnection(constr);
- SqlDataAdapter da = new SqlDataAdapter("Select * From tblFriends", _con);
- DataSet ds = new DataSet();
- da.Fill(ds);
- rptFriends.DataSource = ds.Tables[0];
- rptFriends.DataBind();
-
- }
Default.aspx page code
Default.aspx.cs page code
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- BindRepeater();
- }
-
- private void BindRepeater()
- {
- string constr = ConfigurationManager.ConnectionStrings["ModalConnectionString"].ConnectionString;
- SqlConnection _con = new SqlConnection(constr);
- SqlDataAdapter da = new SqlDataAdapter("Select * From tblFriends", _con);
- DataSet ds = new DataSet();
- da.Fill(ds);
- rptFriends.DataSource = ds.Tables[0];
- rptFriends.DataBind();
-
- }
-
- }
Output
By default, the page displays like the following.
On "Delete" button click, the "Delete Confirmation" modal popup will display.