Click here to Skip to main content
6,595,444 members and growing! (19,798 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate License: The Microsoft Public License (Ms-PL)

Applying a watermark to a Textbox in a Web Application

By Deobrat Singh

An article on using JavaScript in ASP.NET
VB, Javascript, Windows, .NET, ASP.NET, VS2005, Dev
Posted:16 Aug 2006
Updated:29 Aug 2006
Views:47,740
Bookmarked:27 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
15 votes for this article.
Popularity: 3.93 Rating: 3.34 out of 5
1 vote, 6.7%
1
3 votes, 20.0%
2
2 votes, 13.3%
3
4 votes, 26.7%
4
5 votes, 33.3%
5

Update History

Wednesday August 30, 2006: Patched the Single Quote bug. The library used to brake down when a single quote was supplied in WatermarkText

Wednesday August 23, 2006: Patched the library to work with FireFox. 

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 JavaScript. 

Sample screenshot

Background

The idea is to capitalize on 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. First 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. 

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

Enhancements

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

License

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

About the Author

Deobrat Singh


Member
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
Occupation: Software Developer
Company: Microsoft India
Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 32 (Total in Forum: 32) (Refresh)FirstPrevNext
GeneralPassword boxes PinmemberJohnny J.0:53 10 Mar '09  
QuestionMissing Library in Source Zip PinmemberDapo Onawole1:07 19 Nov '08  
AnswerRe: Missing Library in Source Zip PinmemberDeobrat Singh1:17 19 Nov '08  
GeneralRe: Missing Library in Source Zip PinmemberDapo Onawole2:22 19 Nov '08  
GeneralRe: Missing Library in Source Zip PinmemberDeobrat Singh2:24 19 Nov '08  
GeneralC# Version PinmemberBronwenZande16:42 2 Jan '07  
GeneralASP.NET 2.0 ISSUE PinmemberRogerMCT8:34 23 Oct '06  
AnswerRe: ASP.NET 2.0 ISSUE PinmemberDeobrat Singh19:01 23 Oct '06  
GeneralHow to customize the messages while setup the application using vb.net? Pinmemberxxxxxxxxxchandra1:43 30 Aug '06  
QuestionWatermark library and single quotes Pinmemberpbishop10:10 29 Aug '06  
AnswerRe: Watermark library and single quotes [modified] PinmemberDeobrat Singh10:29 29 Aug '06  
GeneralRe: Watermark library and single quotes Pinmemberpbishop10:49 29 Aug '06  
GeneralRe: Watermark library and single quotes [modified] PinmemberDeobrat Singh11:06 29 Aug '06  
GeneralRe: Watermark library and single quotes [modified] PinmemberDeobrat Singh20:47 29 Aug '06  
GeneralRe: Watermark library and single quotes Pinmemberpbishop4:30 30 Aug '06  
GeneralAtlas can do this too with control extenders PinmemberRobert Ensor21:12 22 Aug '06  
GeneralRe: Atlas can do this too with control extenders [modified] PinmemberDeobrat Singh21:46 22 Aug '06  
GeneralValidators? Pinmemberrjpaulsen10:20 22 Aug '06  
GeneralRe: Validators? PinmemberDeobrat Singh19:29 22 Aug '06  
GeneralRe: Validators? Pinmemberitsmaryann1:04 8 Sep '08  
GeneralFireFox Problem Pinmemberasifrazach5:19 22 Aug '06  
GeneralRe: FireFox Problem [modified] PinmemberDeobrat Singh19:31 22 Aug '06  
GeneralRe: FireFox Problem [modified] PinmemberDeobrat Singh23:34 22 Aug '06  
GeneralRe: FireFox Problem Pinmemberasifrazach3:08 23 Aug '06  
GeneralCSS PinmemberANameNotRegistered1:27 16 Aug '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 29 Aug 2006
Editor:
Copyright 2006 by Deobrat Singh
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project