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