Create new account I forgot my password    

Create a TextBoxWatermark Extender Functionality using ASP.NET and jQuery
Rating: 14 user(s) have rated this article Average rating: 3.9
Posted by: Suprotim Agarwal, on 12/10/2008, in category "jQuery and ASP.NET"
Views: this article has been read 21925 times
Abstract: I had recently written an article Creating CollapsiblePanelExtender Functionality using ASP.NET and jQuery to use the ASP.NET and jQuery to create a functionality similar to that provided by the CollapsiblePanelExtender. In this article, I will show you how to create functionality similar to the TextBoxWatermark using jQuery with ASP.NET.

Create a TextBoxWatermark Extender Functionality using ASP.NET and jQuery
 
I had recently written an article Creating CollapsiblePanelExtender Functionality using ASP.NET and jQuery  to use the ASP.NET and jQuery to create a functionality similar to that provided by the CollapsiblePanelExtender. In this article, I will show you how to create functionality similar to the TextBoxWatermark using jQuery with ASP.NET. If you are just getting started with jQuery, check this: Using jQuery with ASP.NET - A Beginner's Guide.
Open Visual Studio 2008 > File > New > Website > Choose ‘ASP.NET 3.5 website’ from the templates > Choose your language (C# or VB) > Enter the location > Ok. In the Solution Explorer, right click your project > New Folder > rename the folder as ‘Scripts’.
Right click the Scripts folder > Add Existing Item > Browse to the path where you downloaded the jQuery library (jquery-1.2.6.js) and the intellisense documentation (jquery-1.2.6-vsdoc.js) > Select the files and click Add.
Now drag and drop the jquery-1.2.6.js file from the Solution Explorer on to your page to create a reference as shown below:
jQuery Ref
Add two labels and two textbox controls to the page. I have added them in a <div> container and have used css to achieve a similar look and feel as shown in the ASP.NET AJAX TextBoxWatermark Extender Toolkit Documentation.
<head runat="server">
    <title>TextBoxWatermarkUsingjQuery</title>
   
    <style type="text/css">
        .demoheading
        {
              padding-bottom:20px;
              color:#5377A9;
              font-family:Arial, Sans-Serif;
              font-weight:bold;
              font-size:1.5em;
        }
        .water
        {
            font-family: Tahoma, Arial, sans-serif;
            font-size:75%;
            color:gray;
        }
    </style>
   
</head>
<body>
    <form id="form1" runat="server">  
        <div class="demoheading">TextBoxWatermark Demonstration Using jQuery</div>      
        <asp:Label ID="lblFNm" runat="server" Text="First name:"></asp:Label>
        <asp:TextBox ID="txtFNm" class="water" Text="Type your First Name" runat="server"></asp:TextBox><br />
        <asp:Label ID="lblLNm" runat="server" Text="Last name:"></asp:Label>
        <asp:TextBox ID="txtLNm" class="water" Text="Type your Last Name" runat="server"></asp:TextBox>                             
   
    </form>
</body>
The final step is to add Watermark behavior to the TextBoxes. Use this jQuery script shown below to do so:
   <script type="text/javascript">
        $(document).ready(function() {
 
            $(".water").focus(function() {
                if ($(this).val() == this.defaultValue) {
                    $(this).val("");
                }
            });
 
            $(".water").blur(function() {
                if ($.trim($(this).val()) == "") {                   
                    $(this).val(this.defaultValue);
                }
            });
        });       
 
    </script>
The jQuery above performs operations on controls marked with the ‘class=water’ attribute. If the textbox contains value and a user sets focus on it, the code empties the textbox. Similarly, if the user clicks on an empty textbox, the value is set back to the defaultValue, thereby creating a watermark effect. Simple, isn’t it! Run the application
At page load:
Load 1
When the user clicks on the FirstName textbox
Load 2
When the user clicks on the LastName textbox
Load 3
The entire code is mentioned here for your reference: [Updated after the handy tip from Arnold Matusz]
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TextBoxWaterMarkUsingjQuery.aspx.cs" Inherits="TextBoxWaterMarkUsingjQuery" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>TextBoxWatermarkUsingjQuery</title>
 
    <script src="Scripts/jquery-1.2.6.js" type="text/javascript"></script>
   
    <script type="text/javascript">
        $(document).ready(function() {
 
            $(".water").focus(function() {
                if ($(this).val() == this.title) {
                    $(this).val(""); $(this).removeClass("water");
                }
            });
 
            $(".water").blur(function() {
                if ($.trim($(this).val()) == "") {                   
                    $(this).val(this.title); $(this).addClass("water");
                }
            });
        });       
 
    </script>
   
   
    <style type="text/css">
        .demoheading
        {
              padding-bottom:20px;
              color:#5377A9;
              font-family:Arial, Sans-Serif;
              font-weight:bold;
              font-size:1.5em;
        }
        .water
        {
            font-family: Tahoma, Arial, sans-serif;
            font-size:75%;
            color:gray;
        }
    </style>
   
</head>
<body>
    <form id="form1" runat="server">  
        <div class="demoheading">TextBoxWatermark Demonstration Using jQuery</div>      
        <asp:Label ID="lblFNm" runat="server" Text="First name:"></asp:Label>
        <asp:TextBox ID="txtFNm" class="water" Text="Type your First Name" runat="server" Tooltip="Type your First Name"></asp:TextBox><br />
        <asp:Label ID="lblLNm" runat="server" Text="Last name:"></asp:Label>
        <asp:TextBox ID="txtLNm" class="water" Text="Type your Last Name" runat="server" Tooltip="Type your Last Name"                 ></asp:TextBox>                             
   
    </form>
</body>
</html>
 
I am beginning to wonder what is it that can’t be done using jQuery..!
I hope you liked the article and I thank you for viewing it.
 If you liked the article,  Subscribe to my RSS Feed or Subscribe Via Email
 









Page copy protected against web site content infringement by Copyscape


How would you rate this article?

User Feedback
Comment posted by Sourav on Saturday, January 24, 2009 5:04 PM
This works good.But if I submit the page then the  initialValue of the text field gets replaced with whatever values I have put during submission. Say,in the example above I enter "Sourav" as first name and click on Submit button. After the page reloads the initialVlaue of the First Name text field becomes "Sourav" and as result whenever I focus on the text field it becomes blank, which is not correct.
Comment posted by yassir on Sunday, January 25, 2009 6:56 AM
>>>I am beginning to wonder what is it that can’t be done using jQuery..!

almost nothing :D

good article by the way
Comment posted by NMyVision on Monday, January 26, 2009 12:18 AM
I would suggest using a different property like tooltip, to avoid problems like Sourav mentioned.
Comment posted by Tim on Monday, January 26, 2009 8:20 AM
@NMyVision but what would you but in place of this.defaultValue if using the tool tip?
Comment posted by Jorge Salinas on Monday, January 26, 2009 9:05 AM
very simple and useful, thanks a lot.
Comment posted by Tim on Monday, January 26, 2009 11:26 AM
@NMyVision but what would you but in place of this.defaultValue if using the tool tip?
Comment posted by Thanigainathan on Tuesday, January 27, 2009 1:14 AM
Hi,

Article is nice and good.
Can you please let us know about the performance of the JQuery ?

Is that adviced to use in production environments ?

Thanks,
Thani
Comment posted by Suprotim Agarwal on Tuesday, January 27, 2009 1:30 AM
Thanigainathan: jQuery is a lightweight, fast JavaScript library and can be used in production environments. Here's a very good article by John Resig (creator of jQuery) about profiling apps using jQuery

http://ejohn.org/blog/deep-profiling-jquery-apps/
Comment posted by Arnold Matusz on Thursday, February 26, 2009 3:46 AM
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="jQueryWaterMark.aspx.cs"
    Inherits="jQueryWaterMark" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>TextBoxWatermarkUsingjQuery</title>

    <script src="JavaScript/jquery-1.3.2.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(document).ready(function() {

            $(".water").each(function() {
                if ($(this).val() == this.title) {
                    $(this).addClass("opaque");
                }
            });

            $(".water").focus(function() {
                if ($(this).val() == this.title) {
                    $(this).val("");
                    $(this).removeClass("opaque");
                }                
            });

            $(".water").blur(function() {
                if ($.trim($(this).val()) == "") {
                    $(this).val(this.title);
                    $(this).addClass("opaque");
                }
                else {
                    $(this).removeClass("opaque");
                }
            });
        });      

    </script>

    <style type="text/css">
        .demoheading
        {
            padding-bottom: 20px;
            color: #5377A9;
            font-family: Arial, Sans-Serif;
            font-weight: bold;
            font-size: 1.5em;
        }
        .water
        {
            font-family: Tahoma, Arial, sans-serif;
            font-size: 75%;
        }
        .opaque
        {
            color: Gray;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div class="demoheading">
        TextBoxWatermark Demonstration Using jQuery</div>
    <asp:Label ID="lblFNm" runat="server" Text="First name:"></asp:Label>
    <asp:TextBox ID="txtFNm" class="water" ToolTip="Type your First Name" Text="Type your First Name"
        runat="server"></asp:TextBox><br />
    <asp:Label ID="lblLNm" runat="server" Text="Last name:"></asp:Label>
    <asp:TextBox ID="txtLNm" class="water" ToolTip="Type your Last Name" Text="Type your Last Name"
        runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="btnTest" runat="server" OnClick="btnTest_Click" Text="Test it all" />
    </form>
</body>
</html>

This code will solve the problem that Sourav posed.
Arnold Matusz
http://blog.dreamlabsolutions.com
Comment posted by Gregory Franz on Thursday, February 26, 2009 4:42 AM
Arnold, Thank you very much. Your solution is working.

Thank you also for Dotnetcurry for the wonderful articles.

Thank You
Gregory
/\/*****\/\

Comment posted by Suprotim Agarwal on Thursday, February 26, 2009 6:43 AM
Arnold, Gregory: Thanks!
Comment posted by Gregory Franz on Thursday, February 26, 2009 7:19 AM
Arnold, Thank you very much. Your solution is working.

Thank you also for Dotnetcurry for the wonderful articles.

Thank You
Gregory
/\/*****\/\

Comment posted by Sri on Saturday, October 17, 2009 1:00 PM
Is this working on VS 2005?
Comment posted by Sri on Saturday, October 17, 2009 1:11 PM
ok this works fine in vs 2005

<script type="text/javascript">
        $(document).ready(function() {

            $(".water").focus(function() {
                if ($(this).val() == this.defaultValue) {
                    $(this).val("");
                }
            });

            $(".water").blur(function() {
                if ($.trim($(this).val()) == "") {                  
                    $(this).val(this.defaultValue);
                }
            });
        });      

    </script>


but this is not working vs 2005

<script type="text/javascript">
        $(document).ready(function() {

            $(".water").each(function() {
                if ($(this).val() == this.title) {
                    $(this).addClass("opaque");
                }
            });

            $(".water").focus(function() {
                if ($(this).val() == this.title) {
                    $(this).val("");
                    $(this).removeClass("opaque");
                }                
            });

            $(".water").blur(function() {
                if ($.trim($(this).val()) == "") {
                    $(this).val(this.title);
                    $(this).addClass("opaque");
                }
                else {
                    $(this).removeClass("opaque");
                }
            });
        });      

    </script>
Comment posted by Nike on Saturday, March 20, 2010 7:50 AM
http://techdotnets.blogspot.com/
Comment posted by Mehboob on Saturday, July 03, 2010 3:43 AM
Excellent,gr888

Post your comment
Name:
E-mail: (Will not be displayed)
Comment:
Insert Cancel

NEWSLETTER