Ali Ersöz
Ali Ersöz

Reputation: 16086

How do I prevent the closing of modal popup window(ModalPopupExtender) on postback?

I'm using Microsoft AjaxControlToolkit for modal popup window.

And on a modal popup window, when a postback occurred, the window was closing. How do I prevent from the closing action of the modal popup?

Upvotes: 15

Views: 45454

Answers (6)

Darrel Lee
Darrel Lee

Reputation: 2470

Was having this same problem keeping a modal open during postbacks.

My solution:

Use EventTarget to determine if the postback is coming from a control in the modal and keep the model open if it is. The postback can come from a control in the modal iff the modal is open.

In the load event for the page control containing the modal. Determine if the postback is from a child of mine. Determine if it is from the control that is in the modal panel.

    Protected Sub Control_Load(sende As Object, e As EventArgs) Handles Me.Load
        If IsPostBack Then
            Dim eventTarget As String = Page.Request.Params.Get("__EventTarget")
            Dim eventArgs As String = Page.Request.Params.Get("__EventArgument")

            If Not String.IsNullOrEmpty(eventTarget) AndAlso eventTarget.StartsWith(Me.UniqueID) Then
                If eventTarget.Contains("$" + _credentialBuilder.ID + "$") Then
                    ' Postback from credential builder modal.  Keep it open.
                    showCredentialBuilder = True
                End If
            End If
        End If
    End Sub

In prerender check my flag and manually show the modal

    Protected Sub Control_PreRender(ByVal sende As Object, ByVal e As EventArgs) Handles Me.PreRender
        If showCredentialBuilder Then
            _mpeCredentialEditor.Show()
        End If
    End Sub

Upvotes: 3

Gregor Primar
Gregor Primar

Reputation: 6805

Put you controls inside the update panel. Please see my sample code, pnlControls is control that holds controls that will be displayed on popup:

<asp:Panel ID="pnlControls" runat="server">

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
               <asp:Button ID="TestButton" runat="server" Text="Test Button" onclick="TestButton_Click" />
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>         
        </ContentTemplate>

    </asp:UpdatePanel>

This will do the job for you :)

Best regards, Gregor Primar

Upvotes: 12

Ali Ers&#246;z
Ali Ers&#246;z

Reputation: 16086

I guess that works but not in my case. I've a user control that opened in a modal popup and this user control makes postback itself. So in that user control I've no modal popup property.

I guess, I've to create an event for my user control, and the page that opens the modal popup have to reopen it in this event.

Upvotes: 1

Ricky Supit
Ricky Supit

Reputation: 3400

You can call Show() method during postback to prevent the modal popup window from closing

MyModalPopoupExtender.Show()

Upvotes: 10

ForceMagic
ForceMagic

Reputation: 6378

Like you prolly already know, the modal popup is clientside only, yeah you can gather informations in it during the postback, but if you do a postback he will hide 100% of the time.

Of course, like other proposed, you can do a .show during the postback, but it depends on what you need to do.

Actually, I don't know why you need a postback, if it's for some validations try to do them clientside.

Could you tell us why you need to do a postback, maybe we could help you better ! :)

Upvotes: 1

Jon Erickson
Jon Erickson

Reputation: 114876

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {
        // reshow
        MyModalPopup.Show()
    }
}

Upvotes: 4

Related Questions