Link to home
Start Free TrialLog in
Avatar of lachmann
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.
Avatar of lachmann
lachmann

ASKER

Wow. I expected to be flooded with solution with what appears to be a simple javascript question. Maybe EE isn't the place that it used to be.
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.
ASKER CERTIFIED SOLUTION
Avatar of Rahul Gade
Rahul Gade
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of leakim971
Use :    
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

Open in new window


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>

Open in new window

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

Open in new window