Setting Focus to a Control - Web

G

Glenn T. Kitchen

Hello All,

Does anyone know how to set the focus to a textbox control on Page_Load?

Thank you,
Glenn
 
P

Patrick Olurotimi Ige

Or use this:-
<html>
<head>
<script language="JavaScript">
function Focus()
{
formID.urname.focus();
}
</script>
</head>
<body onload="Focus();">
<form id="formID">
Name: <input type="text" id="urname" /><br />
Age: <input type="text" id="membership" />
</form>
</body>
</html>

Hope it helps
Patrick
 
G

Glenn T. Kitchen

Thanks Patrick,

I tried implementing in to the HTML of the webForm the code you specified,
but didn't do something right and caused me to have to re-program the whole
page lol. My fault, I don't know HTML very well and was not able to get the
code back to what it was originally.

I'll try it again after making a copy of the next WebForm I program in case
I mess it up again.

Thanks for you input,
glenn
 
P

Patrick Olurotimi Ige

Glenn try posting ur code and where you would like to implement it.
Patrick
 
T

Toby

Try this:-

Public Shared Sub SetFocus(ByVal control As Control)
If control.Page Is Nothing Then
Throw New ArgumentException("The Control must be added to a Page
before you can set the IntialFocus to it.")
End If
'If the control is enabled and visible select it
If control.Visible And control.EnableViewState Then
If control.Page.Request.Browser.JavaScript = True Then
' Create JavaScript
Dim s As StringBuilder = New StringBuilder
s.Append(vbCrLf + "<SCRIPT LANGUAGE='JavaScript'>" + vbCrLf)
s.Append("<!--" + vbCrLf)
s.Append("function SetInitialFocus()" + vbCrLf)
s.Append("{" + vbCrLf)
s.Append(" try " & vbCrLf)
s.Append(" {" & vbCrLf)
s.Append(" document.")
' Find the Form
Dim p As control = control.Parent
While Not (TypeOf p Is System.Web.UI.HtmlControls.HtmlForm)
p = p.Parent
End While
s.Append(p.ClientID)
s.Append("['")
s.Append(control.UniqueID)
' Set Focus on the selected item of a RadioButtonList
If TypeOf control Is RadioButtonList Then
Dim rbl As RadioButtonList = control
If Not rbl Is Nothing Then
Dim suffix As String = "_0"
Dim t As Int32 = 0
Dim li As ListItem
For Each li In rbl.Items
If li.Selected Then
suffix = "_" + t.ToString()
Exit For
End If
t = t + 1
Next
s.Append(suffix)
End If
End If
' Set Focus on the first item of a CheckBoxList
If TypeOf control Is CheckBoxList Then
s.Append("_0")
End If
s.Append("'].focus();" + vbCrLf)
s.Append(" }catch(e){}" & vbCrLf) ' close the try, catch
block
s.Append("}" + vbCrLf)
If control.Page.SmartNavigation Then
s.Append("window.setTimeout(SetInitialFocus, 500);" +
vbCrLf)
Else
s.Append("window.onload = SetInitialFocus;" + vbCrLf)
End If
s.Append("// -->" + vbCrLf)
s.Append("</SCRIPT>" + vbCrLf)
' Register Client Script
control.Page.RegisterClientScriptBlock("InitialFocus",
s.ToString())
End If
End If
End Sub

Regards

Toby.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top