Ramani Sandeep's Blog

DotNetting – Fast , Easy Way of Developing Applications

Archive for July 25th, 2008

Modal Popup Dialog Window in ASP.NET

Posted by ramanisandeep on July 25, 2008

This sample creates parent and child webforms. The child webform is called modally by the parent passing multiple values to the child form. The child form displays the passed values allowing them to be edited and then returns the altered values back to the parent when finished. The child form is modal to only the parent form. To make the child modal to the entire desktop, see the below final note.

1. Open a New Web project in Visual Studio
2.
Create two New WebForm pages named ParentWebForm and ChildWebForm
3.
Open the HTML surface for the ParentWebForm
4.
Locate the yellow line
5.
Delete all code EXCEPT the yellow line
6.
Insert the following HTML below the existing yellow line

<html>

   <head>

       <title>Parent Webform</title>

 

       <script language="javascript">
   1:  

   2:          function OpenChild()

   3:          {

   4:              var ParmA = retvalA.value;

   5:              var ParmB = retvalB.value;

   6:              var ParmC = retvalC.value;

   7:              var MyArgs = new Array(ParmA, ParmB, ParmC);

   8:              var WinSettings = "center:yes;resizable:no;dialogHeight:300px"

   9:              // ALTER BELOW LINE - supply correct URL for Child Form

  10:              var MyArgs = window.showModalDialog("http://localhost/ModalWin/ChildWebForm.aspx", MyArgs, WinSettings);

  11:              if (MyArgs == null)

  12:              {

  13:                  window.alert("Nothing returned from child. No changes made to input boxes");

  14:              }

  15:              else

  16:              {

  17:                  retvalA.value=MyArgs[0].toString();

  18:                  retvalB.value=MyArgs[1].toString();

  19:                  retvalC.value=MyArgs[2].toString();

  20:              }

  21:          }

  22:        

</script>

 

   </head>

   <body>

       <p>

           <input id="retvalA" type="text" value="AAA"></p>

       <p>

           <input id="retvalB" type="text" value="BBB"></p>

       <p>

           <input id="retvalC" type="text" value="CCC"></p>

       <p>

           <button onclick="OpenChild()" type="button">

               Open Child Window</button></p>

   </body>

   </html>

7. In the above code, locate the line saying //ALTER BELOW LINE

8. Supply the correct URL for your Child Webform

9. Open the HTML surface for the ChildWebForm

10. Locate the yellow line

11. Delete all code EXCEPT the yellow line

12. Insert the following HTML below the existing yellow line

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>

<head>

    <title>Child Webform</title>

 

    <script language="javascript">
   1:  

   2:         function Done() {

   3:         var ParmA = tbParamA.value;

   4:         var ParmB = tbParamB.value;

   5:         var ParmC = tbParamC.value;

   6:         var MyArgs = new Array(ParmA, ParmB, ParmC);

   7:         window.returnValue = MyArgs;

   8:         window.close();

   9:         }

  10:         function doInit() {

  11:         var ParmA = "Aparm";

  12:         var ParmB = "Bparm";

  13:         var ParmC = "Cparm";

  14:         var MyArgs = new Array(ParmA, ParmB, ParmC);

  15:         MyArgs = window.dialogArguments;

  16:         tbParamA.value = MyArgs[0].toString();

  17:         tbParamB.value = MyArgs[1].toString();

  18:         tbParamC.value = MyArgs[2].toString();

  19:         }

  20:     

</script>

   1:  

   2:  

   3: </head>

   4: <body onload="doInit()">

   5:     <p>

   6:         Param A:<input id="tbParamA" type="text"></p>

   7:     <p>

   8:         Param B:<input id="tbParamB" type="text"></p>

   9:     <p>

  10:         Param C:<input id="tbParamC" type="text"></p>

  11:     <button onclick="Done()" type="button">

  12:         OK</button>

  13: </body>

  14: </html>

  15:  

  16: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

  17: <html>

  18: <head>

  19:     <title>Child Webform</title>

  20:  

  21:     <script language="javascript">

  22:         function Done() {

  23:         var ParmA = tbParamA.value;

  24:         var ParmB = tbParamB.value;

  25:         var ParmC = tbParamC.value;

  26:         var MyArgs = new Array(ParmA, ParmB, ParmC);

  27:         window.returnValue = MyArgs;

  28:         window.close();

  29:         }

  30:         function doInit() {

  31:         var ParmA = "Aparm";

  32:         var ParmB = "Bparm";

  33:         var ParmC = "Cparm";

  34:         var MyArgs = new Array(ParmA, ParmB, ParmC);

  35:         MyArgs = window.dialogArguments;

  36:         tbParamA.value = MyArgs[0].toString();

  37:         tbParamB.value = MyArgs[1].toString();

  38:         tbParamC.value = MyArgs[2].toString();

  39:         }

  40:     

</script>

 

</head>

<body onload="doInit()">

    <p>

        Param A:<input id="Text1" type="text"></p>

    <p>

        Param B:<input id="Text2" type="text"></p>

    <p>

        Param C:<input id="Text3" type="text"></p>

    <button onclick="Done()" type="button">

        OK</button>

</body>

</html>

1

3. Set the project StartUp page to be the Parent Webform

14. Run the project.

Final Note: To make the child modal to the entire desktop, you’ll need add code to the child which uses

<body onblur="this.focus">

Hope this will Help !!!

Posted in ASP.NET Ajax | Tagged: | 5 Comments »