Link to home
Start Free TrialLog in
Avatar of anwarmir
anwarmir

asked on

Set focus on text box

Hi I am trying to set focus on a text box when opening a form. However the focus seems to focus on a textbox in a tabpage. Any ideas how i can resolve this. The focus seems to work when a validation check is fired within the form.?
Avatar of YZlat
YZlat
Flag of United States of America image

try setting SmartNavigation to "true" inside the page directive for that page
Avatar of anwarmir
anwarmir

ASKER

Is  this a proprty of the form ? As I cant seem to find it in design view
no, at the top of your page in HTML view you'll see
<%@ Page Language="vb".... %>

at the very top of the page.
To that directive add a line Smartnavigation="true"

see if it'll help
Sorry Iam using VB.net not ASP.net

In page load

Page.RegisterStartupScript("focus", "<script>document.getElementById('textbox1').focus();</script>")
ASKER CERTIFIED SOLUTION
Avatar of caball88
caball88

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
winForms...oops

:)
well...i had the same problem and above line of code works....cool...

then after some time...i found a complex but a better solution to the problem....

add a class to your application....add the following lines of code to that....


Imports System.Text

Public Class Pooldimps

    Public Shared Sub SetFocus(ByVal Ctrl As Control)

        Dim sb As New StringBuilder

        sb.Append(Chr(13) & Chr(10))
        sb.Append("<script language='JavaScript'>" & Chr(13) & Chr(10))
        sb.Append("<!--" & Chr(13) & Chr(10))
        sb.Append("function SetFocus()" & Chr(13) & Chr(10))
        sb.Append("{" & Chr(13) & Chr(10))
        sb.Append("document.")

        Dim p As Control = Ctrl.Parent

        While Not TypeOf p Is System.Web.UI.HtmlControls.HtmlForm
            p = p.Parent
        End While

        sb.Append(p.ClientID)
        sb.Append("['")
        sb.Append(Ctrl.UniqueID.ToString)
        sb.Append("'].focus();" & Chr(13) & Chr(10))
        sb.Append("}" & Chr(13) & Chr(10))
        sb.Append("window.onload = SetFocus;" & Chr(13) & Chr(10))
        sb.Append("// -->" & Chr(13) & Chr(10))
        sb.Append("</script>")

        Ctrl.Page.RegisterClientScriptBlock("SetFocus", sb.ToString())

    End Sub


Then you can call this SetFocus method from anywhere....


Private Sub Page_Load

Pooldimps.SetFocus(YourControlName)

End Sub

Cheers....