Focus on the first control

Discussion in 'ASP .Net' started by Rafael A. M. Borges, Mar 10, 2007.

  1. Hi,

    How can I set the focus on the first control in the page? I tried
    Page.Controls(0).Focus() but it didn't works. Does anyone knows it?
    Rafael A. M. Borges, Mar 10, 2007
    #1
    1. Advertising

  2. Rafael A. M. Borges

    xzzy Guest

    this sets focus to the field named 'ctlMyField'

    ctlMyField.Page.RegisterClientScriptBlock("InitialFocus", "<SCRIPT
    FOR='window' EVENT='onload' LANGUAGE='JScript'>document.all." + ctlMyField +
    ".focus();</SCRIPT>");


    "Rafael A. M. Borges" <> wrote in message
    news:...
    > Hi,
    >
    > How can I set the focus on the first control in the page? I tried
    > Page.Controls(0).Focus() but it didn't works. Does anyone knows it?
    >
    xzzy, Mar 11, 2007
    #2
    1. Advertising

  3. On Mar 10, 11:02 pm, "xzzy" <> wrote:
    > this sets focus to the field named 'ctlMyField'
    >
    > ctlMyField.Page.RegisterClientScriptBlock("InitialFocus", "<SCRIPT
    > FOR='window' EVENT='onload' LANGUAGE='JScript'>document.all." + ctlMyField +
    > ".focus();</SCRIPT>");
    >
    > "Rafael A. M. Borges" <> wrote in messagenews:...
    >
    > > Hi,

    >
    > > How can I set the focus on the first control in the page? I tried
    > > Page.Controls(0).Focus() but it didn't works. Does anyone knows it?


    Ok, but I want to do this in a master page, to set the focus on the
    first control in the pages that inherits it. So I need to discover
    programatically the first control of the page.

    Thanks for your help
    Rafael A. M. Borges, Mar 11, 2007
    #3
  4. It could be tricky if you know nothing about the controls on content pages.
    You could try polling through the controls to find the one with minimum
    value of the TabIndex property and call SetFocus on it. But you can't
    guarantee the right order of TabIndex values. Polling the Controls
    collection should be recursive. And what will you do if the first control is
    just an html control and not a web control in the first place?

    I would suggest introducing a property on the master page where content page
    would set the id of the control that should get focus and the master page
    would set focus based on this id.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "Rafael A. M. Borges" <> wrote in message
    news:...
    > On Mar 10, 11:02 pm, "xzzy" <> wrote:
    >> this sets focus to the field named 'ctlMyField'
    >>
    >> ctlMyField.Page.RegisterClientScriptBlock("InitialFocus", "<SCRIPT
    >> FOR='window' EVENT='onload' LANGUAGE='JScript'>document.all." +
    >> ctlMyField +
    >> ".focus();</SCRIPT>");
    >>
    >> "Rafael A. M. Borges" <> wrote in
    >> messagenews:...
    >>
    >> > Hi,

    >>
    >> > How can I set the focus on the first control in the page? I tried
    >> > Page.Controls(0).Focus() but it didn't works. Does anyone knows it?

    >
    > Ok, but I want to do this in a master page, to set the focus on the
    > first control in the pages that inherits it. So I need to discover
    > programatically the first control of the page.
    >
    > Thanks for your help
    >
    Eliyahu Goldin, Mar 11, 2007
    #4
  5. Rafael A. M. Borges

    Bo Salomon Guest

    On Mar 11, 11:07 am, "Eliyahu Goldin"
    <> wrote:
    > It could be tricky if you know nothing about the controls on content pages.
    > You could try polling through the controls to find the one with minimum
    > value of the TabIndex property and call SetFocus on it. But you can't
    > guarantee the right order of TabIndex values. Polling the Controls
    > collection should be recursive. And what will you do if the first control is
    > just an html control and not a web control in the first place?
    >
    > I would suggest introducing a property on the master page where content page
    > would set the id of the control that should get focus and the master page
    > would set focus based on this id.
    >
    > --


    An alternative is to define an interface that content pages must
    implement to benefit from the focus treatment - e.g

    public interface FocusFirst
    {
    WebControl First {get; set;}
    }

    the master page then addresses the interface like this

    FocusFirst f = Page as FocusFirst;
    if (f != null)
    f.First.Focus();
    Bo Salomon, Mar 11, 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. Jason
    Replies:
    4
    Views:
    1,617
    Jason
    May 7, 2007
  2. Paul E Collins

    [Web] Focus on first visible control

    Paul E Collins, Jan 7, 2005, in forum: Javascript
    Replies:
    5
    Views:
    108
  3. Replies:
    2
    Views:
    336
    Thomas 'PointedEars' Lahn
    Jun 6, 2005
  4. Roger
    Replies:
    3
    Views:
    322
  5. Fred
    Replies:
    1
    Views:
    89
    Kiran Makam
    Aug 24, 2008
Loading...

Share This Page