0

i have the following Textbox in an asp.net page.

<asp:TextBox ID="TtxtMessage"  Text="Write Here" ForeColor="Control" runat="server" Height="300px" TextMode="MultiLine" Width="274px"></asp:TextBox>

The default text in the Text box will be write here. If the user clicks that i want to clear the text and this should happen only on the first click.For the subsequent clicks the content entered by the user should remain the same. How to do this using Javascript or Code Behind ?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Sooraj
  • 9,717
  • 9
  • 64
  • 99
  • Not sure what you mean by "only the first click" what happens on the third click? – crthompson Oct 21 '14 at 17:24
  • on the third or an subsequent click the text there should remain the same. ( whatever there was previously typed in by the user – Sooraj Oct 21 '14 at 17:25
  • It's pretty straightforward with jquery... look at http://viralpatel.net/blogs/default-text-label-textbox-javascript-jquery/ and you can easily do the same thing using instead of textboxes – weloytty Oct 21 '14 at 17:31
  • This concept is called a [placeholder (html5)](http://stackoverflow.com/a/18022440/2589202) or [watermark](http://www.aspsnippets.com/Articles/Watermark-TextBox-using-JavaScript.aspx). You are trying to do that manually, but there are already many things that can do that for you. – crthompson Oct 21 '14 at 17:32

2 Answers2

2

You can use the placeholder property like this

<asp:TextBox ID="TtxtMessage"  placeholder="Write Here" ForeColor="Control" runat="server" Height="300px" TextMode="MultiLine" Width="274px"></asp:TextBox>
QuakeCore
  • 1,886
  • 2
  • 15
  • 33
  • Placeholder is an HTML5 designation, not an attribute on an asp textbox. – crthompson Oct 21 '14 at 17:31
  • @QuakeCore, you can improve your answer by explaining that placeholder is limited to HTML5 and modern browsers who have implement it. – Dalorzo Oct 21 '14 at 17:35
2

A placeholder would be a good answer for this (see Add HTML5 placeholder text to a textbox .net).

However using jquery (note I'm not sure if asp.net still mangles the controls ID's in the rendered html) something like this may work:

<script>
$(document).ready(function() {

  var firstClickOccurred = false;

  $("#TtxtMessage").focus(function() {
    if(!firstClickOccurred) {
      $(this).val('');
      firstClickOccurred = true;
    }
  });

});
</script>
Community
  • 1
  • 1
User
  • 62,498
  • 72
  • 186
  • 247