ASP.NET Web Forms and Bootstrap 3 and 4 Modals

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

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

ASP.NET Web Forms

Nicer localised UpdateProgress’ for your ASP.NET pages with multiple UpdatePanels

by John Avis | February 9, 2018

On an ASP.NET Web Forms page with multiple UpdatePanels it’s nice to give the user some feedback when one of the panels is being updated.

Bootstrap Cool Websites

Bootstrap 4 beta Customiser

by John Avis | October 31, 2017

I love Bootstrap v4 and now that it is finally in Beta can start to use it in new projects. But I miss the online customizer that was available for Bootstrap v3.

ASP.NET Web Forms

Disable common UpdateProgress controls for an UpdatePanel with an associated UpdateProgress

by John Avis | April 12, 2017

If you have an ASP.NET Web Forms page with multiple UpdatePanels and multiple UpdateProgress controls, sometimes you may want one or more UpdateProgress controls associated with a specific UpdatePanel, and one or more UpdateProgress controls that have no association with an UpdatePanel so are shown when any UpdatePanel is updated.



by Anonymous | January 4, 2018



Leave a Comment
ASP.NET Html Forms ASP.NET MVC ASP.NET Web Forms ASP.NET Web Pages Bootstrap C# Classic ASP Cool Websites Databases eBay and PayPal Electrical Repairs General Hardware HTML/CSS Jquery/Javascript Media Center Mobile Phones Responsive Web Design SEO and Social Networking Web Design Web Development Web Security web+db Website Hosting Windows XP

About me

...mostly about web development and programming, with a little bit of anything else related to the Internet, computers and technology.


Get the latest posts delivered to your inbox.