Some time you need watermark effect on Asp.net Text Box . then you use this code
var defaultText = “Enter text here”;
function WaterMarks(txt, evt)
if(txt.value.length == 0 && evt.type == “blur”)
txt.style.color = “gray”;
txt.value = defaultText;
if(txt.value == defaultText && evt.type == “focus”)
txt.style.color = “black”;
Above script will be called on onblur and onfocus events of the Text Box.
The script does the following two checks
1. If the Text Box is empty and the event type is blur event it sets the watermark and changes the font color as Gray.
2. If the Text Box matches default text and the event type is the focus it clears the Text Box and sets the font color as Black.
<asp:TextBox ID=”txtName” runat=”server” Text = “Enter text here”
ForeColor = “Gray” onblur = “WaterMarks(this, event);”
onfocus = “WaterMarks(this, event);”>
if Visual Studio will throw warning when you call client side event in the above way.so you need to call from code behind
txtName.Attributes.Add(“onblur”, “WaterMark(this, event);”);
txtName.Attributes.Add(“onfocus”, “WaterMark(this, event);”);