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

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.



<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”SetAndGetTextBox.aspx.cs”

Inherits=”TextBoxSamplesVC.SetAndGetTextBox” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”>

<html xmlns=””>

<head runat=”server”>

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



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


<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>






using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;


namespace TextBoxSamplesVC


public partial class SetAndGetTextBox : System.Web.UI.Page


protected void Page_Load(object sender, EventArgs e)


// Read system date and time

string value;

value = DateTime.Now.ToString();

// Set value to TextBox

tbTest.Text = value;

// Get value from TextBox

lblTwo.Text = tbTest.Text;