Textbox Water Mark in ASP.Net

Introduction

This article explains how to create a watermark in a TextBox using JavaScript and also with Ajax. To do that you can use the following procedure.

Step 1

Download the Ajax control toolkit from link.

Step 2

Right-click on the standard toolbox then select the "Add" tab, provide the name of the tab.

add-tab-name.png

Step 3

Right-click on the new tab then choose "items" then click on the "Browse" button and browse to "AjaxControlToolLit.dll". Then click on the "OK" button.

Step 4

Right-click in the Solution Explorer then select "Add reference" then browse to "AjaxControlToolLit.dll". Then click on the "OK" button.

Step 5

Drag the "ToolKitScriptManager" on the top of your aspx page from the new tab controls.

Step 6

Then drag the "TextBoxWatermarkExtender".

Step 7

Set the TextBoxWatermarkExtender properties TargetControlID=" ", WatermarkText=" ". The "TargetControlID" property specifies the TextBox id, on which you want to set the watermark text, and "WatermarkText" accepts the text as watermark for that TextBox.

Code for TextBoxWatermarkExtender

  1. <asp:TextBoxWatermarkExtender ID="TextBox2_TextBoxWatermarkExtender" runat="server" TargetControlID="TextBox2" WatermarkText="Message1"></asp:TextBoxWatermarkExtender>

 

Full Source code

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <style type="text/css">  
  7.         .style2  
  8.         {  
  9.             width: 128px;  
  10.             height: 31px;  
  11.             position: absolute;  
  12.             left: 27px;  
  13.             top: 83px;  
  14.         }  
  15.         .style3  
  16.         {  
  17.             width: 128px;  
  18.             height: 31px;  
  19.             position: absolute;  
  20.             left: 30px;  
  21.             top: 135px;  
  22.         }  
  23.     </style>  
  24.     </head>  
  25. <body>  
  26.     <form id="form1" runat="server">  
  27.     <div>  
  28.            <h3>  
  29.     <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">  
  30.            </asp:ToolkitScriptManager>  
  31.                Ajax TextBox Water Mark</h3>  
  32.            </div>  
  33.         <asp:TextBox ID="TextBox1" runat="server" CssClass="style2" ></asp:TextBox>  
  34.            <asp:TextBoxWatermarkExtender ID="wmr"  
  35.                runat="server" TargetControlID="TextBox1" WatermarkText="Message1"></asp:TextBoxWatermarkExtender>  
  36.     <p> </p>  
  37.     <p> </p>  
  38.     <p>  
  39.         <asp:TextBox ID="TextBox2" runat="server" CssClass="style3" ></asp:TextBox>  
  40.            <asp:TextBoxWatermarkExtender ID="TextBox2_TextBoxWatermarkExtender"  
  41.                runat="server" TargetControlID="TextBox2" WatermarkText="Message1"></asp:TextBoxWatermarkExtender>  
  42.                </p>  
  43.     </form>  
  44. </body>  
  45. </html> 

Step 8

Output

Ajax-watermark1-textbox.png

Ajax-watermark2-textbox.png
If you do not want to do so much then just use the simple sample JavaScript code, which is described below. That JavaScript code provides you the same functionality that you have done before and can enjoy it with less work and quicker improved code.

Now for how to watermark a TextBox using JavaScript.

JavaScript Code

The given following scriptuses the "onblur" and "onfocus" events of the TextBox. The scripts have two checks; the first one check if the TextBox is empty and if the "blur" event is set then the water mark is done the same as you when assign the TextBox text manually using  "document.getElementById(txtcontrol)" and changes the font color to "red", and the first check is that when the TextBox text matches the default text and the event type is focus then it clears the TextBox and sets the font color to black.

  1. <script type = "text/javascript">  
  2.        function SetWaterMark(txtcontrol, event) {  
  3.            if (txtcontrol.value.length == 0 && event.type == "blur") {  
  4.                var a = document.getElementById(txtcontrol);  
  5.                txtcontrol.style.color = "red";  
  6.                txtcontrol.value = a;  
  7.            }  
  8.            if (event.type == "focus") {  
  9.                txtcontrol.style.color = "black";  
  10.                txtcontrol.value = "";  
  11.            }  
  12.        }  
  13. </script> 

Code for textboxes

Here I am calling the same script on the "onblur" and "onfocus" events and passing the reference of the TextBox and the event object.

  1. <asp:TextBox ID="TextBox1" runat="server" CssClass="style4" Text = "First Name" ForeColor ="blue" onblur = "SetWaterMark(this, event);" onfocus = "SetWaterMark(this, event);"></asp:TextBox>  
  2. <asp:TextBox ID="TextBox2" runat="server" CssClass="style3" Text = "Last Name" ForeColor = "blue" onblur = "SetWaterMark(this, event);" onfocus = "SetWaterMark(this, event);"></asp:TextBox>

 

Default.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.    <script type = "text/javascript">  
  6.        function SetWaterMark(txtcontrol, event) {  
  7.            if (txtcontrol.value.length == 0 && event.type == "blur") {  
  8.                var a = document.getElementById(txtcontrol);  
  9.                txtcontrol.style.color = "red";  
  10.                txtcontrol.value = a;  
  11.            }  
  12.            if (event.type == "focus") {  
  13.                txtcontrol.style.color = "black";  
  14.                txtcontrol.value = "";  
  15.            }  
  16.        }  
  17. </script>  
  18.     <style type="text/css">  
  19.         .style2  
  20.         {  
  21.             width: 56px;  
  22.             height: 26px;  
  23.             position: absolute;  
  24.             left: 36px;  
  25.             top: 127px;  
  26.         }  
  27.         .style3  
  28.         {  
  29.             width: 128px;  
  30.             height: 22px;  
  31.             position: absolute;  
  32.             left: 11px;  
  33.             top: 79px;  
  34.         }  
  35.         .style4  
  36.         {  
  37.             width: 128px;  
  38.             height: 22px;  
  39.             position: absolute;  
  40.             left: 10px;  
  41.             top: 46px;  
  42.         }  
  43.     </style>  
  44. </head>  
  45. <body>  
  46.     <form id="form1" runat="server">  
  47.     <div>  
  48.     <h3>Water Mark Textboxes</h3>  
  49.         <p> </p>  
  50.         <p> </p></div>  
  51.     <asp:TextBox ID="TextBox1" runat="server" CssClass="style4" Text = "First Name" ForeColor ="blue" onblur = "SetWaterMark(this, event);"  
  52.      onfocus = "SetWaterMark(this, event);"></asp:TextBox>  
  53.     <asp:TextBox ID="TextBox2" runat="server" CssClass="style3" Text = "Last Name" ForeColor = "blue" onblur = "SetWaterMark(this, event);"  
  54.     onfocus = "SetWaterMark(this, event);"></asp:TextBox>  
  55.     <asp:Button ID="Button1" runat="server" CssClass="style2" Text="Button" />  
  56.     </form>  
  57. </body>  
  58. </html> 

Output

watermark-textbox1.png

watermark-textbox2.png

watermark-textbox3.png


Similar Articles