ASP.Net javascript "focus" method needs to be called twice??

T

TC

Hello,

I am using the script below to open a new window and once opened, redirect to that open window from the original window:

private void btnNewPDFWindow_Click(object sender, System.EventArgs e)

{

string NewPage = "NewPageZoom.aspx";

string ScriptBlockNewPage = "<script language='javascript'>var
NewPDFPage=window.open('" + NewPage + "','PDFPage');";

ScriptBlockNewPage = ScriptBlockNewPage + "NewPDFPage.focus();</script>";

Response.Write(ScriptBlockNewPage);

}
For some reason, with the code above, I have to click the button twice in order to get it to focus to the 2nd window again.

Any ideas?

Regards,

TC
 
J

John Timney \(ASP.NET MVP\)

At a guess, it might be running the focus code before the window has finished loading completely, on your second click the window is already loaded so the focu works.

Try putting a timer delay into the javascript to see if its that


--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

Hello,

I am using the script below to open a new window and once opened, redirect to that open window from the original window:

private void btnNewPDFWindow_Click(object sender, System.EventArgs e)

{

string NewPage = "NewPageZoom.aspx";

string ScriptBlockNewPage = "<script language='javascript'>var
NewPDFPage=window.open('" + NewPage + "','PDFPage');";

ScriptBlockNewPage = ScriptBlockNewPage + "NewPDFPage.focus();</script>";

Response.Write(ScriptBlockNewPage);

}
For some reason, with the code above, I have to click the button twice in order to get it to focus to the 2nd window again.

Any ideas?

Regards,

TC
 
G

Guest

TC,
I would create a Literal Control and write the script text to that control.
Protected WithEvents script As System.Web.UI.WebControls.Literal
script.text = '<script language=javascript...........

Arne.
 
L

Lowell Heddings

Why wouldn't you change this to a client-side event? You have the button
as a server-side button, which means the page already has to be reloaded
just to show another page.

Change the button to a client-side button, and add the javascript code
as a script block(RegisterClientScriptBlock ). Change your javascript
routine to run as a function. Call the function from the client-side button.

If you need to focus the window, you have much more control over it that
way. You can put in a brief timeout after opening the window, and then
call the focus method... something like this(syntax?)

function OpenPDFPage() {
NewPDFPage=window.open('NewPageZoom.aspx','PDFPage');
SetTimeout(5,'NewPDFPage.Focus()');
}

Much more reusable.

Lowell
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top