共用方式為


TextBox 控制項

TextBox 控制項會產生單行文字方塊。使用者輸入文字方塊的文字存放在繼承自 TextControl 基底類別的 Text 屬性 (Property) 中。

注意 請避免在單一 ASP.NET Mobile Web 應用程式的不同網頁上對 TextBox 控制項使用重複的 ID 屬性 (Property) 值。如果含有相同名稱 <input> 標記的 Deck 已事先從相同網站載入,與 <input> 標記相關的值會被忽略;這是許多 WML 版本 1.1 瀏覽器的特性。將顯示事先載入的 <input> 標記值,而非最近提供的值。

行動控制項語法

需要的屬性和具程式碼功能的項目以粗體樣式標註。

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

注意 ASP.NET 會在呈現至配置器集時忽略樣式 (Font**Color) 屬性 (Attribute)。

自訂屬性

TextBox 控制項的 wmlFormat 屬性 (Attribute) 會傳送至 WML 裝置做為 WML <input> 項目的 Format 屬性 (Property)。這是自訂屬性 (Attribute) 並且與控制項的第一級屬性 (Attribute) 有區別。

注意 wmlFormat 屬性 (Attribute) 的存在會覆寫 WML 格式的 Textbox 控制項 WML numeric 屬性 (Property)。例如,如果 wmlFormat 屬性 (Attribute) 設為 NNN,輸入項目將限制為三個數值字元。如需有效格式的完整清單,請參閱 WML 規格;其可經由無線應用程式通訊協定 (WAP) 論壇 http://www.wapforum.org 取得。

TextBox 控制項的 useRandomID 自訂屬性 (Attribute) 可加以設定而促使控制識別碼 (clientID) 針對安全性目的進行加密。擁有 Password 屬性 (Property) 設為 trueTextbox 控制項會自動加密。

如需詳細資訊,請參閱自訂屬性

內含項目規則

所有衍生自 TextControl 類別之具體類別的內含項目規則都相同,如下列表格所示。下列控制項可以包含 TextBox 控制項。

控制項 註解
System.Web.UI.MobileControls.Form 可以包含任何數目的 TextBox 控制項。
System.Web.UI.MobileControls.Panel 可以包含任何數目的 TextBox 控制項。

TextBox 控制項不可以包含任何其他控制項。

裝置樣板

裝置的特定行為

標籤所藉以呈現的樣式將因裝置而異,但標籤的文字會出現在所有裝置上。

對於 HTML 和 WML,文字方塊都會在其本身的一行呈現為 <input> 標記,其後有 <br> 標記 (分行符號) 跟隨著。

裝置語言 行為描述
HTML Size 屬性 (Property) 決定 TextBox 控制項所呈現的長度。

Title 屬性 (Property) 則會被忽略。

Type 屬性 (Property) 設為 Password 時,文字方塊會以密碼樣式呈現。NumericType 屬性 (Property) 會被忽略。

WML 會顯示 Title 屬性 (Property) 中的文字。

Type 屬性 (Property) 設為 Password 時,產生的 <Input> 標記會有密碼樣式。當 Type 屬性 (Property) 為 Numeric 時,將會有只提供數值欄位之 <Input> 標記的 Format 屬性 (Attribute) 指定給 <Input> 標記。裝置即接著解譯這個格式屬性 (Attribute),就好像輸入模式被切換至數值模式一般。

如需各種樣式屬性 (Attribute) 如何在各種裝置上解譯的詳細資訊,請參閱樣式文件的<樣式繼承>章節和裝置的特定呈現文件。

範例

下列範例在 TextBox 控制項上示範 OnTextChanged 事件的使用。範例包含顯示著標籤、文字方塊和命令的單一表單。當使用者在文字方塊中輸入資料並接著按一下命令按鈕時,資料會經由回傳送回到伺服器。如果 TextBox 控制項中的文字不同於先前的回傳,則會提供訊息指示其已經變更。這個範例也示範 TextBox 控制項的一些可以設定長度限制及密碼項目模式的屬性 (Property)。

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

請參閱

TextBox 類別 | TextBox 類別成員 | 自訂屬性 | 控制項參考