Knowledge Walls
Gopal Rao
Mumbai, Maharashtra, India
Passcode:
Textbox text disappear on click asp.net in Simple solutions of Asp.net/Ado.net/C#.net(Dotnet)
9756 Views
Introduction 
When you click the textbox the text inside the textbox disappear and when the focus leave from the text box the text will appear.For this i gave one example code below
Preview 

When you click the text box user name will disapear,when focus leave that will appear.
Script
<script type="text/javascript" >
            function do_highlight(ele) {
                document.getElementById(ele.id).style.color = "black";

                if (ele.id.indexOf("UserName") != -1 && document.getElementById(ele.id).value == "Username") {
                    document.getElementById(ele.id).value = "";
                }
                if (ele.id.indexOf("PassLog") != -1 && document.getElementById(ele.id).value == "Password") {
                    document.getElementById(ele.id).value = "";
                }
            }

            function do_unfocus(ele) {
                if (ele.id.indexOf("UserName") != -1 && document.getElementById(ele.id).value == "") {
                    document.getElementById(ele.id).value = "Username";
                }
                if (ele.id.indexOf("PassLog") != -1 && document.getElementById(ele.id).value == "") {
                    document.getElementById(ele.id).value = "Password";
                }

                if (ele.id.indexOf("UserName") != -1 && document.getElementById(ele.id).value == "Username")
                    document.getElementById(ele.id).style.color = "rgb(70,70,70)";

                if (ele.id.indexOf("PassLog") != -1 && document.getElementById(ele.id).value == "Password")
                    document.getElementById(ele.id).style.color = "rgb(70,70,70)";
            }
        </script>
Calling the function to asp button
<asp:TextBox ID="UserName"  runat="server" onfocus="do_highlight(this);"  onblur="do_unfocus(this);" value="Username"  
   MaxLength="20" ForeColor="#707070"></asp:TextBox>
<asp:TextBox ID="PassLog" runat="server"  onfocus="do_highlight(this);"onblur="do_unfocus(this);"value="Password"
   ForeColor="#707070" MaxLength="10" TextMode="Password"></asp:TextBox>
Best Lessons of "Asp.net/Ado.net/C#.net(Dotnet)"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details