TetxBox Question...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What shall I do to have a textbox that clears its default text when it is
selected?

Hector
 
Hi Hector,

For that, I'd inject a little client-side JavaScript into the page that
checks the current text when the control gets the focus. If it is the same
as the default text, set it to an empty string.

Here's the idea:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim strDefaultText = "The default text"
If Not IsPostBack Then
TextBox1.Text = strDefaultText
End If
TextBox1.Attributes.Add("onfocus", "doClear(this)")
Page.RegisterClientScriptBlock("clear", _
"<script language='javascript'>function doClear(cntrl)" & _
"{if (cntrl.value=='" & _
strDefaultText & "'){cntrl.value=''}}</script>")
End Sub

<form id="Form1" method="post" runat="server">
<asp:textbox id="TextBox1" runat="server"></asp:textbox>
</form>

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto
 
Back
Top