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

J

Jason

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
 
H

Harry Haller

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?).
 
G

Guest

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...
 
W

Walter Wang [MSFT]

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 ([email protected], 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top