C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

How to return value from the popup window?


Posted Date: 28 Sep 2005    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: Brainstorming GuyMember Level: Diamond    
Rating: 1 out of 5Points: 10



Hai All,
One more tutorial for the beginners. In this tutorial, we are going to see how we can return a value from the popup window to the main window.
We are going to use 2 pages. Webform1.aspx and Webform2.aspx with their corresponding code behind file.
This is a simple method on how you can return a value to the parent window from the popup window.
First Page - WebForm1.aspx
The webform1.aspx file has the following code.



<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language=javascript>
function popWin(llInp)
{
window.open("webform2.aspx","aa","width=300,height=300");
return false;
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 112px; POSITION: absolute; TOP: 152px"
runat="server" Width="176px"></asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 304px; POSITION: absolute; TOP: 152px" runat="server"
Width="80px" Text="Button"></asp:Button>
</form>
</body>
</HTML>



In the page load event of this page, put the following code.


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Button1.Attributes.Add("onClick", "return popWin('" & TextBox1.ID & "');")
End Sub


We are assigning the javascript popWin to the button click. This will take the ID to the javascript function.
The popWin() function passes the control ID to the WebForm2.aspx. Now we will look into the WebForm2.aspx.

Second Page - WebForm2.aspx
The WebForm2.aspx file as following code.



<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
function mainValues()
{
var lsString;
lsString=window.document.Form1['TextBox1'].value;
window.opener.parent.Form1['TextBox1'].value=lsString;
window.close();
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:TextBox id="TextBox1" style="Z-INDEX: 102; LEFT: 24px; POSITION: absolute; TOP: 48px" runat="server"
Width="144px"></asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 192px; POSITION: absolute; TOP: 48px" runat="server"
Width="72px" Text="Return"></asp:Button>
</form>
</body>
</HTML>



In the page_load event of this page, give the following code.


Button1.Attributes.Add("onClick", "return mainValues();")


We are assigning the control name to the hidden control. In the javascript function, we are just assigning the value to the parent window element TextBox1. You can also pass a control name as a parameter to the webform2.aspx and you can specify the control name in ur javascript file.
Hope it helps you a bit in understanding how you can return a value from the popupwindow.
Happy Coding!!!

Regards,
Brainstorming Guys aka Venkatarajan A



Responses

Author: Vikram    26 Feb 2006Member Level: Bronze   Points : 0
Sir, the article is really usefull. But can u plz explain why do u need the parameter llInp. I am just a beginner, so plz explain me

Regards
vikram



Author: ceema Mohan    22 Oct 2006Member Level: Bronze   Points : 0
Hello,

I am using the same function you have given. But I want to restrict the user from going back to the parent window if the condition spcified is not cottect like

If Res <> 1 Then
Label6.Text = "This parent code is already in use, please enter another Parent Code"
Else
btn6.Attributes.Add("onClick", "return mainValues();")
Session("PageSelection") = ""
End If

But for me, it's going always back to the parent even if the value of Res=0. Please advice.


Author: kevin    16 Jan 2008Member Level: Bronze   Points : 0
I very nice .net. However can you tell me how can I add it to drop down list on parent page.

Thank



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
How to return values from a popup window  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Tips for ASP.NET Application Performance Enhancement
Previous Resource: Treeview - XML, XPATH - Show part of XML Document
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use