How to close a modaldialog window from button click event

In a ASP.Net 2 website I have a modaldialog window on which I have a save and close button. So the button saves data from the window to a database which works fine but how do I  implement the "close window" part of the event?
Al230762Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
In the front end:

  <script language="javascript" type="text/javascript">
        function RefreshParent() {
             /*  This calls a button on the parent in the parent if you want to trigger a post back for the parent          window.opener.__doPostBack('btnReloadInfo', '');*/
            window.close(); // this closes the window.
            return false;
        }      
      </script>  


backend:
   Page.ClientScript.RegisterStartupScript(this.GetTyp(), "Refresh", "RefreshParent();", true);

You may have to use a scriptmanager.RegisterStartupScript if you're using ajax in the modal.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Al230762Author Commented:
Thanks ged325. To clarify, we have a button in the modaldialog ("btnSave") which saves the data on the modaldialog to the database so in the backend we have:

        Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

SaveData()

End sub

I can see that window.close() would work but how would we have the "btnSave" button run the SaveData() function and then close the modaldiaog?
0
Al230762Author Commented:
Sorry ged325. I have now got this working well.

So I now have:

Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

SaveData()
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "Refresh", "closeWindow();", True)

End sub

and in the front end:

<script language="javascript" type="text/javascript">
        function closeWindow() {
            window.close();
            return false;
        }      
      </script>

Many thanks for your help
0
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Al230762Author Commented:
ged325 - just a supplementary question on this.

With Firefox and the above code, I just see a blank page with the word "null" at top-left when the modaldialog is closed. Any ideas?
0
Kyle AbrahamsSenior .Net DeveloperCommented:
How are you opening the modal dialog?
0
Al230762Author Commented:
This is how the modaldialog is opened from a button in a gridview by setting attributes of the button in the RowDataBound event of the gridview:


     Dim btnpop1 As LinkButton = DirectCast(e.Row.Cells(14).FindControl("lnkEdit"), LinkButton)
                    btnpop1.CommandArgument = grdInterventions.DataKeys(0).Value
                    If BrowserType Like "IE*" Or BrowserType Like "Firefox*" Then
                        btnpop1.OnClientClick = "javascript:window.showModalDialog('EditIntervention.aspx?InterID=" & Convert.ToInt32(grdInterventions.DataKeys(e.Row.RowIndex).Value) & "','','dialogWidth:1000px;dialogHeight:700px'); window.location = 'EditIntervention.aspx'; "
                    Else
                        btnpop1.OnClientClick = "window.open('EditIntervention.aspx?InterID=" & Convert.ToInt32(grdInterventions.DataKeys(e.Row.RowIndex).Value) & "','_self','width=1000,height=700,toolbar=no, location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=yes, resizable = yes');"
                    End If
0
Kyle AbrahamsSenior .Net DeveloperCommented:
http://stackoverflow.com/questions/13738964/how-to-close-showmodaldialog-in-firefox-chrome-in-javascript

You may have to use top.close in FireFox vs window.close for a modal.
0
Al230762Author Commented:
Problem solved - please note the schoolboy error in my code returning me to the same page:

      btnpop1.OnClientClick = "javascript:window.showModalDialog('EditIntervention.aspx? ........... window.location = 'EditIntervention.aspx'; "

many thanks for your help ged325
0
Kyle AbrahamsSenior .Net DeveloperCommented:
Glad you got it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.