lachmann
asked on
ASP.NET - select text in a textbox with javascript
I am validating some user entered text on the server side in my VB code. If the validation fails I would like to select the text in the textbox. How do I select the text with Javascripts. Examples would be most helpful.
ASKER
Perhaps more info on My side would be helpful
Here is the JavaScript Function which is in a .js file that is pulled into the masterpage
function SelectAll(id)
{
document.getElementById(id ).focus();
document.getElementById(id ).select() ;
}
Here is some of the vb.net code in the page code behind.
Private Sub ValidateField()
If Me.tbfield1.Text.Length < 10 Then
'call the javascript function
End If
End Sub
I am looking for what I need to do to call the javascript function.
Thanks,
Phil.
Here is the JavaScript Function which is in a .js file that is pulled into the masterpage
function SelectAll(id)
{
document.getElementById(id
document.getElementById(id
}
Here is some of the vb.net code in the page code behind.
Private Sub ValidateField()
If Me.tbfield1.Text.Length < 10 Then
'call the javascript function
End If
End Sub
I am looking for what I need to do to call the javascript function.
Thanks,
Phil.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Use :
test page :
Protected Sub ValidateField(sender As Object, e As System.EventArgs) Handles Button1.Click
If Me.tbfield1.Text.Length < 10 Then
Dim javascript As String = "SelectAll('" & tbfield1.ClientID & "')"
ClientScript.RegisterStartupScript(Me.GetType, "Startup", javascript, True)
End If
End Sub
test page :
<%@ Page Title="Page d'accueil" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script language="javascript" type="text/javascript">
function SelectAll(id) {
document.getElementById(id).focus();
document.getElementById(id).select();
}
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:TextBox runat="server" ID="tbfield1" ></asp:TextBox>
<br />
<asp:Button runat="server" ID="Button1" Text="Submit" />
</asp:Content>
code behind :Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub ValidateField(sender As Object, e As System.EventArgs) Handles Button1.Click
If Me.tbfield1.Text.Length < 10 Then
Dim javascript As String = "SelectAll('" & tbfield1.ClientID & "')"
ClientScript.RegisterStartupScript(Me.GetType, "Startup", javascript, True)
End If
End Sub
End Class
ASKER