TextBox Question

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
Thanks. That worked out like a charm.

Hector

Ken Cox said:
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

hecsan07 said:
What shall I do to clear the default text of a textbox when this is
selected?

Hector
 

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

Back
Top