<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderHeader" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderForTitleAndIntro" Runat="Server">
<table width="100%" cellpadding="2" cellspacing="0">
<tr valign="top" class="ArticleTitle">
<td style="padding-left:10px;" valign="middle">
asp:TextBox control</td>
</tr>
<tr>
<td class="ArticleContents">
TextBox control is used to enter data into the form that can be sent to the webserver by posting the form.
</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</table>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PlaceHolderForContents" Runat="Server">
<div class="ArticleContents">
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 <span class="DemoCP">TextMode</span> 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 <span class="DemoCP">BackColor, ForeColor, BorderColor, BorderStyle, BorderWidth, Height etc. </span>
are implemented through style property of respective tags.
<p>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 <span class="DemoCP">AutoPostBack</span> property of the TextBox is set to true,
then Form is posted back to the server when cursor leaves the box.
If <span class="DemoCP">OnTextChanged</span> event has been set, this event will also fire.
</p>
<!-- START - Demo Section -->
<table class="DemoPlaceHolder" border="1" cellpadding="2" cellspacing="4">
<tr>
<td class="DemoTitle">
DEMO : TextBox
</td>
<td align="right">
<a class="DemoShowSource" href="../../misc/codeviewer/default.aspx?pagename=~/tutorials/controls/textbox.aspx" target="_blank">Show Source Code</a>
</td>
</tr>
<tr>
<td>
<table border="0">
<tr>
<td>
<asp:Label ID="lbl" AssociatedControlID="TextBox1" runat="Server" Text="TextMode is Singleline"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="Server" Width="300" ForeColor="blue" BorderColor="brown"></asp:TextBox>
</td>
</tr>
<tr valign="Top">
<td>
<asp:Label ID="Label2" AssociatedControlID="TextBox1" runat="Server" Text="TextMode is Multiline"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="Server" TextMode="multiLine" Rows="4" Columns="50" Text="TextBox with TextMode as Multiline"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" AssociatedControlID="TextBox3" runat="Server" Text="PostBack the form"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="Server" AutoPostBack="false" OnTextChanged="FireTextChange"></asp:TextBox> It will Postback the page when cursor leaves this box.
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="btnSubmit" runat="Server" OnClick="ChangeLabelText" Text="Submit" />
</td>
</tr>
</table>
</td>
<td>
Ex. <asp:Label ID="Label1" runat="server" Text="TextBox value will be written here" BorderWidth="1" BorderColor="Brown"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<!-- START - Server Side Code -->
<pre>
// 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;
}
</pre>
<!-- END - Server Side Code -->
</td>
</tr>
</table>
One point that should be noted that If <span class="DemoCP">AutoPostBack</span> property has been set to false, despite <span class="DemoCP">OnTextChanged</span> event has been set, it will not fire.
<!-- END - Demo Section -->
</div>
<br />
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="PlaceHolderFooter" Runat="Server">
</asp:Content>
Go Top