473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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 4022
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Eric Osman | last post by:
Hi, When I run the following html file on netscape, and the button is clicked, the new window pops BEHIND all my other windows. How can I easily fix this html file so the window comes up in...
8
by: alanstew | last post by:
With the body tag calling out 'window onload', a function with a 'window.open' fails at the 'window.open' line. If I cut out the body tag, the function executes as normal. At first I thought it...
6
by: Brian | last post by:
Hi everyone, I'm writing a function (in javascript) that needs to do one thing if the page has not loaded, and another (different) thing if the page has already loaded. I'm looking for a way...
14
by: Mark | last post by:
Hi, At the top of my php file I have got :- <body onblur="self.focus();"> so when I click off onto another window, my window comes back up in front which is perfect. The problem however, is...
5
by: Mark | last post by:
Hi all, how do I open a javascript window from a code behind page? I was using Response.Write("<script language='javascript'>window.open('url','name','options');</script>") but now for some...
2
by: jackson2005 | last post by:
OK, I need to do three different things. On the ONLOAD event I would like a popup box to open. In this popup box I need two text boxes. One for the UserName and one for the BillingTo name. ...
8
by: Jeff User | last post by:
Hello all I am working on creating a Web application I am using C# code behind ..NET1.1 Rather than navigate to a new window, I want to open a seperate IE window when the user clicks a...
5
by: Finn Stampe Mikkelsen | last post by:
Hi How can i set a focus to a textbox in my codebehind page?? I have this WebForm, that takes information from a user and 2 buttons on the form. One that takes action on the entered...
9
by: tshad | last post by:
This was posted before but the message got messed up (all NLs were stripped out for some reason). I have 2 labels that hold the name of different images on my .aspx page. <asp:Label ID="Logo"...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.