TextBox Control

The TextBox control generates single-line text boxes. The text that a user enters in a text box is stored in the Text property, which is inherited from the TextControl base class.

Note   Avoid using duplicate ID property values for TextBox controls on different pages of a single ASP.NET mobile Web application. It is a characteristic of many WML version 1.1 browsers that the value associated with an <input> tag is ignored if a deck that contains an <input> tag of the same name was previously loaded from the same site. The value from the previously loaded <input> tag will be displayed, rather than the value that was most recently provided.

Mobile Controls Syntax

Required properties, defaults, and code-featured elements are noted in bold type.

<mobile:TextBox
  runat="server"
   id="id"
   Font-Name="fontName"
   Font-Size={NotSet|Normal|Small|Large}
   Font-Bold={NotSet|False|True}
   Font-Italic="{NotSet|False|True}
   ForeColor="foregroundColor"
   BackColor="backgroundColor"
   Alignment={NotSet|Left|Center|Right}
   StyleReference="styleReference"
   Wrapping="{NotSet|Wrap|NoWrap}"

   MaxLength="maxLength"
   Numeric="{True, False}"
   Password="{True, False}"
   OnTextChanged="textChangedEventHandler"
   Size="textBoxLength"
   Text="Text"
   InnerText>
</mobile:TextBox>

Note   The ASP.NET ignores style (Font* and *Color) attributes when rendering to the adapter sets.

Custom Attributes

The wmlFormat attribute for the TextBox control is sent to WML devices as the Format property of the WML <input> element. This is a custom attribute and is distinct from the first-class attributes of the control.

Note   The presence of the wmlFormat attribute overrides the WML numeric property for the Textbox control of the WML format. For example, if the wmlFormat attribute is set to NNN, the input element is limited to three numeric characters. For a full listing of valid formats, see the WML specification, available through the Wireless Application Protocol (WAP) Forum at http://www.wapforum.org.

The useRandomID custom attribute for the TextBox control can be set to cause the control identifier (clientID) to be encrypted for security purposes. Textbox controls that have the Password property set to true are automatically encrypted.

For more information, see Custom Attributes.

Containment Rules

The containment rules for all concrete classes derived from the TextControl class are the same and are shown in the following table. The following controls can contain a TextBox control.

Control Comments
System.Web.UI.MobileControls.Form Can contain any number of TextBox controls.
System.Web.UI.MobileControls.Panel Can contain any number of TextBox controls.

The TextBox control cannot contain any other controls.

Device Templates

None.

Device-Specific Behavior

The style with which a label is rendered will differ based on the device, but the text of the label will appear on all devices.

For both HTML and WML, a text box is rendered on its own line as an <input> tag, followed by a <br> tag (line break).

Device language Description of behavior
HTML The Size property determines the length of the rendered TextBox control.

The Title property is ignored.

When the Type property is set to Password, the text box is rendered in a password style. A Type property of Numeric is ignored.

WML The text in the Title property is displayed.

When the Type property is set to Password, the <Input> tag generated has a password style. When the Type attribute is Numeric, the <Input> tag is given a Format attribute for the <Input> tag that suggests a numeric-only field. Devices then interpret this format attribute as if the input mode were switched to numeric mode.

For more information about how the different style attributes are interpreted on different devices, see the Style Inheritance section of the Styles documentation and the Device-Specific Rendering documentation.

Example

The following example demonstrates the use of the OnTextChanged event on the TextBox control. The example contains a single form that displays a label, a text box, and a command. When a user types data in the text box and then clicks the command button, the data is sent back to the server through postback. If the text in the TextBox control is different than the previous postback, a message is given indicating that it has been changed. This example also demonstrates some of the TextBox control properties, setting a length limit and password entry mode.

<%@ Page Inherits="System.Web.UI.MobileControls.MobilePage" 
   Language="VB" %>
<%@ Register TagPrefix="mobile"
    Namespace="System.Web.UI.MobileControls"
    Assembly="System.Web.Mobile" %>

<script language="vb" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
   ' Provide the default setting.
   Label1.Text = "TextBox Unchanged"

   ' Dynamically set attributes of the TextBox.
   TextBox1.Alignment = Alignment.Center
   TextBox1.MaxLength = 5
   TextBox1.Password = True
End Sub

Sub AlertUser(sender As Object, e As EventArgs)
   Label1.Text = "TextBox Changed"
End Sub
</script>

<mobile:Form id="Form1" runat="server">
   <mobile:Label runat="server" id="Label1" Alignment="center" />
   <mobile:TextBox runat="server" id="TextBox1" 
      OnTextChanged="AlertUser" />
   <mobile:Command runat="server" Text="Submit" 
      Alignment="center" />
</mobile:Form>
[C#]
<%@ Page Inherits="System.Web.UI.MobileControls.MobilePage" 
   Language="c#" %>
<%@ Register TagPrefix="mobile"
    Namespace="System.Web.UI.MobileControls"
    Assembly="System.Web.Mobile" %>

<script language="c#" runat="server">
void Page_Load(object sender, EventArgs e)
{
   // Provide the default setting.
   Label1.Text = "TextBox Unchanged";

   // Dynamically set attributes of the TextBox.
   TextBox1.Alignment = Alignment.Center;
   TextBox1.MaxLength = 5;
   TextBox1.Password = true;
}

void AlertUser(Object sender, EventArgs e)
{
   Label1.Text = "TextBox Changed";
}
</script>

<mobile:Form id="Form1" runat="server">
   <mobile:Label runat="server" id="Label1" Alignment="center" />
   <mobile:TextBox runat="server" id="TextBox1" 
      OnTextChanged="AlertUser" />
   <mobile:Command runat="server" Text="Submit" 
      Alignment="center" />
</mobile:Form>

See Also

TextBox Class | TextBox Class Members | Custom Attributes | Control Reference