ASP.NET modal popup not closing/hiding after javascript alert showing

BeUnique 2,332 Reputation points
2023-12-07T06:51:20.2033333+00:00

I am trying to hide/close modal popup window from my code behind after alert given.

below is the simple code.

what is the problem in the below code..

Dim Msg As String
Msg = "Rec. updated successfully."
   System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me.Page, Me.GetType(), "ClosePopup", "alert('" + Msg + "');window.location='window.close();'", True)
Developer technologies ASP.NET Other
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2023-12-07T07:57:10.5166667+00:00

    Hi @Gani_tpt,

    There is a problem with the following line of code. The way you wrote it is wrong.

    window.location='window.close();'

    The window.location object can be used to get the current page address (URL) and to redirect the browser to a new page.

    Window close():closes a window.

    The two should not be used together.

    If you want to use window.location, you can use window.location.href to return the href (URL) of the current page.

            Dim Msg As String
            Msg = "Rec. updated successfully."
            System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me.Page, Me.GetType(), "ClosePopup", "alert('" + Msg + "');window.location.href = 'https://localhost:44395/WebForm2';", True)
    

    Or you can close the ASP.NET modal popup directly:

      Dim Msg As String
            Msg = "Rec. updated successfully."
            System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me.Page, Me.GetType(), "ClosePopup", "alert('" + Msg + "');$('#YOURModalID').modal('hide');", True)
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.