Click here to Skip to main content
15,886,518 members
Articles / Web Development / ASP.NET
Tip/Trick

JavaScript watermark in place of Ajax watermark in ASP.NET with C#

Rate me:
Please Sign up or sign in to vote.
3.50/5 (2 votes)
30 Apr 2012CPOL 14.2K   228   2
Provides a simple and easy way to use water mark in your application

Introduction 

this code is basically provide a way to users to apply the water mark on there application using simple java script functions  

Using the code 

just simply use this code given below or download its zip file and enjoy the javascript water mark in your application  

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
<script type="text/javascript"
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
    </script>  
   
    <style type="text/css">
    .water
    {
         font-family: Tahoma, Arial, sans-serif;
         color:gray;
    }
    </style>
   
    <script type="text/javascript">
        $(function() {
 
            $(".water").each(function() {
                $tb = $(this);
                if ($tb.val() != this.title) {
                    $tb.removeClass("water");
                }
            });
 
            $(".water").focus(function() {
                $tb = $(this);
                if ($tb.val() == this.title) {
                    $tb.val("");
                    $tb.removeClass("water");
                }
            });
 
            $(".water").blur(function() {
                $tb = $(this);
                if ($.trim($tb.val()) == "") {
                    $tb.val(this.title);
                    $tb.addClass("water");
                }
            });
        });       
 
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div class="smallDiv">
     <h2>TextBox Watermark Demonstration</h2>    <br />          
        <asp:TextBox ID="txtFNm" class="water" Text="Type your First Name"
        Tooltip="Type your First Name" runat="server"></asp:TextBox><br />
        <asp:TextBox ID="txtLNm" class="water" Text="Type your Last Name"
        Tooltip="Type your Last Name" runat="server"></asp:TextBox>
        <br /><br />
        <asp:TextBox ID="TextBox1" runat="server" class="water" Text="Name" 
            ToolTip="Name">Name</asp:TextBox>
        
        <br />
        <br />
        <asp:TextBox ID="TextBox2" runat="server" class="water" Text="Password" 
            ToolTip="Password"></asp:TextBox>
        
        <br />
        <br />
        <br />
        
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
            onclick="btnSubmit_Click" />
        <br /><br />
        Tip: Click on the TextBox to start typing. The watermark
        text disappears.
    </div>
    </form>
</body>
</html>

just  copy and paste this above code or use zip file to complete your requirement regarding water mark in any application  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
John B Oliver27-May-12 11:53
John B Oliver27-May-12 11:53 
Although this is a good tip, it would be worthwhile to explain what the code is doing.
There are four (4) items of interest in the code
- Control setup
- When the control receives focus.
- When the control loses focus.
- The control styling.
QuestionArticle or Tip? Pin
Manfred Rudolf Bihy30-Apr-12 2:24
professionalManfred Rudolf Bihy30-Apr-12 2:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.