Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

i have assigned a default string of text to a text box. now i want if someone click on that textbox, value inside the textbox gets removed.

i am using below code but its not working and giving me an error.
C#
public void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
   TextBox tb = (TextBox)sender;
   tb.Text = string.Empty;
   tb.GotFocus -= TextBox_GotFocus;
}

can anyone please help me in resolving this issue.
Posted
Updated 4-Aug-13 9:10am
v2
Comments
[no name] 4-Aug-13 15:07pm    
"giving me an error" and that error would be.... ?
CHill60 4-Aug-13 15:11pm    
Do you really mean to remove the GotFocus handler?
jonlink01 4-Aug-13 15:13pm    
No, suppose i have assigned a default text = mm//dd//yy to my textbox.

now i want when user click on text box mm//dd//yy gets removed. so that user can update a correct date in a correct format
Maciej Los 4-Aug-13 15:33pm    
Post it as an answer. My virtual 5!

 
Share this answer
 
v3
Please try this.

<input type="text" placeholder="mm/dd/yy" onblur="if(this.value==''){ this.placeholder='mm/dd/yy';this.style.color='#293f46';}" autocomplete="off" id="datepicker" style="color: rgb(41, 63, 70);"/>


Demo URL
Demo URL
 
Share this answer
 
try this one:

ASP.NET
<asp:TextBox ID="new_range1" onChange="newobjCompute()"  onFocus="this.value=''; return false;" runat="server" BorderWidth="0px" Height="47px"
                           Width="90px" style="text-align: center">0</asp:TextBox>


see the "onFocus" tag? set a value for your textbox first. if you click the textbox, it will delete the default value to null.
 
Share this answer
 
v2
why you are writing too much code.check out below code:

<asp:TextBox ID="txt" runat="server" value="test" onfocus="if(this.value=='test')txt.value='';" onblur="if(this.value=='') this.value='test';"></asp:TextBox>


please mark if it helps you.

thanks
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900