Passing values from the child window to the parent window

Sometimes we are in a situation in which we need to pass the values from the Child window to the parent window. Here is a code snippet that does the operation.

Parent window:

Parent window contains a textbox in which the value will be passed from the child window. And also it contains a linkbutton which is used to open the child window:

protected void Page_Load(object sender, EventArgs e)
    {
        
string openWindow = @"window.open('Child.aspx')";

        
this.LinkButton1.Attributes.Add("onclick", openWindow); 
       
    }

Now here is the child window:

protected void Page_Load(object sender, EventArgs e)

{

this.LinkButton1.Attributes.Add("onclick", "PassValues()");

}

And the child window html code:

<script language=javascript>

function PassValues()

{

var txtValue = document.getElementById("TextBox1").value;

// Now pass the value to the Parent form

window.opener.form1.txtUserName.value = txtValue;

window.close();

}

</script>

Yup! thats pretty much it. Now when you click the linkButton of the child window then the child window will be closed and the textbox value is passed from the child window to the parent window.

powered by IMHO

 

Print | posted @ Tuesday, September 27, 2005 1:36 PM

Twitter