Search code examples
c#asp.netmodalpopupextender

Another modal popup extender question


I am trying to get a modal popup to work, it needs to be triggered in the Code behind.

 <asp:Button ID="btnModalPopUp" runat="server" Text="Button" Style="display: none" />
<asp:Panel ID="pnlModalPopup" runat="server" CssClass="modalPopup" Style="display: none"
    Width="233px">
    <div id="Div1" runat="server" cssclass="title">
        Modal text here.
        <asp:TextBox ID="txtEditComments" runat="server"></asp:TextBox>
    </div>
</asp:Panel>
<cc1:ModalPopupExtender ID="modalMessage" runat="server" TargetControlID="btnModalPopUp"
    PopupControlID="pnlModalPopup" BackgroundCssClass="modalBackground"         DropShadow="true"/>

Code behind:

protected void Page_Load(object sender, EventArgs e)
{
    modalMessage.Show();
}

Even though it hits the "modalMessage.Show();" code it doesn't show the modal panel.


Solution

  • Two solutions:

    The first solution:

    Remove Style="display:none" from the pnlModalPopup.

    The first solution is will cause the popup to "flash" on the screen when the page first loads, and then quickly disappear.

    The second solution:

    protected void Page_Load(object sender, EventArgs e)
    {
        pnlModalPopup.Style["display"] = "block";
        modalMessage.Show();
    }
    

    Recommendation: I would recommend using the second solution, that way the modal popup doesn't flicker and then disappear.

    Edit: I just tested your code:

    I just tested your code in a simple page that contained only the code that you provided...It worked like expected.

    Check the following:

    1. Is your modal popup is defined in an UpdatePanel that is conditionally updated?
    2. Check to make sure that the modal popup isn't defined in a Panel that has it's visibility set to false.
    3. If that doesn't work, then check if the modal popup is actually in the source code of the rendered web page.