TextBox

Content:

  1. Get value from TextBox
  2. Set value to TextBox
  3. Get value from TextBox when Master page is used
  4. Set value to TextBox when Master page is used

Get value from TextBox:

var textbox1 = document.getElementById("TextBox1");
alert(textbox1.value);

Set value to TextBox:

var textbox1 = document.getElementById("TextBox1");
textbox1.value = 'Welcome to sbo';
alert(textbox1.value);

If you are using Master page and Content page then the Text Box id will be changed. why?

When you are using a master page with content page then the control id will be chaged some thing like 'ContentPlaceHolder1_TextBox1' when it is renderd to browser. TextBox id is prefixed with master page ContentPlaceHolder id. Suppose your TextBox is inside a User Control, then it's id is prefixed with Use Control ID.

Get value from TextBox when Master page is used:

var textbox1 = document.getElementById("<%= TextBox1.ClientID %>");
alert(textbox1.value);

Set value to TextBox when Master page is used:

var textbox1 = document.getElementById("<%= TextBox1.ClientID %>");
textbox1.value = 'Welcome to sbo';
alert(textbox1.value);

Note:

As we discussed, if you are using Master page and Content page then id's will get changed.

Read more detailed article here.

  • Updated
    Jan 07, 2015
  • Views
    2,386