How to create water text mark on asp.net textbox ?


DefaultSearch

Hi

We can create water text mark on asp.net textbox using two approach
a. Using ajax toolkit
b.Using Jquery

Recently i used this feature in one of my personal project using jquery like this. Here is the complete working code


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SearchForm.aspx.cs" Inherits="SearchForm" %>

<!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></title>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
     <script type="text/javascript">
         $(document).ready(function () {
             var searchBox = $("#<%=txtName.ClientID %>");
             searchBox.focus(
             function () {
                 if (searchBox.val() == this.title) {
                     searchBox.removeClass("defaultCSS");
                     searchBox.val("");
                 }
             });

             searchBox.blur(
             function () {
                 if (searchBox.val() == "") {
                     searchBox.addClass("defaultCSS");
                     searchBox.val(this.title);
                 }

             });
             searchBox.blur();
         });
     
     </script>
     <style type="text/css" media="screen">
     .defaultCSS
     {
         color:#CCCCCC;
         font-style:italic;
     }
     
     </style>

</head>

<body>
    <form id="form1" runat="server">
    <div>
    <p><asp:TextBox ID="txtName" runat="server" Width="150px" ToolTip="Enter the city Name"/><asp:Button ID="btnSearch" runat="server" Text="Search" /></p>
        
    </div>
    </form>
</body>
</html>


Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.