Set focus on a TextBox

  • Thread starter Thread starter mg
  • Start date Start date
Here's a little script I got, and modified, from Charles Carroll, i think,
(www.learnasp.com).

Public Shared Sub SetFocus(ByVal ControlClientID As String, ByVal Page
As System.Web.UI.Page)
Dim scriptFunction As New System.Text.StringBuilder
Dim scriptClientId As String
scriptFunction.Append("<script language='javascript'>")
scriptFunction.Append("document.getElementById('")
scriptFunction.Append(ControlClientID)
scriptFunction.Append("').focus();")
scriptFunction.Append("</script>")
Page.RegisterStartupScript("focus", scriptFunction.ToString())
End Sub

ControlClientID is the ClientID property of your control (likely a textbox).

Karl
 
VB
Sub SetFocus(ByVal controlToFocus As Control)

Dim scriptFunction As New StringBuilder
Dim scriptClientId As String

scriptClientId = controlToFocus.ClientID

scriptFunction.Append("<script language='javascript'>")
scriptFunction.Append("document.getElementById('")
scriptFunction.Append(scriptClientId)
scriptFunction.Append("').focus();")
scriptFunction.Append("</script>")

RegisterStartupScript("focus", scriptFunction.ToString())

End Sub

C# (something like this)
Fucntion SetFocus(Control controlToFocus )
{
String scriptFunction;
String scriptClientId;

scriptClientId = controlToFocus.ClientID;

scriptFunction += ("<script language='javascript'>");
scriptFunction. += ("document.getElementById('");
scriptFunction. += (scriptClientId);
scriptFunction. += ("').focus();");
scriptFunction.Append("</script>");

RegisterStartupScript("focus", scriptFunction.ToString());

}
 
Back
Top