共用方式為


How to close an ASP.NET modal popup having a usercontrol from the code in popup's usercontrol itself when using Ajax Control Toolkit's ModalPopupExtender

Lots of popup controls (having user control inside them) we use in ASP.NET webpages need a close/hide button inside the user-control. But it is not possible to close the popup from inside the control's code directly as the ModalPopupExtender object is not available in there. So mostly we keep these close buttons outside the control on the main page itself.

Below is a simple pattern to solve this issue. I hope it will save some time for anyone stuck in this situation.

Add an event called 'Close' to the page/view class of popup. First define a delegate for the event.

public delegate void CloseModal();

public event CloseModal Close;

Add a handler for this event in the page_load of the main page/control which contains this popup. This code should execute for all requests to the page not just first one so don't keep it in the code segment which executes for non-postback requests only.

DetailsViewPart.Close += new CloseModal(DetailsViewPart_Close);

In the handler function write the code to hide this popup as usual.

void DetailsViewPart_Close()
{
    DetailsModalPopupExtender.Hide();
}

Now add a function in the popup usercontrol to close it.

public void CloseMe()
{
    if (Close != null)
        Close();
}

Now simply call usercontrol.CloseMe() from anywhere to close the modal popup.

Comments

  • Anonymous
    August 29, 2008
    PingBack from http://informationsfunnywallpaper.cn/?p=3026

  • Anonymous
    February 03, 2011
    Hi! I liked the post but I think I'm a lil bit stupid or so, because as I understand there is no declaration of "Close" in the user control, so I'm getting a compilation error in the function CloseMe(). Can you help me to solve this? Thx!

  • Anonymous
    March 24, 2011
    What if my Submit button is inside the ajax control?

  • Anonymous
    August 09, 2012
    Thanks friend. You have saved my life.