Search code examples
asp.netmodalpopupextender

No target for dynamic Modal Popup with Master Pages


My dynamically added modal popup extender can't find the target control ID. I'm using master pages.

EDIT: It works great without MasterPages.

Web Form:

<asp:Content ID="Content3" ContentPlaceHolderID="cphContent" runat="server">
    <a runat="server" href="javascript:void(0);" ID="btnVote">vote</a>
    <asp:Panel ID="popScore" runat="server"></asp:Panel>
</asp:Content>

CodeBehind

protected void Page_Load(object sender, EventArgs e)
{
    ModalPopupExtender mpeScore = new ModalPopupExtender()
    {
        ID = "mpeSc",
        PopupControlID = popScore.ClientID,
        TargetControlID = btnVote.ClientID,
        BackgroundCssClass = "modalBackground",
        BehaviorID = "mpeScore"
    };
    this.Controls.Add(mpeScore);
}

Error message is

The TargetControlID of 'mpeSc' is not valid. A control with ID 'ctl00_cphContent_btnVote' could not be found.

I tried btnVote.ID also.


Solution

  • It took me days to figure this out, but you have to hook to the ResolveControlID event when using master pages or separate UpdatePanels

    ModalPopupExtender mpeScore = new ModalPopupExtender()
    {
        ID = "mpeSc",
        PopupControlID = popScore.ClientID,
        CancelControlID = cancelVote.ClientID,
        TargetControlID = btnVote.ID,   //will note resolve, done in exception
        BackgroundCssClass = "modalBackground",
        BehaviorID = "mpeScore"
    };
    mpeScore.ResolveControlID += mpe_ResolveControlID;
    this.Form.Controls.Add(mpeScore);
    

    You can use the same handler for all Modal Popups and resolve the controls like this

    protected void mpe_ResolveControlID(object sender, AjaxControlToolkit.ResolveControlEventArgs e)
    {
        switch (e.ControlID)
        {
            case "btnVote":
                e.Control = btnVote;
                break;
            case "btnComDummy":
                e.Control = btnComDummy;
                break;
        }
    }
    

    Glad I could help (me).