asp.net.ph

Skip Navigation Links

Getting and Setting Values in TextBox Controls

Controls You Can Use on Web Forms   ASP.NET Standard Controls   TextBox Control


You can get or set the value of a TextBox control at run time.

To get or set values in the TextBox control

  • Get or set the value of the control’s Text property.

The following example shows how you can read the value of a TextBox control and use it to set the value of another control, in this case, a Label control.

In this case, the method simply handles a Button control’s Click event and displays in the Label whatever the user has typed into the TextBox.

void setLabelText ( object src, EventArgs e ) {
   msg.Text = myTextBox.Text;
}
  C# VB
TextBox Control Example
Run Sample | View Source

NOTE: User input in a Web Forms page can include potentially malicious client script. By default, the Web Forms page validates that user input does not include script or HTML elements. In addition, authors can apply HTML encoding to strings to protect against script exploits in a Web application.

private void myTextHandler ( object src, EventArgs e ) {
   ...
   msg.Text = Server.HtmlEncode ( myTextBox.Text );
}
  C# VB

See Also

Web Forms Events and Handlers   Setting Web Server Control Properties Programmatically



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note