Bootstrap Modals inside UpdatePanels in ASP.NET Web Forms

By · July 14, 2015 · 4 comments

I have been working on an ASP.NET Web Forms project that is being converted to Bootstrap 3 and have run into a few issues with Modals.

From this experience, here's how you can structure your markup for when you need to use Bootstrap Modals inside ASP.NET UpdatePanels.

The most important part of this is that you don't place Modals inside UpdatePanels.

Okay, so that contradicts a bit with what I said above, but I'm not suggesting you don't use UpdatePanels, just that you place them appropriately.

Bootstrap Modals don't work when the relevant parts of the DOM are altered after the page is initially rendered. This makes them a bit more difficult to implement in an ASP.NET Web Forms environment.

Let's look at an example where we have a list of records generated by a Repeater, each having an edit button which opens a Modal where the record can be edited.

I would wrap an UpdatePanel around the table, have the Modal outside of this UpdatePanel, but have a second UpdatePanel inside the Modal.

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<li><asp:LinkButton ID="Button1" runat="server" CommandArgument='<%# Eval("ID") %>' Text='<%# Eval("Title") %>'></asp:LinkButton></li>

<asp:Panel ID="Panel1" runat="server" CssClass="modal" DefaultButton="btnContactInsertSubmit" tabindex="-1" role="dialog" aria-labelledby="myLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<div class="modal-header">
<h4 class="modal-title" id=" myLabel ">Edit</h4>
<div class="modal-body">
<div class="modal-footer">
<asp:Button ID="Button2" runat="server" CssClass="btn btn-primary" OnClick="Button2_Click" Text="Submit" />
<asp:LinkButton ID="Button3" runat="server" CssClass="btn btn-link" OnClick="Button3_Click" Text="Cancel"></asp:LinkButton>

In the code behind we can handle the showing and hiding of the Modal by injecting some JavaScript on asynchronous PostBack, and selectively updating only the appropriate UpdatePanel.

protected void repContact_ItemCommand(object sender, RepeaterCommandEventArgs e)
int id = Utilities.IntParse(e.CommandArgument.ToString());
//Populate your form inside the Modal here
ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "show", "$(function () { $('#" + Panel1.ClientID + "').modal('show'); });", true);

protected void Button3_Click(object sender, EventArgs e)
ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "hide", "$(function () { $('#" + Panel1.ClientID + "').modal('hide'); });", true);

protected void Button2_Click(object sender, EventArgs e)
//Save your record here
ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "hide", "$(function () { $('#" + Panel1.ClientID + "').modal('hide'); });", true);

This solution has worked perfectly for me even in complex Web Forms scenarios.

Get the latest posts delivered to your inbox.

Comments (4)

# Posted by Mark · June 19, 2016

Thanks. I was looking for this


# Posted by Barry Michael Doyle · July 11, 2016

Thanks man! What a great help :) It helped me instantly with all my problems!!


Barry Michael Doyle
# Posted by .... · September 22, 2016

Thank You So Much.Great help.


# Posted by Francisco Huit · February 20, 2017

gracias, llevaba aproximadamente 35 horas buscando esta solución y lo único que me faltaba era hacer esto

ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "hide", "$(function () { $('#" + Panel1.ClientID + "').modal('hide'); });", true);

crear una función para llamar al modal y no llamarla directamente desde una etiqueta script


Francisco Huit
Leave a Comment

All comments are moderated and rel="nofollow" is in use. Avatars are sourced from – 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

Get the latest posts delivered to your inbox. *