How To Display Watermark In ASPX Textbox Using AJAX Control Toolkit In ASP.NET With C#

In this post, we will learn how to add a watermark inside a textbox using AJAX Control Toolkit in C#. For this example, first, we have to install the AJAX Control Toolkit from NuGet packages. For this example, we have to add a ScriptManager, an ASPX TextBox, and aTextBoxWatermarkExtender for displaying the watermark text in the textbox. Let's start coding.

Install the AJAX Control Toolkit from NuGet Package Manager. Right click on the project and click on "Manage NuGet Packages". Type Ajax Toolkit and install.

Display Watermark In ASPX Textbox Using The AJAX Control Toolkit Using ASP.NET With C#
 
Select AjaxControlToolkit from the list.
 
Display Watermark In ASPX Textbox Using The AJAX Control Toolkit Using ASP.NET With C#
 
After installing the AJAX Control Toolkit, open the toolbox and right-click on it. Select the "Chose items" option for adding all AJAX Control Toolkit controls in the toolbox. The following dialog box will appear for adding the AJAX Control Toolkit controls.

Display Watermark In ASPX Textbox Using The AJAX Control Toolkit Using ASP.NET With C#

When clicking on "Choose items", the below dialog is opened.

Display Watermark In ASPX Textbox Using The AJAX Control Toolkit Using ASP.NET With C#

After opening the above dialog, click on the browse button and go to your project's bin folder for adding this AJAX Control Toolkit dll.

Display Watermark In ASPX Textbox Using The AJAX Control Toolkit Using ASP.NET With C#

All the above steps are completed, now, we can use the AJAX Control Toolkit controls in our toolbox. First, we have to add a ScriptManager from AJAX Extension inside the form tag.

Now, write the below code for displaying a water mark in the textbox.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TextBoxWaterMarkForm.aspx.cs" Inherits="TextBoxWaterMarkForm" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title>TextBoxWatermarkExtender</title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.         <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>  
  12.         <div>  
  13.             <asp:TextBox runat="server" ID="TextBox1"></asp:TextBox>  
  14.             <ajaxToolkit:TextBoxWatermarkExtender ID="TBWE2" runat="server"  
  15.                 TargetControlID="TextBox1"  
  16.                 WatermarkText="Type First Name Here"  
  17.                 WatermarkCssClass="watermarked" />  
  18.         </div>  
  19.     </form>  
  20. </body>  
  21. </html> 
The above code,first displays the watermark in the textbox. But, when we click or focus on the textbox, the  watermark will disappear.

Before Click

Display Watermark In ASPX Textbox Using The AJAX Control Toolkit Using ASP.NET With C#

After Click

Display Watermark In ASPX Textbox Using The AJAX Control Toolkit Using ASP.NET With C#