Cannot close dialog from link inside same dialog - returns error

Cannot close dialog from link inside same dialog - returns error

Hi I have a issue which is  very common but I searched it and tried every possible solution but nothing worked.

My problem is I have a classic asp page default.asp, I defined a dialog in it + a div "mydiv" and a button which  opens the defined dialog in "mydiv" and loads default2.asp in this dialog.

Default2.asp is also a form to submit and has many links to products, when user clicks on one of those links it updates info to its parent page "Default.asp" and tries to close Default2.asp (the dialog)

It fails to close the dialog giving error
[   cannot  call methods on dialog prior to initialization; attempted to call method ' close ']

Please let me know what is wrong here. I asked this to various forums but no success yet.

The code is here
------------------------------------------------  Default.asp code starts from here --------------------------------------------------
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title></title>
  5. <script src="/JQuery/jquery-1.9.1.js"></script>
  6. <script src="/JQuery/jquery-ui.js"></script>
  7. <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />

  8. <script>
  9. $(document).ready(function(){
  10. var MyDlg = $( "#mydiv" );
  11.     MyDlg.dialog({
  12.                 autoOpen: false,
  13.                  closeOnEscape: true,
  14.                  resizable: true,
  15.                  draggable: true,
  16.                  modal: true,
  17.                  width: 300,
  18.                  height: 400,
  19.  buttons: {
  20.         Cancel: function() {
  21.           MyDlg.dialog( "close" );
  22.  }
  23.       }
  24.     });

  25. $("#Opendlg").click(function() {
  26.   MyDlg.load("Default2.asp").dialog("open");
  27. });

  28. });
  29. </script>

  30. </head>
  31. <body>
  32. This is a test form-1
  33. <br><br>

  34. <% 
  35. Dim FName, LName
  36. FName = Request.form("fname")
  37. LName = Request.form("lname")

  38. Response.write("The First name was := " & FName & " and Last name was:=" & LName )
  39. %>

  40. <br><br>
  41. <form ID=Form1 method="post" action="Default.asp">
  42. First Name: <input type="text" name="fname"><br>
  43. Last Name: <input type="text" name="lname"><br><br>
  44. <input type="submit" value="Submit">
  45. </form>
  46. <br><br>

  47. <input type="button" ID="Opendlg" value="Open Dialog"/>

  48. <div id="mydiv" title="Product Wizard" >
  49. </div>

  50. </body>
  51. </html>
-----------------------------------------------------  Default2.asp Starts from here -----------------------------------------------------
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Default2.asp</title>
  5. <script src="/JQuery/jquery-1.9.1.js"></script>
  6. <script src="/JQuery/jquery-ui.js"></script>
  7. <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />


  8. <script type="text/javascript">
  9. $(function() {
  10.     $('#Form2').submit(function(evt) {
  11.         evt.preventDefault();
  12.         $.ajax({
  13.             url: "Default2.asp", 
  14.             type: 'POST',
  15.             data: $(this).serialize(),
  16.             success: function(result) {
  17. $('#mydiv').html(result);
  18.             }
  19.         });
  20.     });
  21. });

  22. function closeThis()
  23. {
  24.  // $("#mydiv").hide();
  25.   $("#mydiv").dialog("close");
  26. }
  27. </script>

  28. </head>
  29. <body>
  30. This is Default2.asp file
  31. <a href="JavaScript:closeThis();">Close This Dialog</a>
  32. <br><br>

  33. <% 
  34. Dim Name, Address
  35. Name = Request.form("Name")
  36. Address = Request.form("address")

  37. Response.write("The Name was " & Name & " and Address was " & Address )
  38. %>

  39. <br><br>
  40. <form ID=Form2 >
  41. Name: <input type="text" name="Name"><br>
  42. Address: <input type="text" name="address"><br><br>
  43. <input type="submit" value="Submit">
  44. This is just a test
  45. </form>

  46. </body>
  47. </html>