Bootstrap Modals inside UpdatePanels in ASP.NET Web Forms

John Avis by | July 14, 2015 | Bootstrap ASP.NET Web Forms

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.
Update: I'm now using a purely server-side technique to control Bootstrap 3.x modals. See my new post.

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">
<ContentTemplate>
<ul>
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<li><asp:LinkButton ID="Button1" runat="server" CommandArgument='<%# Eval("ID") %>' Text='<%# Eval("Title") %>'></asp:LinkButton></li>
</ItemTemplate>
</asp:Repeater>
</ul>
</ContentTemplate>
</asp:UpdatePanel>

<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">
<ContentTemplate>
<div class="modal-header">
<h4 class="modal-title" id=" myLabel ">Edit</h4>
</div>
<div class="modal-body">
...
</div>
<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>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
</asp:Panel>

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);
UpdatePanel2.Update();
}

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

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

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

Update: I'm now using a purely server-side technique to control Bootstrap 3.x modals. See my new post.

Related Posts

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.


Bootstrap ASP.NET Web Forms

ASP.NET Web Forms and Bootstrap 3 Modals

by John Avis | July 6, 2017

There are a few methods for hiding and showing Bootstrap 3.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 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.

Comments

Mark

by Mark | June 19, 2016

Thanks. I was looking for this

Reply

Barry Michael Doyle

by Barry Michael Doyle | July 11, 2016

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

Reply

....

by .... | September 22, 2016

Thank You So Much.Great help.

Reply

Francisco Huit

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

Reply

Andrea Marchi

by Andrea Marchi | July 21, 2017

Thank you. I've solved all my problems.

Reply

SoiffSaidedox

by SoiffSaidedox | January 17, 2018

This field would mostly deal with hydrocarbon gases since they have the potential for being used as fuel.Astrology assists all of us realize what's going on, as well as how you can much better convey individuals internal dramas, this assist all of us to identify what exactly are individuals components as well as that shops they require. A “one site fits all” answer for one low price is not the answer when it comes to custom web design.First, you can try to think about calligraphy.Things you can do to reduce the amount of pain and pulling include??Gentle stretching of the Quadriceps?Strengthening your buttock muscles to reduce the load on the Quads?Taping techniques to redistribute the load going through the tendon?Strengthening other muscles around the hips so not as much strain goes through your knees. The construction equipment provided by these companies are well maintained and inspected for safety on a regular basis; they can be put to work as soon as they are delivered to the construction site. Second, you can also consider stamps idea. The more leads you have, the greater your success will be. Rate your energy level right now on a scale of zero to 10. Hacer una investigaci

Reply

Leave a Comment
Tags
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.

Subscribe

Get the latest posts delivered to your inbox.