Select text in asp:textbox

T

TJHerman

I am using Visual Studio 2005 and writing all code-behind in VB. I'm setting
the focus of a textbox after I run some calculations. I want to additionally
select all the text in the textbox that I've set the focus to so that the
user doesn't have to manually select the text and type. Can someone give me
an example of how to do this programmatically using vb?
 
K

Ken Cox [Microsoft MVP]

Hi TJ,

Here's a little code that inserts some JavaScript when the page loads to
select the text in a textbox.

If you can use Atlas, you'll find a really nice technique for creating a
Textbox watermark:

http://atlas.asp.net/atlastoolkit/TextBoxWatermark/TextBoxWatermark.aspx

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
Page.SetFocus("TextBox1")
ClientScript.RegisterStartupScript _
(Me.GetType, "sel", _
"document.all.TextBox1.select();", True)
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Select and Set Focus</title>
<script language="javascript" type="text/javascript">

</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:textbox id="TextBox1" runat="server">Start typing
here</asp:textbox><br />
<br />
<asp:button id="Button1" runat="server" text="Button" />&nbsp;</div>
</form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,145
Latest member
web3PRAgeency
Top