ASP.NET Web Forms and Bootstrap 3 Modals

By · July 6, 2017 · 0 comments

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.

Get the latest posts delivered to your inbox.

Comments

There are no comments yet. Be the first to leave a comment!

Leave a Comment

All comments are moderated and rel="nofollow" is in use. Avatars are sourced from gravatar.com – a globally recognised avatar.

Type the numbers from the picture above

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

profile for John at Stack Overflow, Q&A for professional and enthusiast programmers
Subscribe

Get the latest posts delivered to your inbox. *