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=”C#” AutoEventWireup=”true” CodeBehind=”SetAndGetTextBox.aspx.cs”
Inherits=”TextBoxSamplesVC.SetAndGetTextBox” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”https://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.cs
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;
}
}
}