Modal PopUp Overlay
Introduction
This time I will share how to cause the same effect as the ModalPopUpExtender from the AjaxControlToolkit in Asp.Net using CSS and Ajax. We'll be able to create Master/Details, LookUps, Error Messages, or whatever other use you want to give it! There're a lot of approaches out there, so, probably nothing new... Just my way :-)
Background
Well, if there's a ModalPopUpExtender in the AjaxControlToolkit, why not to use it and do something different?
My scenario was such a pain for that. Let me explain:
I have Master Pages, Web Pages, Content Holders and User Controls. User Controls are loaded dynamically when used as "PopUps". I started to search how to convert them into "PopUp Windows" and found two different ways that thought would work.
The firts try: ModalPopUpExtender.So I prepare everything: CSS background class, PopUpPanel, OkControl, CancelControl, etc... It looked great... but... whenever I clicked or used any control that would cause a Postback, the PopUp hid. Well, there's a workaround for that using Javascript to prevent the control itself to cause a postback or also we could try to reset the "state" (hidden or shown) of the popup after the postback... Too much work for me. If you remember, I'm loading dynamically usercontrols into the PopUpPanel... So I decided to use the second option.
The second try: Well, the second try was using simple HTMLand CSS. I found an example where with two Divs and CSS could be achieved. It look familiar for me because that's the way sometimes I show UpdateProgress as ModalPopUp overlaying the page... How coudn't I think of it before? Well, in this case, it needed a little more than that, a Javascript function that would show and hide the Divs when needed. So, I started to try to call the Javascript function from code in the common different ways but due to my scenario (UserControls inside Panels inside UpdatePanels inside ContentHolders in a WebPage inside a MasterPage) I faced an error from the server trying to parse the request. So, as I was not able to change that structure to avoid the error, I thought that as I'm using Ajax, it would probably work with some changes, and that's what I did.
The code and the solution
Let's add an Update Panel. at the end of our Html (of course inside the content tag). Now, in the UpdatePanel ContentTemplate we'll just add two panels, one for the Overlay, and one for the PopUp.
The second panel (panelPopUpPanel in the example below) has a nested panel, wich I use to create the "Title" bar: In the sample below, it just contains the "Close" button.
After this nested panel (still inside the PopUpPanel) add whatever you want to display there (in the sample below, it contains a User Control call ucPopUpUsersQuickSearch)
<asp:updatepanel id="upPopUps" runat="server" updatemode="Always">
<contenttemplate>
<asp:panel id="panelOverlay" runat="server" class="Overlay" visible="false">
<asp:panel id="panelPopUpPanel" runat="server" class="PopUpPanel" visible="false">
<asp:panel id="panelPopUpTitle" runat="server" style="width: 100%; height: 20px; text-align: right; ">
<asp:imagebutton id="cmdClosePopUp" runat="server" imageurl="~/Images/Close.png">
</asp:imagebutton></asp:panel>
<uc:usersquicksearch id="ucPopUpUsersQuickSearch" runat="server" visible="false">
</uc:usersquicksearch> </asp:panel>
</asp:panel></contenttemplate>
</asp:updatepanel>
Notice that both panels (Overlay and PopUp) have the Class attribute set. Here is the Css to give the OverlayPanel an overlay effect and to give the PopUpPanel the position and size and border and whatever you want to specify for it. Play with it to change the appearance in a way you like it.
.Overlay {
position:fixed;
top:0px;
bottom:0px;
left:0px;
right:0px;
overflow:hidden;
padding:0;
margin:0;
background-color:#000;
filter:alpha(opacity=50);
opacity:0.5;
z-index:1000;
}
.PopUpPanel {
position:absolute;
background-color: #737373;
top:100px;
left:15%;
z-index:2001;
padding:10px;
min-width:400px;
max-width:600px;
-moz-box-shadow:3.5px 4px 5px #000000;
-webkit-box-shadow:3.5px 4px 5px #000000;
box-shadow:3.5px 4px 5px #000000;
border-radius:5px;
-moz-border-radiux:5px;
-webkit-border-radiux:5px;
border: 1px solid #CCCCCC;
}
Notice the Overlay position. It is set to "fixed". Why? If you have a large page that needs scrolling, with "fixed" you will overlay all the page even if scrolling.
So, how do we show the Pop Up? Have you notice that the panels have their Visible attribute to false? Just change that attribute to show them or hide them, like this:
Private Sub showPopUp(b As Boolean)
panelOverlay.Visible = b
panelPopUpPanel.Visible = b
End Sub
That was just a function that receives a boolean parameter (true or false) and sets the Visible attribute of the panels to the value of the parameter received.
And that's it, easy huh? Here is how it looks!!
Final Comments
As said, this can be used in many different scenarios: Update Progress, Quick Searchs, Master Detail pages, etc. Please feel free to try and change it. If you improve it, please share it. If you want to download a sample code, it is available in the beginning of this article. Hope it helps!
Thanks for reading!
发表评论
SefPFR Hey, thanks for the blog.Really thank you! Want more.
5SOf1e Thanks for sharing, this is a fantastic blog post.Really thank you! Will read on...