Setting focus....Please....no more JavaScript!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Setting focus....Please....no more JavaScript!

  1. #1
    KraKheD Guest

    Setting focus....Please....no more JavaScript!


    Is it possible to set focus to an item in a .Net Webpage OTHER than using
    Javascript?


    Many thanks....

  2. #2
    mark erickson Guest

    Re: Setting focus....Please....no more JavaScript!


    Hi,

    When you say "other than Javascript", do you mean without any client-side
    scripting at all? I don't know of a way to do it without client-side scripting--using
    server-side code only.

    However, if you mean you just want to avoid Javascript which might be disabled
    on a client's browser for security reasons, here is some vbscript:
    (Note: afaik, vbscript will not run on browsers other than internet explorer)

    <html>
    <head></head>
    <body>
    <form id=myform runat=server>
    <asp:textbox id=myTextbox1 text="Don't go here" runat=server/>
    <input type=text name=myTextbox2 value="Focus on ME!">
    </form>
    </body>

    <script language=vbscript>
    sub window_onload()
    myform.mytextbox2.focus
    end sub
    </script>

    </html>



    "KraKheD" <S@S.com> wrote:
    >
    >Is it possible to set focus to an item in a .Net Webpage OTHER than using
    >Javascript?
    >
    >
    >Many thanks....



  3. #3
    Michael Guest

    Re: Setting focus....Please....no more JavaScript!


    Mark,
    Your example is a good one for always putting focus on a single text box.
    I, too, am faced with an interesting puzzle here as well. If I enter data
    in that textbox, want to do server-side validation of that field, then position
    the cursor to the next field.....that cannot be easily done. The browser
    comes back with the cursor out of the form, requiring the user to click on
    the next field before being able to enter data. It seems that there should
    be a way to make the Webform handle a notice upon refresh to 'put focus on
    field n' where n is the field below the one they just entered data on.

    Michael


    "mark erickson" <m1erickson@cs.com> wrote:

    ><form id=myform runat=server>
    ><asp:textbox id=myTextbox1 text="Don't go here" runat=server/>
    ><input type=text name=myTextbox2 value="Focus on ME!">
    ></form>
    ></body>
    >
    ><script language=vbscript>
    >sub window_onload()
    >myform.mytextbox2.focus
    >end sub
    ></script>





  4. #4
    Russell Jones Guest

    Re: Setting focus....Please....no more JavaScript!

    I've posted this in a couple of places, because it answers more than one
    question.

    Michael, you can use this control--or similar techniques, to set the focus
    to any visible control that can accept the focus. And for Mark--this writes
    the JavaScript for you.

    Here's a generic "FocusManager" control that has one public String property,
    called BoundControlID. You build this control in a separate Web Control
    Library project (I keep a project specifically for Web Custom Controls).
    After building the project, you want to be able to drag/drop this control on
    a Web Form, and then set the BoundControlID to the id of any control that
    can receive the focus. To do that,
    1. Create a Web Control Library project. Delete the default WebControl class
    file.
    2. Add the FocusManager.vb class to the project
    3. Build the project.
    4. Right click in the Toolbox and select Customize Toolbox
    5. Click the .NET Framework Components tab.
    6. Click the Browse button and navigate to the DLL for the Web Control
    Library project you added in step 1. It will be in the bin directory. Select
    that DLL.
    7. You'll see a dialog containing a list of control names, positioned on the
    FocusManager item. Make sure it's checked, then click OK.

    Now, you can test the control. Drag/drop it on a Web Form. Click on the
    FocusManager control and set its BoundControlID property to the ID of a
    control on the form that you want to receive focus at startup.

    That's it.

    The FocusManager custom control uses the Page.RegisterStartupScript method
    to write a client-side script that will execute after the browser has
    rendered the form controls. Although you can set the BoundControlID property
    at design time, the real usefulness of this control is that you can set the
    property from the parent Web Form level, for example, to set the focus to a
    control with an ID of "Text1":

    FocusManager1.BoundControlID = "Text1"

    Using the control in this way from the parent page, you can set the focus to
    any control on your page.

    Here's the code for the FocusManager.vb class
    ***********
    Imports System.ComponentModel
    Imports System.Web.UI

    <DefaultProperty("Text"), ToolboxData("<{0}:FocusManager
    runat=server></{0}:FocusManager>")> Public Class FocusManager
    Inherits System.Web.UI.Control
    Dim mBoundControlID As String

    <Bindable(True), Category("Appearance"), DefaultValue("")> Property
    BoundControlID() As String
    Get
    Return mBoundControlID
    End Get

    Set(ByVal Value As String)
    mBoundControlID = Value
    End Set
    End Property

    Protected Overrides Sub Render(ByVal output As
    System.Web.UI.HtmlTextWriter)
    Dim s As String
    If Not mBoundControlID Is Nothing Then
    If Not Me.Page.FindControl(mBoundControlID) Is Nothing Then
    s = "<script type=""text/javascript"">"
    s += "document.getElementById(""" & mBoundControlID &
    """).focus();"
    s += "</script>"
    Me.Page.RegisterStartupScript("FocusManager", s)
    End If
    End If
    End Sub

    End Class
    ******************

    "Michael" <michael.eber@nationalcity.com> wrote in message
    news:3c04e734@147.208.176.211...
    >
    > Mark,
    > Your example is a good one for always putting focus on a single text box.
    > I, too, am faced with an interesting puzzle here as well. If I enter

    data
    > in that textbox, want to do server-side validation of that field, then

    position
    > the cursor to the next field.....that cannot be easily done. The browser
    > comes back with the cursor out of the form, requiring the user to click on
    > the next field before being able to enter data. It seems that there

    should
    > be a way to make the Webform handle a notice upon refresh to 'put focus on
    > field n' where n is the field below the one they just entered data on.
    >
    > Michael
    >
    >
    > "mark erickson" <m1erickson@cs.com> wrote:
    >
    > ><form id=myform runat=server>
    > ><asp:textbox id=myTextbox1 text="Don't go here" runat=server/>
    > ><input type=text name=myTextbox2 value="Focus on ME!">
    > ></form>
    > ></body>
    > >
    > ><script language=vbscript>
    > >sub window_onload()
    > >myform.mytextbox2.focus
    > >end sub
    > ></script>

    >
    >
    >




  5. #5
    Michael Guest

    Re: Setting focus....Please....no more JavaScript!


    Russel,

    Now THIS is cool. Thanks.


  6. #6
    Michael Guest

    Re: Setting focus....Please....no more JavaScript!


    Whoops, spoke too quickly!
    Russel: I got the Web Control built (using .NET RC3)
    I got it added to my form. (NOTE: my form has a mix of ASP Web controls
    and HTML controls....it was originally stuck out side of my <form></form>
    tags. I moved it between the form tags. I ran it and......NOTHING! There
    is no script created and focus is given to nothing in the form even though
    I have defined a default tag for focus. Also, where I programmatically set
    the field I still get nothing. There is no focus given to any field in the
    form. (I should note that I've also put a VBScript in the beginning of my
    form to handle the ExitButton_onClick event)

    Any ideas as to why it isn't working? I've implemented the code exactly:
    Imports System.ComponentModel
    Imports System.Web.UI

    <DefaultProperty("Text"), ToolboxData("<{0}:FocusManager runat=server></{0}:FocusManager>")>
    Public Class FocusManager
    ' Inherits System.Web.UI.WebControls.WebControl
    Inherits System.Web.UI.Control

    Dim mBoundControlID As String

    <Bindable(True), Category("Appearance"), DefaultValue("")> Property BoundControlID()
    As String
    Get
    Return mBoundControlID
    End Get

    Set(ByVal Value As String)
    mBoundControlID = Value
    End Set
    End Property

    Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
    Dim s As String
    If Not mBoundControlID Is Nothing Then
    If Not Me.Page.FindControl(mBoundControlID) Is Nothing Then
    s = "script tag removed <!--- script type=""text/javascript"">"
    s += "document.getElementById(""" & mBoundControlID & """).focus();"
    s += "</script> <!--- --->"
    Me.Page.RegisterStartupScript("FocusManager", s)
    End If
    End If
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center