Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   emulating window.focus in Body onload() event and setting focus to a control on same page (http://www.velocityreviews.com/forums/t504233-emulating-window-focus-in-body-onload-event-and-setting-focus-to-a-control-on-same-page.html)

Jason 05-04-2007 10:46 PM

emulating window.focus in Body onload() event and setting focus to a control on same page
 
Hi,

Here's the scenario:

I have a web application that has window A and window B.

A user has both window A and B open - window A is in the foreground and
window B is behind it.

If the user looking at window A clicks the "open window B" link, I want
window B to come to the front.

My approach was to set the window B Body tag's onload event like so:

<body onload="window.focus()">

I also want to place the default focus in the first text box in window B. I
was doing this like so:

protected void Page_Load(object sender, EventArgs e)

{

this.txtUserName.Focus();

}



The problem is, I can't seem to successfully use both of these at the same
time.

this.txtUserName.Focus() only works when I don't specify the onload() event
for the body tag.

I should note that window.focus() in the body tag's onload event fires
successfully whether this.txtUserName.Focus() is specified or not.


Not sure if it's pertinent, but Window B is a content page that has a master
page that is in turn a content page for another master page (aka, a nested
master page).

I have tried using
Page.Form.DefaultFocus = txtUserName.ClientID;

as well, but to no avail.

Any ideas are greatly appreciated!

Jason



Harry Haller 05-06-2007 06:26 AM

Re: emulating window.focus in Body onload() event and setting focus to a control on same page
 
On Fri, 4 May 2007 15:46:45 -0700, "Jason" <JasonJason@nospam.nospam>
wrote:

>Hi,
>
>Here's the scenario:
>
>I have a web application that has window A and window B.
>
>A user has both window A and B open - window A is in the foreground and
>window B is behind it.
>
>If the user looking at window A clicks the "open window B" link, I want
>window B to come to the front.
>
>My approach was to set the window B Body tag's onload event like so:
>
><body onload="window.focus()">
>
>I also want to place the default focus in the first text box in window B. I
>was doing this like so:
>
>protected void Page_Load(object sender, EventArgs e)
>
>{
>
>this.txtUserName.Focus();
>
>}
>
>
>
>The problem is, I can't seem to successfully use both of these at the same
>time.
>
>this.txtUserName.Focus() only works when I don't specify the onload() event
>for the body tag.
>
>I should note that window.focus() in the body tag's onload event fires
>successfully whether this.txtUserName.Focus() is specified or not.
>
>
>Not sure if it's pertinent, but Window B is a content page that has a master
>page that is in turn a content page for another master page (aka, a nested
>master page).
>
>I have tried using
>Page.Form.DefaultFocus = txtUserName.ClientID;
>
>as well, but to no avail.
>
>Any ideas are greatly appreciated!
>
>Jason


The solution is to manipulate the windows with javascript. Precisely
how depends upon what you called them, where you put them, etc.
Suffice to say you will be doing it client-side not server side. Since
the two windows are already open and coded I can't see how server-side
code enters the equation.

As such, you may get a better response from a javascript of DHTML
forum.

PS: Why are you using two windows? When browsing the web I hate sites
that open windows and popups left right and centre at me. I don't
think I'm unique in that way. Did your user specifically ask you to
use multiple windows? I thought asp.net had loads of features to get
away from that kind of stuff. I advise you to stick with one window
using tabs, divs or the master pages features (panels?).

Alexey Smirnov 05-06-2007 07:56 PM

Re: emulating window.focus in Body onload() event and setting focus to a control on same page
 
On May 5, 12:46 am, "Jason" <JasonJa...@nospam.nospam> wrote:
> A user has both window A and B open - window A is in the foreground and
> window B is behind it.


Does it work in IE7 and Firefox? (using tabbed browsing)


> The problem is, I can't seem to successfully use both of these at the same
> time.


I think the problem is here that you used code-behind and inline
scripting together.

Either try to use a code-behind only

Dim script As New System.Text.StringBuilder
With script
.Append("<Script Language='JavaScript'>")
.Append("function Page_Load() {")
.Append("document.focus();")
.Append("document.getElementById('" & txtUserName.ClientID &
"').focus();")
.Append("'}")
.Append("</Script>")
End With
RegisterStartupScript("startup", script.ToString)

or add this script in the inline code of your ASPX-page

<Script Language='JavaScript'>
function Page_Load() {
document.focus();
document.getElementById('myfield').focus();
}
</Script>
.....

<body onload="Page_Load()">

or you can also try to use the TabIndex property...


Walter Wang [MSFT] 05-07-2007 02:53 AM

RE: emulating window.focus in Body onload() event and setting focus to a control on same page
 
Hi Jason,

As community members suggested above, I'm afraid there's no reliable way to
activate window on client-side; this will largely depend on the client
browser, whether or not it's multi-tab based or even if there's any popup
blocker may interfere with your client-side javascript which trying to
manipulate the windows.

For the control focus question, in ASP.NET 2.0, when you use
Control.Focus(), it will automatically generate some javascript at the end
of the form, for example:

...

<script type="text/javascript">
<!--
WebForm_AutoFocus('TextBox2');// -->
</script>
</form>
</body>
</html>


This code will execute before your body.onload function executes, which
means your body.onload function will make the focus to your window instead
of the textbox2.

Hope this helps.


Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


Jason 05-07-2007 05:54 PM

Re: emulating window.focus in Body onload() event and setting focus to a control on same page
 
OK. Thanks to all for your responses

/jason


"Walter Wang [MSFT]" <wawang@online.microsoft.com> wrote in message
news:ZENu8LFkHHA.3352@TK2MSFTNGHUB02.phx.gbl...
> Hi Jason,
>
> As community members suggested above, I'm afraid there's no reliable way
> to
> activate window on client-side; this will largely depend on the client
> browser, whether or not it's multi-tab based or even if there's any popup
> blocker may interfere with your client-side javascript which trying to
> manipulate the windows.
>
> For the control focus question, in ASP.NET 2.0, when you use
> Control.Focus(), it will automatically generate some javascript at the end
> of the form, for example:
>
> ..
>
> <script type="text/javascript">
> <!--
> WebForm_AutoFocus('TextBox2');// -->
> </script>
> </form>
> </body>
> </html>
>
>
> This code will execute before your body.onload function executes, which
> means your body.onload function will make the focus to your window instead
> of the textbox2.
>
> Hope this helps.
>
>
> Regards,
> Walter Wang (wawang@online.microsoft.com, remove 'online.')
> Microsoft Online Community Support
>
> ==================================================
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> ==================================================
>
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>





All times are GMT. The time now is 01:29 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.