Passing Parameters from Child to Parent Window
The scenario is very simple, you have a child window which have input fields and you want to transfer the text/input from the child window to the parent window. By parent window I mean that window that opened the child window.
Here is the code for the parent window:
<form id="form1" runat="server">
<div>
<a href="#" onclick="OpenNewWindow()" >Open a new window</a>
<asp:TextBox ID="txtParent" runat="server" />
</div>
</form>
</body>
</html>
<script language = "javascript" type="text/javascript">
function OpenNewWindow()
{
var newWindow = window.open('PopUp.aspx','MyPopUp','width=200,height=200');
}
</
script> I am going to populate the txtParent textbox of the parent window from the child window.
Here is the child window:
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtUserName" runat="server" />
<a href="#" onclick="TransferText()" >Transfer Text to Parent Window</a>
</div>
</form>
</body>
</html>
<script language="javascript" type="text/javascript">
function TransferText()
{
window.opener.form1.txtParent.value = document.getElementById("txtUserName").value;
}
Yup! thats all to it. The window.opener can be used to refer back to the parent window. Please keep in mind that window.opener is not equal to window.parent which, is used in case of frames, iframes.