服務(wù)熱線
153 8323 9821
驗證控件
移動WEB程序中的驗證控件和傳統(tǒng)的ASP.NET程序中的驗證控件具有類似的工作方式。例如RequiredFieldValidator控件確保用戶必須輸入信息, CompareValidator控件用來比較兩個字段,RangeValidator確保整個字段都必 須在一個特定的范圍之內(nèi),RegularExpressionValidator控件用正則表達(dá)式來驗證信息,而CustomValidator控件利用用戶自定義代碼進(jìn)行信息的驗證。
當(dāng)然,移動WEB程序中的驗證控件和傳統(tǒng)的ASP.NET程序中的驗證控件還是有些細(xì)小的區(qū)別。例如ValidationSummary控件不支持DisplayMode、EnableClientScript、ShowMessageBox、ShowSummary等屬性。
但是在移動WEB程序中,ValidationSummary控件添加了BackLabel和 FormToValidate兩個屬性。因為移動設(shè)備的屏幕限制,所以ValidationSummary控件通常將驗證信息在一個新的窗體中進(jìn)顯示。當(dāng)在呈現(xiàn)窗體的過程中發(fā)生錯誤時,驗證過程將BackLabel屬性中的文本用作驗證摘要頁上顯示的字符串。而使用 FormToValidate屬性將一個窗體指定為待驗證的窗體。
下面的程序包含一個文本框和兩個驗證控件。RequiredFieldValidator控件確保文本框中的信息不為空,而RegularExpressionValidator控件確保輸入的值為一個北京的電話號碼。
清單14: ValidatorDefault.aspx
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="ValidatorDefault.aspx.cs" Inherits="ValidatorDefault" %>
<%@ Register TagPrefix="mobile"
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<mobile:Form id="Form1" runat="server">
<mobile:Label ID="lblPhoneNumber" Runat="server">
輸入電話號碼
:</mobile:Label>
<mobile:TextBox ID="txtPhoneNumber"
Runat="server"></mobile:TextBox>
<mobile:RequiredFieldValidator ID="rfvPhone"
Runat="server" ControlToValidate="txtPhoneNumber"
ErrorMessage="
電話號碼必須輸入">×
</mobile:RequiredFieldValidator>
<mobile:RegularExpressionValidator ID="revPhone"
Runat="server" ControlToValidate="txtPhoneNumber"
ErrorMessage="
錯誤的電話格式
"
ValidationExpression="(\(\d{3}\)|\d{3}-)?\d{8}">×
</mobile:RegularExpressionValidator>
<mobile:Command ID="cmdPhoneNumber"
Runat="server">OK</mobile:Command>
<mobile:ValidationSummary ID="ValidationSummary1" Runat="server"
BackLabel="返回重新輸入" FormToValidate="Form1">
</mobile:ValidationSummary>
</mobile:Form>
</body>
</html>