Bootstrap Modals inside UpdatePanels in ASP.NET Web Forms

(Not yet rated)

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.
Rate this post:

Related Posts

Electronics Web Development Retro

Another pointless project - the programmable digital watch

by johna | January 20, 2025
I've come up with yet another pointless project. Would you like a watch that you could program yourself - but not a "smart watch"?

Web Development Retro

Converting dBase IV programs to run in the browser

by johna | September 13, 2024
Some pointless entertainment trying to get some old dBase programs running in the browser.

Web Development

How to set up a debugging using the Turnkey Linux LAMP stack and VS Code

by johna | December 19, 2023
The second part in my guide to setting up a website and database using the Turnkey Linux LAMP stack.

Comments

Mark

by |

Thanks. I was looking for this

Reply

Barry Michael Doyle

by |

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

Reply

....

by |

Thank You So Much.Great help.

Reply

Francisco Huit

by |

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 |

Thank you. I've solved all my problems.

Reply

Yuki Gunawan

by |

Wonderful thanks you, i have been a week searching for this

Thanks you John Avis

Reply

VincentTaf

by |

it's a really great site!A lot of helpful information and handy ideas, thank you =)

Reply

David

by |

Small enhancement: dismissing the modal window is also possible by using a simple html button with attribute data-dismiss="modal"

Reply

Callisto

by |

Excelente aporte me ayudo bastante :D

Reply

Leave a Comment

About

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

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

Subscribe

Get the latest posts delivered to your inbox.