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

  • Thread starter Thread starter TC
  • Start date Start date
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
 
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
 
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.
 
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
 
Back
Top