ASP.NET Web Forms and Bootstrap 3 and 4 Modals

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

There are a few methods for hiding and showing Bootstrap 3.x and 4.x modals in an ASP.NET Web Forms application. 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.
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 {
filter: alpha(opacity=50);
-khtml-opacity: 0.5;
opacity: 0.5;

.modal-static {
bottom: auto;
display:block !important;

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 class="modal-body">
<p>My modal content</p>
<div class="modal-footer">
<asp:LinkButton ID="btnModalCloseFooter" runat="server" CssClass="btn btn-primary" OnClick="btnModalClose_Click" Text="Close"></asp:LinkButton>

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

What's new in Bootstrap v5

by John Avis | December 22, 2020

There are many changes in Bootstrap v5, but these are some of the stand-out changes I noticed when updating a website to v5.

How to use the new Bootstrap Icons v1.2 web font

by John Avis | December 12, 2020

With the release of Bootstrap Icons v1.2, there is now an easy-to-use web font. Here's how you can use the new web font in your web pages.

Intermittent "Unable to read data from the transport connection: net_io_connectionclosed" errors

by John Avis | May 6, 2020

If you are having intermittent problems sending email in .NET using System.Net.Mail consider switching libraries.



by Anonymous | January 4, 2018




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.



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?


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.


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; }



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.



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.


Leave a Comment



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

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


Get the latest posts delivered to your inbox.