ASP.NET Web Forms and Bootstrap 3 and 4 Modals

johna by | July 6, 2017 | Bootstrap ASP.NET Web Forms Web Development

ASP.NET and Bootstrap 4 Modals

Update: this technique including CSS works without any changes for Bootstrap 4.0 too!

There are a few methods for hiding and showing Bootstrap 3.x modals in an ASP.NET Web Forms application, including a client script method I mentioned in a previous post.

This technique is pure ASP.NET (requires no client script) and allows you to show and hide modals by changing the Visible property of a containing PlaceHolder or Panel.

The advantage over this method over using client script and the native Bootstrap methods is that the modal will not be affected by full or partial postbacks and will be fully controlled by server-side ASP.NET.

A few new CSS classes are needed for this method:

.modal-static-backdrop {
-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
filter: alpha(opacity=50);
-moz-opacity:0.5;
-khtml-opacity: 0.5;
opacity: 0.5;
background:#000;
height:100%;
left:0;
position:fixed;
top:0;
width:100%;
z-index:2000;
}

.modal-static {
bottom: auto;
display:block !important;
position:absolute;
z-index:2001;
}

The Bootstrap modal itself needs a few tweaks:

<asp:PlaceHolder ID="ModalPlaceHolder" runat="server" Visible="false">
<div class="modal-static-backdrop"></div>
<div class="modal modal-static">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<asp:LinkButton ID="btnModalCloseHeader" runat="server" CssClass="close" OnClick="btnModalClose_Click"><span aria-hidden="true">×</span></asp:LinkButton>
<h4 class="modal-title">Sample Modal</h4>
</div>
<div class="modal-body">
<p>My modal content</p>
</div>
<div class="modal-footer">
<asp:LinkButton ID="btnModalCloseFooter" runat="server" CssClass="btn btn-primary" OnClick="btnModalClose_Click" Text="Close"></asp:LinkButton>
</div>
</div>
</div>
</div>
</asp:PlaceHolder>

Then to control showing and hiding of the modal you can just set the Visible property of the placeholder, eg:

ModalPlaceHolder.Visible = true; //or false to hide it

In my example above, the modal is closed using either of the buttons in the header and footer which reference a btnModalClose_Click event which simply sets the PlaceHolder's Visible property to false.

Note that you should not use any of Bootstrap's own decoration of elements, eg. data-dismiss="modal" otherwise you could lose server-side control of the modal.

Also note that the modal should be positioned outside of any element that has a CSS 'position' set, otherwise the modal will appear relative to that element. It is best to move your modals out to just within the BODY CSS element.

Put the PlaceHolder (or whatever containing control you use) inside an UpdatePanel and the modal will show and hide without a full postback.

Update: Be aware that the modal will always display at the top of the page, but the user may be scrolled further down the page. You should take steps to scroll to the top of the page when showing a modal.

If the modal is inside an UpdatePanel then you can use something like the following code when showing your modal.

ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "UpdatePanel1StartupScript", "setTimeout('window.scrollTo(0,0)', 0);", true);

Related Posts

Web Development

How to set up a debugging using the Turnkey Linux LAMP stack and VS Code

by johna | December 19, 2023
The second part in my guide to setting up a website and database using the Turnkey Linux LAMP stack.

Website Hosting Web Development

How to set up a website and database using the Turnkey Linux LAMP stack

by johna | November 18, 2023
If you need to host your own website for the purposes of web development, Turnkey Linux LAMP Stack is an easy to install all-in-one solution that you can set up on a spare computer or a VM (Virtual Machine).

Bootstrap

How I use the new Bootstrap Icons web font

by johna | January 7, 2022
But using a free online tool, you can create your own web font with just the Bootstrap icons you need.

Comments

Anonymous

by Anonymous | January 4, 2018

Awesome!

Reply

Wilhelmina

by Wilhelmina | November 22, 2018

Thank you, I've used your code and its working fine. However, when I integrated the bootstrap datepicker inside the modal, it is not working the datepicker does not show.

I have tried setting the z-index of the datepicker to a higher value than the modal, yet it still does not work. (see below)

datepicker {
z-index: 2002 !important;
}

If it's not too much could you please check what is causing this issue?

Note: I have tested the datepicker on a bootstrap modal and it is working fine.

Reply

John

by John | November 22, 2018

Hi Wilhelmina.

Can you give me a link to exactly which date picker you are using?

Have you got your non working page somewhere online where I can see it and experiment?

In your CSS in your comment you have only got "datepicker", not ".datepicker" (with a dot). Could that be the problem?

Reply

Jon Smith

by Jon Smith | November 27, 2018

Is there a work around so that the modal does not display at the top of the page. One of my requirements is that the web page maintains scroll position.

Reply

Jon Smith

by Jon Smith | November 27, 2018

I found a work around so that you don't have to scroll to the top of the page to see the modal. It's based on stack overflow article. Spam prevention won't allow me to post the link but you can find it by googling:

center div in the middle of the screen - even when page is scrolled up/down

Step 1 - Add an ID to the second div in the modal

<div id="modalPopup" class="modal modal-static">

Step 2 - Add the following styles:

<style type="text/css">
html, body{ min-height: 100%; }
#modalPopup{ height: 100%; left: 0; position: fixed; top: 0; width: 100%; }
#modalPopup .container{ height: 60%; left: 50%; position: absolute; top: 50%; z-index: 10; width: 60%; }
#modalPopup .container .text{ background: #fff; height: 100%; left: -50%; position: absolute; top: -50%; width: 100%; }
#modalPopup .bg{ background: rgba(0,0,0,0.5); height: 100%; left: 0; position: absolute; top: 0; width: 100%; z-index: 9; }
</style>

Reply

John

by John | November 27, 2018

Hi Jon. Thanks for the comment. I did try your changes and it works well... Except if the modal content is too big to fit on the screen. Then there is no way of scrolling to see the rest of the modal. This might not happen if you are only dealing with small dialog/confirmation boxes, but anything larger could be a problem, particularly on mobile devices.

Reply

John

by John | November 27, 2018

Jon, I was just experimenting a bit further with your changes. To accommodate modal-body content that won't fit on screen it is possible to add a vertical scrollbar to modal-body with a few CSS additions:

#modalPopup .modal-content { height:calc(100vh - 3.5rem); }
#modalPopup .modal-header, #modalPopup .modal-footer { flex:0 0 auto; }
#modalPopup .modal-body { overflow-y:scroll; }

I haven't done a lot of testing but this seems worth investigating if you may have content that might not fit on screen.

Reply

Leave a Comment

About

...random postings about web development and programming, Internet, computers and electronics topics.

I recommend ASPnix for web hosting and Crazy Domains for domain registration.

Subscribe

Get the latest posts delivered to your inbox.