...

View Full Version : How to send a single variable to Popup window



bnath001
03-05-2012, 09:09 PM
Hello folks,

I have a very simple problem...but stuggling with this.

I have a popup window (.aspx page). It will called from a parent .aspx page. All I want to do is read the name of the parent aspx page in popup page. Based on the name of the parent window, in the page_load event of the popup window, I want to execute some code.

But the only catch is that, I am not able to read the parent window name until the page_load event (of popup window) complets the execution.

1. calling popup window from parent window.

Code:

function OpenWindow_HistoricalRuns() {

var windowIncsimHistRuns = window.open('MVal.aspx', 'Market', 'width=350,height=425,scrollbars=0');

windowIncsimHistRuns.moveTo(500, 300);

}
In the body of the popup window the following function tries to read the parent window name:

function SetDisplay() {

document.getElementById('<%=TextBoxSourcePageName.ClientID%>').value = window.parent.name;

}
In the page_load event of the popup window, I want to see what is value of TextBoxSourcePageName control. I hoped I will get 'MarketVal'. But I never get anything.


protected void Page_Load(object sender, EventArgs e)
{

if (!this.IsPostBack)
{
try
{
if (TextBoxSourcePageName.Text == "MarketVal")
}
}
}


In my situation, I need to know the window.parent.name by the time Page_load event gets triggered on the popup window. Because I have to do some specific thing in the Page_Load event (of popup window), based on window.parent.name.

So my fundamental question is, which one will get fired first in an .aspx page.

1. Javascript in the body tag
or 2. Page_load event of the .aspx page.

If Javascript function in the body tag gets fired first, then by the time I get to Page_Load event, I should already have whatever I need.

thanks

Old Pedant
03-05-2012, 09:54 PM
> In my situation, I need to know the window.parent.name by the time Page_load event gets triggered on the popup window.

You can't, using JavaScript.

You apparently don't understand the loading cycle of an ASP.NET Page. Or any page served up by a web server.

JavaScript DOES NOT AND CAN NOT execute *UNTIL* the page is sent from the server to the browser.

And with ASP.NET, Page_Load occurs *BEFORE* even the <html> tag is sent to the browser! (It has to be! Because in Page_Load you could opt to do a Response.Redirect, and you can't redirect once *ANYTHING* has been sent to the browser...even the headers that are sent before the <html> tag.)

There is no possible way for you to have JavaScript send some value back to the ASP.NET server before the ASP.NET Page_load event occurs. (I don't think there is any way to do this before the entire end of the ASP.NET page creation cycle. I can't think of one, anyway.)

There is, however, a trivial way to do this:


var windowIncsimHistRuns = window.open('MVal.aspx?opener=' + window.name, 'Market', ... );

And then simply have your ASP.NET code look for Request.QueryString("opener")

bnath001
03-05-2012, 11:47 PM
Thanks Mr.Old Pedant.

I was focusing on Javascript and forgot to think about asp.net page cycle. Let me go thru asp.net page cycle chapters in the books I have.

REgards & thanks again
nath

bnath001
03-05-2012, 11:58 PM
Old Pedant,

This is what I did.


function OpenWindow_HistoricalRuns() {




var windowIncsimHistRuns = window.open('MVal.aspx?opener=' +window.name, 'MarketVal', 'menubar=1,width=350,height=425,scrollbars=0');


windowIncsimHistRuns.moveTo(500, 300);

}


In the Page_Load event of the popup window, I did


string v = Request.QueryString["opener"];

I am getting always "" (empty string) in the variable v.

Anything wrong I am doing in the syntax?
thanks

Old Pedant
03-06-2012, 03:28 AM
Are you sure that the window you are coming from *HAS* a name?

Windows don't have names unless you explicitly set them.

Find out by doing this:


function OpenWindow_HistoricalRuns() {
alert("The name of this window::" + window.name + "::");
window.open('MVal.aspx?opener=' +window.name,
'MarketVal',
'menubar=1,top=300,left=500,width=350,height=425,scrollbars=0');
}

Notice how I avoided the need for the call to moveTo.

bnath001
03-06-2012, 09:48 PM
I actually gave a name to the window that calls the popup window.

by saying window.name = "MVal'; I am also able to read that name of the window in Page_Load event of the window that is opened.

But now, the issue is the popup is no longer a popup window. It is a full page loaded like a content form. It loads as a new page (not a small popup anymore). So what I learnt is if you want a .aspx page to load as a popup you can't pass paramters using '?' from the main page. Is that true.

Thanks

Old Pedant
03-06-2012, 09:58 PM
Shouldn't be. ASP.NET doesn't control the opening or sizing of the windows. That is 100% a browser-side capability and decision.

I think you need to look elsewhere for the problem.

bnath001
03-06-2012, 10:48 PM
Thanks for your help so far.

I will try to get help somewhere else.

Regards

rnd me
03-07-2012, 05:20 PM
you can do anything, don't give up.

simply put a sentinel on your dependancy-having function:




function SetDisplay() {
if(!window.parent || !window.parent.name){return setTimeout(SetDisplay ,100);}
document.getElementById('<%=TextBoxSourcePageName.ClientID%>').value = window.parent.name;

}


(changes in red)

Old Pedant
03-07-2012, 09:49 PM
When I said to look elsewhere, I meant that the error is probably in the JavaScript code. You didn't show us the latest version of that code.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum