• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

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.
0
lachmann
Asked:
lachmann
  • 2
1 Solution
 
lachmannAuthor Commented:
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.
0
 
lachmannAuthor Commented:
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.
0
 
Rahul_GadeCommented:
I guess, page.registerStartupScript is something that can help you.
See http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript.aspx

-Rahul
0
 
leakim971PluritechnicianCommented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now