![]() |
|
|
|||||||
![]() |
ASP Net - ASP.Net javascript "focus" method needs to be called twice?? |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
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 |
|
|
|
|
#2 |
|
Posts: n/a
|
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 "TC" <> wrote in message news:... 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 |
|
|
|
#3 |
|
Posts: n/a
|
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. "TC" wrote: > 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 > > |
|
|
|
#4 |
|
Posts: n/a
|
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 TC wrote: > 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 > > |
|