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.?
try setting SmartNavigation to "true" inside the page directive for that page
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
<%@ Page Language="vb".... %>
at the very top of the page.
To that directive add a line Smartnavigation="true"
see if it'll help
ASKER
Sorry Iam using VB.net not ASP.net
In page load
Page.RegisterStartupScript ("focus", "<script>document.getEleme ntById('te xtbox1').f ocus();</s cript>")
Page.RegisterStartupScript
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.To String)
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.RegisterClientSc riptBlock( "SetFocus" , sb.ToString())
End Sub
Then you can call this SetFocus method from anywhere....
Private Sub Page_Load
Pooldimps.SetFocus(YourCon trolName)
End Sub
Cheers....
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
p = p.Parent
End While
sb.Append(p.ClientID)
sb.Append("['")
sb.Append(Ctrl.UniqueID.To
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.RegisterClientSc
End Sub
Then you can call this SetFocus method from anywhere....
Private Sub Page_Load
Pooldimps.SetFocus(YourCon
End Sub
Cheers....