Passing values from child window to parent window
By AzamSharp
Views: 3558

Introduction:

I know what you might be wondering, Where is AzamSharp? Well I ran away..... from hurricane RITA. Anyway I am back and I bring you yet another article. In this article we will see that how we can post the values from the child window to the parent window. Consider a situation in which you enter some data in the child window and want that data to be posted to the parent window. Let's see how this can be done.

Parent Window:

Our parent window contains a simple TextBox and a LinkButton. The TextBox will display the data which is sent from the child window and the LinkButton will simply opens the child window also known as the popup window. Below is the simple code that is used to attach the onclick attribute to the LinkButton.

protected void Page_Load(object sender, EventArgs e)

{

string openWindow = @"window.open('Child.aspx')";

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

}

Now let's see the child window. In the code below we attach the onclick event to the LinkButton which is on the Child page.

Child Window:

protected void Page_Load(object sender, EventArgs e)

{

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

}

Child Page also contains a TextBox. Whatever you type in the TextBox will be transferred to the parent page. Now all we need is JavaScript so that we can pass the information to the parent page.

<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>

This is it! Now when you type something in the TextBox of the child window and press the LinkButton that value is passed to the parent window and displayed in the TextBox of the parent window. After that the child window is automatically closed.

I hope you liked the article, happy coding!

 

By AzamSharp


Enter Comment/Feedback
  •  
  •  
  •  
  •  
  •  

Comments/Feedbacks
Subject: it's not working with IE
Name: sreenivas
Date: 7/18/2007 6:28:02 AM
Comment:
to get the value from child window to parent i used the same code what u provided.(window.opener.form1.txtUserName.value = txtValue;).In IE i am getting error as window.opener is null or not an object. please uide me to override this problem.
Subject: RE: Its not working in IE
Name: AzamSharp
Date: 7/19/2007 7:57:19 AM
Comment:
Hi Sreenivas,

Make sure you are using the right form element and your form id is form1. Also, how are you retrieving the values of txtValue.
Subject: passing values from child form to parent form in c#.net window forms
Name: jiaudheen
Date: 10/4/2007 1:35:27 AM
Comment:
i need to do the same described above for window application.....

i have a multiline textbox and a button in parent form. in the child form i have text boxes,comboboxes,button.
the thing is i click the button in the parent form ,the child form opens. i fill the text boxes,selct the value from combo box and click ok button in child form. on clicking the button these values shoud be passed to the multiline txt box of parent form and should be displayed there...
Subject: dotnet
Name: sandeep
Date: 4/24/2008 3:33:37 AM
Comment:
ur all articles
are very good



Join WebHost4Life.com






Copyright GridViewGuy 2007-2008