Click here to Skip to main content
15,897,704 members
Articles / Web Development / ASP.NET

Applying a watermark to a textbox in a Web Application

Rate me:
Please Sign up or sign in to vote.
3.39/5 (16 votes)
29 Aug 2006Ms-PL1 min read 128.1K   1.5K   33   37
An article on using JavaScript in ASP.NET for applying a watermark to a textbox in a Web Application.

Introduction

You may have seen some web sites displaying watermarks in text boxes. As soon as you click on a text box, the watermark disappears and you can start entering text. In this article, I will explain a small JavaScript trick that you can use to do the same in your web application. I have included a library that you can use to achieve the same functionality without having to worry about writing JavaScript code.

Sample screenshot

Background

The idea is to capitalize on the OnFocus and OnBlur events of the HTML input tag. The library sets flips between two styles for the input tag and, also, it checks if the default text has been changed or not.

Using the code

To use the code, you need to provide at least two styles for the text box. One that represents the watermarked textbox and another that represents a normal textbox. The sample project includes such a stylesheet.

To apply watermark to a textbox with id=textbox1, write the following in your page_load event handler:

VB
TextBoxWatermark.WaterMarkHelper.ApplyWaterMarkToTextBox(textBox1, _
   "Watermarktext", "WaterMarkedTextBoxStyle", "NormalTextBoxStyle")

Enhancements

You can enhance the library to intercept the submit event and remove the default text before the required field validators fire.

Update history

  • August 30, 2006: Patched the single quote bug. The library used to break down when a single quote was supplied in WatermarkText.
  • August 23, 2006: Patched the library to work with Firefox.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer Microsoft India
India India
I am a developer (okay... that's very obvious!). I started programming at the age of 17 when I entered my engineering graduation back in the year 2001. I started off with C, C++ on UNIX. But soon I realized that if I need to make money out of my skills, I need to add some visual element to my applications (and that I need to switch to MS technologies!). Someone told me about VB. I started learning it but couldn’t get thru (I was religiously in love with curly braces; I never felt VB is a language). Then I began with VC++ 6.0 and I fell in love with it. Since then, I have been working on various MS technologies (C++, C#, VB.NET, SQL Server 2000/2005 etc.) I graduated in 2005 and currently, I am working with Microsoft in Hyderabad, India.
That’s all I can write as of now. If you want to know more, drop me a mail, I will try and respond Smile | :)

Comments and Discussions

 
GeneralC# Version Pin
Member 769493221-Mar-11 1:53
Member 769493221-Mar-11 1:53 
GeneralNEED IN C# Pin
suryansh4u21-Sep-10 10:38
suryansh4u21-Sep-10 10:38 
GeneralMy vote of 5 Pin
suryansh4u21-Sep-10 10:36
suryansh4u21-Sep-10 10:36 
Generalhi Pin
manishkumarsoni1-Jul-10 16:04
manishkumarsoni1-Jul-10 16:04 
GeneralPassword boxes Pin
Johnny J.9-Mar-09 23:53
professionalJohnny J.9-Mar-09 23:53 
QuestionMissing Library in Source Zip Pin
Murpheux Melony19-Nov-08 0:07
Murpheux Melony19-Nov-08 0:07 
AnswerRe: Missing Library in Source Zip Pin
Deobrat Singh19-Nov-08 0:17
Deobrat Singh19-Nov-08 0:17 
GeneralRe: Missing Library in Source Zip Pin
Murpheux Melony19-Nov-08 1:22
Murpheux Melony19-Nov-08 1:22 
GeneralRe: Missing Library in Source Zip Pin
Deobrat Singh19-Nov-08 1:24
Deobrat Singh19-Nov-08 1:24 
GeneralC# Version Pin
BronwenZande2-Jan-07 15:42
BronwenZande2-Jan-07 15:42 
GeneralASP.NET 2.0 ISSUE Pin
RogerMCT23-Oct-06 7:34
RogerMCT23-Oct-06 7:34 
AnswerRe: ASP.NET 2.0 ISSUE Pin
Deobrat Singh23-Oct-06 18:01
Deobrat Singh23-Oct-06 18:01 
QuestionHow to customize the messages while setup the application using vb.net? Pin
xxxxxxxxxchandra30-Aug-06 0:43
xxxxxxxxxchandra30-Aug-06 0:43 
QuestionWatermark library and single quotes Pin
pbishop29-Aug-06 9:10
pbishop29-Aug-06 9:10 
AnswerRe: Watermark library and single quotes [modified] Pin
Deobrat Singh29-Aug-06 9:29
Deobrat Singh29-Aug-06 9:29 
GeneralRe: Watermark library and single quotes Pin
pbishop29-Aug-06 9:49
pbishop29-Aug-06 9:49 
GeneralRe: Watermark library and single quotes [modified] Pin
Deobrat Singh29-Aug-06 10:06
Deobrat Singh29-Aug-06 10:06 
GeneralRe: Watermark library and single quotes [modified] Pin
Deobrat Singh29-Aug-06 19:47
Deobrat Singh29-Aug-06 19:47 
GeneralRe: Watermark library and single quotes Pin
pbishop30-Aug-06 3:30
pbishop30-Aug-06 3:30 
GeneralAtlas can do this too with control extenders Pin
Robert Ensor22-Aug-06 20:12
Robert Ensor22-Aug-06 20:12 
GeneralRe: Atlas can do this too with control extenders [modified] Pin
Deobrat Singh22-Aug-06 20:46
Deobrat Singh22-Aug-06 20:46 
QuestionValidators? Pin
rjpaulsen22-Aug-06 9:20
rjpaulsen22-Aug-06 9:20 
AnswerRe: Validators? Pin
Deobrat Singh22-Aug-06 18:29
Deobrat Singh22-Aug-06 18:29 
GeneralRe: Validators? Pin
itsmaryann8-Sep-08 0:04
itsmaryann8-Sep-08 0:04 
GeneralFireFox Problem Pin
asifrazach22-Aug-06 4:19
asifrazach22-Aug-06 4:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.