Search code examples
javascriptasp.netajaxmodalpopupextendercode-behind

ASP.NET show ModalPopupExtender from Buttons created in Code-Behind


Hi I have a modalPopupExtender in my aspx web page pointing to a Panel and in Code-Behind I create buttons which I want them to show the modalPopup, so I have:

buttonX.OnClientClick = "javascript:$get(" + modalPopup.ClientID + ").show();";

but instead it just does a PostBack, even if I put "return false;" at the end of the past code.


Solution

  • I think I have figured something out although I'm not sure it's the best solution.

    This is my code-behind:

    protected void Page_Load(object sender, EventArgs e)
        {
            Button btn = new Button();
            btn.Text = "Hello World!";
            btn.OnClientClick = "javascript:$get('" + HiddenField.ClientID + "').click(); return false;";
            PanelHello.Controls.Add(btn);
        }
    

    and this is my ASPX page:

    <asp:HiddenField ID="HiddenField" runat="server" />
    <asp:ModalPopupExtender ID="modalPopup" runat="server" TargetControlID="HiddenField"
        PopupControlID="Panel" DynamicServicePath="" Enabled="True" />
    <asp:Panel ID="Panel" runat="server">
        <h1>
            Hello World!</h1>
    </asp:Panel>
    

    And this makes the modal pop up without posting back =D!