Solved

ASP.NET + Popup window return value + Javascript

Posted on 2004-08-02
18
1,070 Views
Last Modified: 2008-02-07
I have a popup window that has a flash movie, at the end of the flash movie i have a close window button, using javascript.  Is there anyway of sending a value back to the main ASP.net so it will refresh or reload?
0
Comment
Question by:dkilby
  • 7
  • 6
  • 4
  • +1
18 Comments
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11696656
yes you can send data to the parent page from the popup page.
For example, I have a little calendar image on a page, clicking on which pops up a a calender window. When a date is selected from the calendar, it sends the date back to the main page and display the date in a textBox.

My Calender Page:
=============

Public Class PopUpCalendar
    Inherits System.Web.UI.Page
    Protected WithEvents control As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents calDate As System.Web.UI.WebControls.Calendar

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        control.Value = Request.QueryString("textbox").ToString()
    End Sub

    Private Sub calDate_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calDate.SelectionChanged
        Dim strScript As String = "<script>window.opener.document.forms(0)." + control.Value + ".value = '"
        strScript += calDate.SelectedDate.ToString("MM/dd/yyyy")
        strScript += "';self.close()"
        strScript += "</" + "script>"
        RegisterClientScriptBlock("anything", strScript)
    End Sub
End Class
=================================================

In the main page, I am using:

onclick="window.open('popupCalendar.aspx?textbox=TB_Date','cal','width=355,height=200,left=270,top=180')" href="javascript:;">

** "TB_Date" is the TextBox control where I want to display the selected date.

-Baan
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11696851
One way would be to put an onFocus event handler on the "parent" page, so that when it got the focus, it checked if the window was still open, and took action accordingly.
0
 

Author Comment

by:dkilby
ID: 11696873
Crescendo, How would i do that though
0
 

Author Comment

by:dkilby
ID: 11697104
Baan

If i just want to send a value back when the close window button is pressed, what code do i need?  right now all i have is window.close.  I kind of understand what you have but not fully.

Thanks for the help
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11697242
ok...
Go to the event handler for that button... and add this

        Dim strScript As String = "<script>window.opener.document.forms(0)." + control.Value + ".value = '"
        strScript += your data that you want to send back
        strScript += "';self.close()"
        strScript += "</" + "script>"
        RegisterClientScriptBlock("MyScript", strScript)

=====
How are you showing the popup... ?  I need more information to tell you exactly what you need
-Baan
0
 

Author Comment

by:dkilby
ID: 11697324
to get the popup to come up this is the script.

btn_playNow.Attributes.Add("OnClick", "javascript:window.open('Game/testclose.htm','Played','width=500,height=400,menubar=no,resizable=no,left=150,top=150')")


the code to close the window is Flash and the code is

getURL("javascript:close()");
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11697443
Excellent.... I need a little more info. Where do you want to send the data value from the popup window ?
For example;  get a textbox in your main page, you can make it invisible. Say you have the textbox (TB1), then...
It should be like this...
btn_playNow.Attributes.Add("OnClick", "javascript:window.open('Game/testclose.htm?textbox=TB1','Played','width=500,height=400,menubar=no,resizable=no,left=150,top=150')")

In the Page_Load of the PopUp page, get the control name...

Dim Ctrl As String
ctrl =  Request.QueryString("textbox").ToString()

Then, when you are closing the form, just add the script (In here, I am assuming that you have a btn, clickin on which will close the PopUp page and send the value):
So inside the Button's Clicked event...
        Dim strScript As String = "<script>window.opener.document.forms(0)." + ctrl + ".value = '"
        strScript += your data that you want to send back
        strScript += "';self.close()"
        strScript += "</" + "script>"
        RegisterClientScriptBlock("MyScript", strScript)

-Baan
0
 

Author Comment

by:dkilby
ID: 11697487
I want to send the data value back to the main page, the problem is the Close Window is in Flash, i can not add the asp.net script in flash, or is the script for the button click event actionscript?
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11697587
Hmm... I see,  it is an html form, not an ASP/ASPX Webform. Can you access the CLOSE event of that form or the flash object?
wait... can you use session variables ? I am not sure if you can...
-Baan
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Expert Comment

by:crescendo
ID: 11697743
Here's the javascript, modify it for your own needs. It's a whole web page you can test with. When you click the button, it opens Google in a new window, if you close Google and go back to the original page, it detects the closure and opens Microsoft. Simply substitute your own sites/pages.

<html>
<head>
<title>This is the opening page</title>
</head>

<script Language=JavaScript>

  var win = null;
  var nms = "Neil is initialised";

  function butOpenWin_onclick()
  {
    win = window.open("http://www.google.co.uk", "myWindow","resizable=yes");
  }

  function gotFocus()
  {
    if (win && win.closed) location.href = "http://www.microsoft.com";
  }
</script>


<body onfocus="gotFocus()">
<input type="button" value="Open" onClick="butOpenWin_onclick()">
<p>Some text to click in............</p>
<p>Some text to click in............</p>
<p>Some text to click in............</p>
<p>Some text to click in............</p>
<p>Some text to click in............</p>
<p>Some text to click in............</p>
<p>Some text to click in............</p>
<p>Some text to click in............</p>
<p>Some text to click in............</p>
<p>Some text to click in............</p>
</body>
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11697749
PS It's straight HTML and Javascript so should work with your page easily.
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11697979
And if you simply want to reload the page, replace

    location.href = "http://www.microsoft.com";

with

    window.location.reload();

PS ignore the "Neil is initialised" bit - that was left in from testing!
0
 

Author Comment

by:dkilby
ID: 11699123
Crescendo, i tried the code and it worked in a html button, i tried it in a ASP.NET button and can not get it to work.

I have the javascript at the top of the page and the onFocus in the body tag,  I have added this code to the asp.net button, it opens the window no problem, but when i close the window nothing happens.  Any suggestions.

                btn_playNow.Attributes.Add("OnClick", "javascript:butOpenWin_onclick()")
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11699137
Have you put the onFocus= code in the BODY tag?
0
 

Author Comment

by:dkilby
ID: 11699151
<body onfocus="gotFocus()">  

if i put a HTML button on the same page it works,  i really need to use a ASP.NET button if possible
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11699316
I think it is because the ASP:Button is causing a postback, and that is causing the var "win" to be reinitialised, so it can't tell if the window is open or not when it gets the focus.

This means that you can't do anything with the first page until the second page closes. I'll see if I can find a workaround.
0
 
LVL 9

Accepted Solution

by:
crescendo earned 500 total points
ID: 11701527
dkilby:

I asked around and the answer is so simple. In the new window, where you use Javascript to close the window, put

    opener.location.href=opener.location.href;
    window.close();

This will tell the original window to reload itself. If you want to be really careful and check that the original window still exists, change the first line to

    if (window.opener) opener.location.href=opener.location.href;

Hope this does it.
0
 
LVL 2

Expert Comment

by:dlarlick
ID: 11714797

  Check out the following article

  "PopUp the .NET Way.."

  http://www.dotnetjohn.com/articles.aspx?articleid=88

  This article talks about using a popup for window for just about any control you would desire.

   There is a corresponding article Create a Popup calendar for ASP.NET forms that does somewhat the same thing (on dotnetjohn) that is done using jscript. (if i remember correctly)

  -DLARLICK

 
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now