views:

161

answers:

6

I am using ASP.NET.

I am not sure what you will call this but I would like text to be displayed in my text box called txtName. So when the form load the text box will have faded text that will say "Required". Then when the user click inside the text box i want the user to place a value inside the text box.

Is this possible in ASP.NET? If so, how can this be done????

+4  A: 

If you can use the MS AjaxControlToolkit, there is a watermark extender that will do precisely that.

MS Ajax TextBoxWatermark Extender

If not, you can probably find a jQuery plugin or roll your own javascript. Using server-side events will probably not do what you want.

brentkeller
+1  A: 

Here's a great jQuery plug-in:

http://www.mudaimemo.com/p/formnotifier/
Nissan Fan
+1  A: 

Try something like this:

<asp:TextBox ID="txtName" runat="server"
                            onclick="if (this.value == 'Required') {this.value=''}"
                            Width="275px">Required</asp:TextBox>
Jack Marchetti
thanks it does work, how can i use this code and make the font light on load and then when click change the font back to normal color?
Etienne
A: 

If using the Ajax Toolkit is an option or makes sense there is a tool in the tool box called textbox watermark (see http://www.asp.net/AJAX/AjaxControlToolkit/Samples/TextBoxWatermark/TextBoxWatermark.aspx)

Michael Gattuso
+3  A: 

onClick is a client side event. onFocus may be better. You can use txtName.Attributes.Add("onFocus", "this.value = '';this.style.color = 'black';")

Put it in the Page's PreRender event.

MikeB
A: 

best to use Jquery plugin In field Labels http://fuelyourcoding.com/in-field-labels/

Muhammad Akhtar