TextBox control is used to enter data into the form that can be sent to the webserver by posting the form.
The form can be posted by clicking a button or an Image (We will cover it later). When it is rendered on the page, it is implemented through <input> or <textarea> HTML tag.
When
TextMode property is Singleline then this control is rendered as <input> HTML tag with Type property as Text, if Multiline then <textarea>,
and if Password then <input> HTML tag with Type property as Password. If you will not set TextMode property, it will be set to Singleline by default.
Its properties like
BackColor, ForeColor, BorderColor, BorderStyle, BorderWidth, Height etc.
are implemented through style property of respective tags.
You can set its Text property either by setting Text properties in the .aspx page or from server side page. (other properties can also be set from both pages)
When AutoPostBack property of the TextBox is set to true,
then Form is posted back to the server when cursor leaves the box.
If OnTextChanged event has been set, this event will also fire.
DEMO : TextBox
|
Show Source Code
|
|
Ex. TextBox value will be written here
|
// Singleline TextBox code
<asp:TextBox ID="TextBox1" runat="Server"
Width="300"></asp:TextBox>
// Multiline TextBox code
<asp:TextBox ID="TextBox2" runat="Server"
TextMode="multiLine"
Rows="4" Columns="50"
Text="TextBox with TextMode as Multiline">
</asp:TextBox>
// TextBox with AutoPostBack=true
<asp:TextBox ID="TextBox4"
runat="Server"
AutoPostBack="true"></asp:TextBox>
// Fires when Page Loads
protected void Page_Load(object sender, EventArgs e)
{
// Fires only when Page is not posting back
if (!IsPostBack)
{
TextBox1.Text = "Write something here.";
}
}
// Fires when Button is clicked
protected void ChangeLabelText(object sender, EventArgs e)
{
Label1.Text = TextBox1.Text;
}
// Fires when TextChanges in the box
protected void FireTextChange(object sender, EventArgs e)
{
TextBox1.Text = "OnTextChanged event fired";
TextBox2.Text = TextBox3.Text;
}
|
One point that should be noted that If
AutoPostBack property has been set to false, despite
OnTextChanged event has been set, it will not fire.