출처 : http://www.dotnetjunkies.com/Article/A72FCFD7-3874-408A-8FCE-541BEC74C704.dcik


Returning value from the dialog box

OK! What about returning any value from the dialog box? Suppose you have opened a dialog box for some purpose and you want that it returns some value to the parent web form.

For returning any thing to the parent window from the child dialog box, javascript provides one attribute of the window object, that is

window.returnValue

to understand its working. Let create a small web project with two web forms named :

parent.aspx
child.aspx

Parent web form will have one textbox and a button. Look at the following code of the parent web form.

parent.aspx

<body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
    <asp:TextBox id="txtValue" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 24px" runat="server"></asp:TextBox>
    <asp:Button id="btnOpen" style="Z-INDEX: 102; LEFT: 176px; POSITION: absolute; TOP: 24px" runat="server" Text="Open..."></asp:Button>
  </form>
</body>

parent.aspx.vb

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  btnOpen.Attributes.Add("onclick", "var strReturn; strReturn=window.showModalDialog('child2.aspx',null,'status:no;dialogWidth:370px;dialogHeight:220px;dialogHide:true;help:no;scroll:no');if (strReturn != null) document.getElementById('txtValue').value=strReturn;")
End Sub

And, child web form which will have a textbox and two buttons. Look at the following code of the child page.

child2.aspx

<body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
    <asp:TextBox id="txtValue" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 24px" runat="server"></asp:TextBox>
    <asp:Button id="btnOK" style="Z-INDEX: 103; LEFT: 48px; POSITION: absolute; TOP: 56px" runat="server" Text="Ok" Width="56px"></asp:Button>
    <asp:Button id="btnCancel" style="Z-INDEX: 102; LEFT: 112px; POSITION: absolute; TOP: 56px" runat="server" Text="Cancel"></asp:Button>
  </form>
</body>

child2.aspx.vb

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  btnOK.Attributes.Add("onclick", "window.returnValue = document.getElementById('txtValue').value; window.close();")

  btnCancel.Attributes.Add("onclick", "window.close();")
End Sub

the purpose of this small module is, that when user click on the button of the parent form, it will open child.apsx file in the dialog box. In the child form user will enter some value in the textbox and it he click on the Ok button it will return that value to the parent form. With that it will update the textbox of the parent form. And of course, if user click on the Cancel button, it will simply close the child dialog box without doing anything.

In all of the above case, there is nothing to do with the ASP.NET. All of things are actually handle by the javascript. See the javascript code of the child web form.

<input name="txtValue" type="text" id="txtValue" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 24px" />

<input type="submit" name="btnOK" value="Ok" id="btnOK" onclick="window.returnValue = document.getElementById('txtValue').value; window.close();" style="width:56px;Z-INDEX: 103; LEFT: 48px; POSITION: absolute; TOP: 56px" />

<input type="submit" name="btnCancel" value="Cancel" id="btnCancel" onclick="window.close();" style="Z-INDEX: 102; LEFT: 112px; POSITION: absolute; TOP: 56px" />

In the child web form, the value to be return is assigned to the window.returnValue. And the return value is assigned to a variable in the parent web form. Thereafter, it assigned to the textbox.

The idea is that, window.returnValue returns some value from the child window to the parent window, and thereafter, you can do any thing with that returned value. You can even pass to the VB code using parameter of the query string of the document action.

There will be one limitation of using above method of returning value from the dialog box. And the limitation is that you can only return one value to the parent window.



What about if I want to return more then one value?

I am sorry, there is no remedy of that, but I usually overcome with this limitation by using session object. I simply put all values to the session object in the child window and there after I handle it in the parent window.

+ Recent posts