[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ASP.NET - select text in a textbox with javascript

Posted on 2011-10-31
4
Medium Priority
?
311 Views
Last Modified: 2012-05-12
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
Comment
Question by:lachmann
  • 2
4 Comments
 

Author Comment

by:lachmann
ID: 37058837
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
 

Author Comment

by:lachmann
ID: 37058867
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
 
LVL 7

Accepted Solution

by:
Rahul_Gade earned 2000 total points
ID: 37058895
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
 
LVL 83

Expert Comment

by:leakim971
ID: 37060241
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question