469,632 Members | 1,681 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,632 developers. It's quick & easy.

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
May 4 '07 #1
4 3785
On Fri, 4 May 2007 15:46:45 -0700, "Jason" <Ja********@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?).
May 6 '07 #2
On May 5, 12:46 am, "Jason" <JasonJa...@nospam.nospamwrote:
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...

May 6 '07 #3
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 (wa****@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.

May 7 '07 #4
OK. Thanks to all for your responses

/jason
"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:ZE**************@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 (wa****@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.

May 7 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Eric Osman | last post: by
5 posts views Thread by Mark | last post: by
2 posts views Thread by jackson2005 | last post: by
8 posts views Thread by Jeff User | last post: by
5 posts views Thread by Finn Stampe Mikkelsen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.