How to programmatically set the TextBox value and get the TextBox value in ASP.NET in VB.NET

The next example uses a test.aspx page named SetAndGetTextBox.aspx which has two asp:Label controls and one asp:TextBox control. In the code behind the local string variable value is use to read in it the system date and time by using:

Value = DateTime.Now.ToString

The rows in bold show how value is used to set the TextBox and how the TextBox is used to set the Label.

SetAndGetTextBox.aspx

<%@ Page Language=”vb” AutoEventWireup=”false” CodeBehind=”SetAndGetTextBox.aspx.vb”

Inherits=”TextBoxSamplesVB.SetAndGetTextBox” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head runat=”server”>

<title> Set and get the TextBox value</title>

</head>

<body>

<form id=”form1″ runat=”server”>

<div>

<asp:Label ID=”lblOne” runat=”server” Text=”Now is :”></asp:Label>

<asp:TextBox ID=”tbTest” runat=”server”></asp:TextBox>

<br />

<asp:Label ID=”lblTwo” runat=”server” Text=”Label”></asp:Label>

</div>

</form>

</body>

</html>

SetAndGetTextBox.vb

Public Class SetAndGetTextBox

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim Value As String

Value = DateTime.Now.ToString

‘Set value to TextBox

tbTest.Text = Value

‘Get value from TextBox

lblTwo.Text = tbTest.Text

End Sub

End Class