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

Discussion in 'ASP .Net' started by Jason, May 4, 2007.

  1. Jason

    Jason Guest

    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
    Jason, May 4, 2007
    #1
    1. Advertising

  2. Jason

    Harry Haller Guest

    On Fri, 4 May 2007 15:46:45 -0700, "Jason" <>
    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?).
    Harry Haller, May 6, 2007
    #2
    1. Advertising

  3. On May 5, 12:46 am, "Jason" <> 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...
    Alexey Smirnov, May 6, 2007
    #3
  4. 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 (, 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.
    Walter Wang [MSFT], May 7, 2007
    #4
  5. Jason

    Jason Guest

    OK. Thanks to all for your responses

    /jason


    "Walter Wang [MSFT]" <> wrote in message
    news:...
    > 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 (, 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, May 7, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bender
    Replies:
    2
    Views:
    255
    Thomas 'PointedEars' Lahn
    Oct 22, 2003
  2. Starry Gordon
    Replies:
    1
    Views:
    111
  3. David Otton

    window.onload and body.onload differences

    David Otton, Nov 4, 2004, in forum: Javascript
    Replies:
    2
    Views:
    543
    Martin Honnen
    Nov 4, 2004
  4. Replies:
    18
    Views:
    539
  5. Roger
    Replies:
    3
    Views:
    320
Loading...

Share This Page