Dynamic ModalPopupExtender not firing the OK Click event

ASP.NET 4.7.2 Web Forms c# VS 2019

I am trying to use a modalpopupextender to prompt for new data for foreign key fields. Like the form itself, the MPE is built on the fly in code — in this case the click handler for the hidden button that the Javascript fires off to build and show the MPE.

I read every single article on SO and the ASP forums and tried everything I saw there. No joy. I get the popup perfectly. Hitting OK closes the popup, but never fires the OK Event.

Here is the code:

    //Building the form, we do this in OnInit:
    // AJAX Update Panel
    UpdatePanel PUP = new UpdatePanel()
        ID = "PUP",
    // HiddenField containing the field name to permit 
    // creating the correct modalpopup.
    HiddenField HFPopupField = new HiddenField()
        ID = "HF_POPUP"
    // Create Hidden button to track the popup
    Button BPopup = new Button()
        ID = "BPOPUP",
        UseSubmitBehavior = false  
    BPopup.Click += BPopup_Click;
    BPopup.Attributes.Add("style", "display: none;");
    // And create the background panel for the popup.
    Panel PnlPopup = new Panel()
        ID = "PNLPOPUP",
        CssClass = "MpeBackground"
    PnlPopup.Attributes.Add("style", "display: none;");

    /// Event handler for hidden button.
    protected void BPopup_Click(object sender, EventArgs e)
        [snip -- code to get the dataset that is being filled]
        UpdatePanel PUP = Placeholder.FindControlRecursive("PUP");
        Table T = new Table()
            CssClass = "PopupTbl"
        TableRow TRTitle = new TableRow();
        TableCell TCTitle = new TableCell()
            CssClass = "PopupTitle",
            ColumnSpan = 2
        Label LPopTitle = new Label()
            Text = [title of the popup]
        DataRow drData = null;
        // Add Fields, and also the cancel and Add buttons
        foreach (DataColumn DC in dsColumns.Tables[0].Columns)
            TableRow TRColumn = [create a tablerow with 2 columns, a prompt and the input field]
            if (TRColumn != null) 
        }   // end of foreach(DataColumn DC in dsColumns.Tables[0].Columns)  
        TableRow TRButtons = new TableRow();
        TableCell TCButtons = new TableCell()
            ColumnSpan = 2,   
        Button MPEBOK = new Button()
            ID = "MPE" + sFieldName + "_MPEBOK",
            Text = "OK",
            CausesValidation = false,
            UseSubmitBehavior = false
        MPEBOK.Click += MPEBOK_Clicked;
        LiteralControl LCB = new LiteralControl()
            Text = "  "
        //*** Postback Trigger                                     ***
        AsyncPostBackTrigger trigger = new AsyncPostBackTrigger()
            ControlID = MPEBOK.ID,
            EventName = "click"
        //*** Cancel Button                                        ***
        Button MPEBuhBye = new Button()
            ID = "MPE" + sFieldName + "_BUHBYE",
            Text = "Cancel",
            UseSubmitBehavior = false
        AjaxControlToolkit.ModalPopupExtender MPE = new AjaxControlToolkit.ModalPopupExtender()
            ID = "MPE" + sFieldName,
            PopupControlID = "PNLPOPUP",
            TargetControlID = "BPOPUP",
            BackgroundCssClass = "MpeBackground"
        // Add the MPE to the UpdatePanel.
        // Show the modal popup extender.

    protected void MPEBOK_Clicked(object sender, EventArgs e)
        [snip - this never fires]

I cannot find out what is happening here. Can anyone see something hinky?



Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can’t add a server side button or inject a server side button into the page DOM.

When you drag a asp.net button onto the form, BOTH the “mypage.cs” and mypage.desinger.cs ARE updated. The wire up of the button occurs at design time, and you would have to modify mypage.desinger.cs ALSO and ADD a button event stub.

So you can’t do this.

A compromise would be to also add some js and have that HTML button execute a .click() method of a hidden asp.net button you drop into that page (that would give you the post back, and the running behind of a separate button event code stub.

This event resolution occurs at compile time – not at page render time. You have to drop that button onto the page.

I suppose you could adopt a standard that you always place right below that “div” on the page the button (hidden with style=none. And then as noted, have your injected code along with some js execute a click on the hidden button. Or just have the js button code execute a __doPostback(“some value”) and pick this up in the page on-load event, and then call the routine (function) from on-page load event.

I think better would be to use a jQuery.UI dialog, as that dialog CAN say load + use another different web page into a “div” on the existing page. So you layout, make, and create the nice looking popup form as a separate web page. jQuery is able to remove the “form” and additonal tags out of that page load, and then inject it into the existing page. (that code would be rather hard to re-produce). so jQuery.UI is able to pop up that separate page. however, the buttons on that loaded page (into that div) of course can’t really run any code behind in the current page. However, the buttons CAN run local js in the current page. Thus the actions of this injected page would be local to each page. But the popup would not be directly calling a code behind stub.

Now, to adopt jQuery.UI, then you also have to of course adopt jQuery. So that is two extra libraries you need. (but, jQuery you likely already have).

However, I suppose the whole point of using the ajax toolkit is to avoid jQuery.ui in the first place. To be fair, before jQuery.ui came along, that tool kit was REALLY impressive, and gave asp.net folks a REAL leg up on the competition. (and it tends to be MUCH less wiring up then say using jQuery.UI

So the AjaxToolkit in its heyday was impressive. Now, it of course showing its age, but I still use the kit, and this is especially the case for the AjaxFileUploader. And yes I do use the popups – even to this day. However, I find now that jQuery.UI dialogs are more flexible, and would be better in this case (because you want a on-the fly setup).

Also, having code behind buttons in even the jQuery.UI dialog, or in this case the ajax popup? Well, only the action button can run code behind. The cancel button of course will just dismiss the dialog. However, any button in the dialog that WILL run code behind? Well, that’s ok, since you have a page post back, and it actually the page postback that BLOWS out the dialog anyway.

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x