Can anyone tell me if you see anything wrong with this code? I'm trying to set focus on txt_job textbox when a button is clicked.
Private Sub SetFocusScript(ByVal strTextBox As String)
Dim sb As New System.Text.StringBuilder()
sb.Append("<script language='javascript'>")
sb.Append("document.frmView." & strTextBox & ".focus();")
sb.Append("</script>")
' register the script
If Not ClientScript.IsStartupScriptRegistered("myBoxSelect") Then
ClientScript.RegisterStartupScript(Me.GetType, "myBoxSelect", sb.ToString())
End If
End Sub
Thanks in advance,
Ravina Brar
IT Project Manager
ECO Canada
--------------------------
The man who smiles when things go wrong has thought of someone to blame it on.
- Robert Bloch
Private Sub SetFocusScript(ByVal ClientID As String)
If Not ClientScript.IsStartupScriptRegistered("SetFocus") Then
Dim s As String = "<script language=|javascript| type=|text/javascript|>" + "document.form1." + ClientID + ".focus();" + "</script>"
ClientScript.RegisterStartupScript(Me.[GetType](), "SetFocus", s.Replace("|"C, """"C))
End If
End Sub
' samples
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
SetFocusScript(TextBox1.ClientID)
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)
SetFocusScript(ListBox1.ClientID)
End Sub
Ravina Brar
IT Project Manager
ECO Canada
--------------------------
The man who smiles when things go wrong has thought of someone to blame it on.
- Robert Bloch
Ravina
Member
115 Points
125 Posts
Setting focus on control in the code-behind VB.NET
May 07, 2008 03:38 PM|LINK
Hello,
Can anyone tell me if you see anything wrong with this code? I'm trying to set focus on txt_job textbox when a button is clicked.
Private Sub SetFocusScript(ByVal strTextBox As String) Dim sb As New System.Text.StringBuilder() sb.Append("<script language='javascript'>") sb.Append("document.frmView." & strTextBox & ".focus();") sb.Append("</script>") ' register the script If Not ClientScript.IsStartupScriptRegistered("myBoxSelect") Then ClientScript.RegisterStartupScript(Me.GetType, "myBoxSelect", sb.ToString()) End If End SubIT Project Manager
ECO Canada
--------------------------
The man who smiles when things go wrong has thought of someone to blame it on.
- Robert Bloch
scott@elband...
Star
11346 Points
1865 Posts
Re: Setting focus on control in the code-behind VB.NET
May 07, 2008 03:51 PM|LINK
Are you passing the TextBox clientID to the method of just the ID? You will need to pass this to get the javascript to work:
TextBox1.ClientID
HarveyTriana
Member
57 Points
20 Posts
Re: Setting focus on control in the code-behind VB.NET
May 07, 2008 04:36 PM|LINK
Look this approach:
Private Sub SetFocusScript(ByVal ClientID As String)
If Not ClientScript.IsStartupScriptRegistered("SetFocus") Then
Dim s As String = "<script language=|javascript| type=|text/javascript|>" + "document.form1." + ClientID + ".focus();" + "</script>"
ClientScript.RegisterStartupScript(Me.[GetType](), "SetFocus", s.Replace("|"C, """"C))
End If
End Sub
' samples
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
SetFocusScript(TextBox1.ClientID)
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)
SetFocusScript(ListBox1.ClientID)
End Sub
Notes. Original source:
private void SetFocusScript(string ClientID)
{// by Harvey Triana
if (!ClientScript.IsStartupScriptRegistered("SetFocus"))
{
string s = "<script language=|javascript| type=|text/javascript|>"
+ "document.form1." + ClientID + ".focus();"
+ "</script>";
ClientScript.RegisterStartupScript(this.GetType(), "SetFocus", s.Replace('|', '"'));
}
}
// samples
protected void Button1_Click(object sender, EventArgs e)
{
SetFocusScript(TextBox1.ClientID);
}
protected void Button2_Click(object sender, EventArgs e)
{
SetFocusScript(ListBox1.ClientID);
}
convert tool:
http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx
http://vexpert.mvps.org
gt1329a
All-Star
15377 Points
2501 Posts
ASPInsiders
MVP
Re: Setting focus on control in the code-behind VB.NET
May 07, 2008 06:57 PM|LINK
A guide to combining jQuery and ASP.NET: jQuery for the ASP.NET developer
HarveyTriana
Member
57 Points
20 Posts
Re: Setting focus on control in the code-behind VB.NET
May 07, 2008 07:24 PM|LINK
Yes. It is a better option.
http://vexpert.mvps.org
Ravina
Member
115 Points
125 Posts
Re: Setting focus on control in the code-behind VB.NET
May 07, 2008 08:27 PM|LINK
Thank You.
That is a easier way.
Appreciate all the responses.
IT Project Manager
ECO Canada
--------------------------
The man who smiles when things go wrong has thought of someone to blame it on.
- Robert Bloch