65.9K
CodeProject is changing. Read more.
Home

Creating a watermark ("") textbox control

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.09/5 (10 votes)

Mar 24, 2006

CPOL
viewsIcon

49381

downloadIcon

407

This article describes the implementation of a watermark textbox control.

Sample Image - WatermarkTextBoxControl.jpg

Introduction

This article shows the source code required to create a textbox control which will show a default string when no content is entered. This can be a useful feature when you need to guide a user in filling in the required data in a web form.

Typical uses of this control include showing "< enter your text here >" or "< search terms >" in gray in a search box in a web page.

The control shows the text when no content is entered, and removes the text when the control receives focus (when the textbox is selected by mouse or by pressing tab, in the onFocus event). When the control loses focus (in the onBlur event), when no text is entered, the watermark text is again inserted in the textbox.

Usage

Typical usage of this control is demonstrated here:

<%@ Register TagPrefix="WeSeeSc" 
  Namespace="We.See.Web.ServerControls" Assembly="ServerControls" %>

Search:
<WeSeeSc:WatermarkTextBox id="query" Runat="server" 
  Watermark="< enter your question >"></WeSeeSc:WatermarkTextBox></TD>