0

How do I set default text for asp textbox during page load and clear them when focused on..

I know to do it in html textbox using javascript and jquery..But how to do it in asp.net.. Any help please..

I wrote

<asp:TextBox ID="Rloginemail" runat="server" Width="100%" Height="25px" 
     BackColor="#ECE8D0" BorderStyle="None" ForeColor="#482B1B" 
     Text="E-MAIL" >E-MAIL</asp:TextBox>

In the above code you can see text=email.. if I use this default text is shown but not cleared on focus..

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sasidharan
  • 3,676
  • 3
  • 19
  • 37

6 Answers6

10

HTML 5

You can use placeholder html5 attribute

<input type="text" placeholder="search" />

jQuery Placeholder Plugin

If your browser doesnt support then use this jquery placeholder

$('input, textarea').placeholder()
Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120
2

You have mentioned that you can do it for html textbox using javascript or jQuery, but don't know how to do it with Asp.Net textbox. right? Is it just because id of your asp.net textbox changes dynamically and you are not able to access it?. If yes, then you can access the id using following code:

document.getElementById("<%=textbox.ClientID%>")

You can use above code in the script written in the aspx code to get asp.net textbox and then you can use javascript/jQuery as you know how to do it.

Nitin Joshi
  • 1,638
  • 1
  • 14
  • 17
0

Use OnFocus event and set Text property to empty string

Example:

<asp:TextBox ID="Rloginemail" runat="server" Width="100%" Height="25px" BackColor="#ECE8D0" BorderStyle="None" ForeColor="#482B1B" Text="E-MAIL" onfocus="if (this.value == 'E-MAIL')  this.value = ''; ">E-MAIL</asp:TextBox>

Or, as mentioned in this answer - you can also use asp.net textbox property called placeholder:

        <asp:TextBox ID="Rloginemail" runat="server" Width="100%" Height="25px" 
                     BackColor="#ECE8D0" BorderStyle="None" ForeColor="#482B1B" 
                     placeholder="E-MAIL" ></asp:TextBox>
Community
  • 1
  • 1
Nogard
  • 1,779
  • 2
  • 18
  • 21
  • "I know to do it in html textbox using javascript and jquery..But how to do it in asp.net" – tariq Nov 28 '13 at 11:05
  • html textbox is not asp textbox. And you cant do it in pure ASP.net as it would require postback to process. Check others solutions if you doubt so – Nogard Nov 28 '13 at 11:07
  • i know that its not possible purely through ASP.net, nearest would be to use ajax control toolkit, isn't it? – tariq Nov 28 '13 at 11:10
0

In asp.net you can do it by using ajax control toolkit. we have a TextboxExtender there which can do it besides other things as below

<ajaxToolkit:TextBoxWatermarkExtender ID="TBE" runat="server"
TargetControlID="youTextBoxID"
WatermarkText="YOUR DEFAULT TEXT"
WatermarkCssClass="yourPreferred css" />
tariq
  • 2,193
  • 15
  • 26
0

We use Ajax Control Toolkit's Textbox Watermark extender for this: http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/TextBoxWatermark/TextBoxWatermark.aspx

SteinIP
  • 376
  • 4
  • 5
0
  $(document).ready(function () {
    $(":input[data-watermark]").each(function () {
        $(this).val($(this).attr("data-watermark"));
        $(this).css("color", "#a8a8a8");
        $(this).bind("focus", function () {
            if ($(this).val() == $(this).attr("data-watermark")) $(this).val('');
            $(this).css("color", "#000000");
        });
        $(this).bind("blur", function () {
            if ($(this).val() == '') {
                $(this).val($(this).attr("data-watermark"));
                $(this).css("color", "#a8a8a8");

            }
            else {
                $(this).css("color", "#000000");
            }
        });
    });
});

style

<style>
.placeholder {
color: #a8a8a8;

}

::-webkit-input-placeholder {
color: #a8a8a8;

}

:-moz-placeholder {
color: #a8a8a8;

}

.home_textbox{width:155px; height:25px; border:1px solid #c9c5c1; border-radius:5px; position:relative; font-size:13px; color:#b5b0aa; font-family:"Myriad Pro"; padding:0 5px;}

textbox

<input type="text" name="TxtLogin" id="TxtLogin"
                            class="home_textbox" runat="server" tabindex="1" style="color:Black;" data-watermark="Username" />
Indranil.Bharambe
  • 1,462
  • 3
  • 14
  • 25