0
TextBox t1 = new TextBox();
qwe2.controls.AddControl(t1);

I added textbox dynamically to qwe2 qwe2 is just a panel

  <asp:panel ID="qwe2" runat="server"></asp:panel>

so how can i set default text to my text and when text box is focused i want it to disappear?

Andrey Belykh
  • 2,578
  • 4
  • 32
  • 46
user2114177
  • 273
  • 1
  • 9
  • 18

4 Answers4

4

This will set a default value, then remove it on focus. If the textbox is blank, it will add the default value back on blur.

TextBox t1 = new TextBox();
t1.Attributes.Add("value", "Default text...");
t1.Attributes.Add("onFocus",@"if(this.value == 'Default text...') {this.value = '';}");
t1.Attributes.Add("onBlur", @"if (this.value == '') {this.value = 'Default text...';}");
qwe2.controls.AddControl(t1);

EDIT: switching font color, though I would advise you to rather use the Jquery placeholder plugin instead https://github.com/mathiasbynens/jquery-placeholder

t1.Attributes.Add("value", "Default text...");
t1.Style.Add("color", "LightGrey");
t1.Attributes.Add("onFocus", @"if(this.value == 'Default text...') {this.value = '';this.style.color ='LightGrey';}else{this.style.color = '';}");
t1.Attributes.Add("onBlur", @"if (this.value == '') {this.value = 'Default text...';this.style.color ='LightGrey';}else{this.style.color = '';}");
t1.Attributes.Add("onClick", "this.style.color = '';");
Riv
  • 1,849
  • 1
  • 16
  • 16
  • Thanks it works))))) one more question the text looks like normal text can i do it shadow greybydefault? you know there is special type of text in textboxes – user2114177 Aug 14 '13 at 10:34
  • It shouldn't do that, cos you're only clearing the value when its "Default text...". – Riv Aug 14 '13 at 11:19
  • Upd: i didn't noticed i didn't deleteold version `tbox("sometext ", t1);` it's working but on blur seems not to be working it lefts textbox empty upd2: fixed the problem it's my mistake )) – user2114177 Aug 14 '13 at 11:32
0

Set default text of a textbox:

t1.Text = "Your default text here.";

For the focus event, you'll find the answer here. Hope this helps.

Edit:

The link I provided is an answer to handle the change of focus, not to set the focus. You can set the focus of a textbox using the Focus() method:

t1.Focus();
Community
  • 1
  • 1
Abbas
  • 14,186
  • 6
  • 41
  • 72
0

I think you need to add placeholder dynamically. you can do with..

t1.Attributes.Add("placeholder", "--your default text here--");

try it.

Umesh Sehta
  • 10,555
  • 5
  • 39
  • 68
0

try this..

    t1.Attributes.Add("onfocus","if (this.value == 'Full Name') {this.value = '';}");
    t1.Attributes.Add("onblur","if (this.value == '') {this.value = 'Full Name';}");
    qwe2.controls.AddControl(t1);
    t1.Focus();

I hope its may help...

Umesh Sehta
  • 10,555
  • 5
  • 39
  • 68