ASP.NET Mobile RequiredFieldValidator Control[转]

Definition and Usage
定义和用法

The RequiredFieldValidator control is used to make an input control a required field.
RequiredFieldValidator控件的作用是:使输入控件变成一个必要的输入域。

With this control, the validation fails if the input value does not change from its initial value. By default, the initial value is null.
通过该控件,如果输入值并为将原始值改变,那么验证失败。默认情况下,原始值都是空值。

Note: Leading and trailing spaces of the input value are removed before validation.
注意:在验证之前,输入值的首尾空格都将被删除。

Note: The InitialValue property does not set the default value for the input control. It indicates the value that you do not want the user to enter in the input control.
注意:InitialValue属性并不会为输入控件设置默认值。它将会显示你不希望用户在输入控件中输入的值。


Properties
属性

Property
属性
Value
Description
描述
Alignment left
center
right
Optional. How to align the control
可选参数。指定控件的排列方式
BackColor rgb(x,x,x)
#xxxxxx
colorname
Optional. A background color for the control
可选参数。指定控件的背景颜色
ControlToValidate id_of_target_control Optional. The id of the control to validate
可选参数。指定参与验证的控件id
Display None
Static
Dynamic
Optional. The display behavior for the validation control. Legal values are:
可选参数。指定确认控件的显示方式。合法值如下: 
  • None (the control is not displayed. Used to show the error message only in the ValidationSummary control)
    无效果。(控件不显示。仅显示ValidationSummary 控件的错误信息)
     
  • Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation
    静态效果(如果验证失败,则控件显示错误信息。如果输入控件有效,也会为信息保留页面空间)
     
  • Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation
    动态效果(如果验证失败,则控件显示错误信息。即使输入控件有效,也不会为信息保留页面空间)

Dynamic is default
默认为动态样式

ErrorMessage errortext_for_summary Optional. The text to display in the ValidationSummary control when validation fails
可选参数。当验证失败时,将在ValidationSummary 控件中显示文本信息
ForeColor rgb(x,x,x)
#xxxxxx
colorname
Optional. A foreground color for the control
可选参数。指定控件的前景色
Font-Bold false
true
Optional. Specifies whether or not the text in the control should be bold
可选参数。指定控件中的文本字体是否以粗体显示
Font-Italic false
true
Optional. Specifies whether or not the text in the control should be italic
可选参数。指定控件中的文本字体是否以斜体显示
Font-Name fontname Optional. Specifies the font name of the text in the control
可选参数。指定空间中的文本字体名称
Font-Size normal
small
large
Optional. Specifies the font size of the text in the control
可选参数。指定空间中的文本字体尺寸
id unique_name Optional. A unique id for the control
可选参数。为空间指定一个独立的id
InitialValue initial_value Optional. Specifies the starting value of the input control. Default value is null
 
runat "server" Required. Specifies that the control is a server control
必要参数。指定该控件为服务器控件
StyleReference name_of_style_element Optional. Specifies a reference to a style to be applied to the control
Text errortext Optional. The message to display when validation fails
可选参数。当验证失败时,指定显示的信息
Wrapping wrap
nowrap
Optional. Specifies whether or not the text in the control should wrap
可选参数。指定控件中的文本是否允许被嵌套

Example 1
案例1

The following example contains two forms in an .aspx file. The first form has a label with the text "Please enter a number from 1 through 100", an input box to input a number, a RangeValidator control that checks that the input value is a number from 1 through 100, a RequiredFieldValidator control that checks that the input field isn't empty, and a submit button. The second page is activated by the submit button on the first page, and displays a response. If the input value validates as an error, an error message is displayed:
下面的案例包含了 .aspx 文件中的两张表单。第一张表单包含一个名为“Please enter a number from 1 through 100” 的标签,一个输入框,一个用于确认输入值范围从数字1-100的RangeValidator 控件,以及一个提交按钮。第二张页面将被第一张页面中的提交按钮激活。如果输入值确认出现错误,那么将显示一段错误信息:

<%@ Page 
Inherits="System.Web.UI.MobileControls.MobilePage"%>
<%@ Register TagPrefix="Mobile"
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<script runat="server">
sub page2(Sender as Object,E as EventArgs)
if Page.IsValid then
ActiveForm=f2
lbl2.Text="You entered number " & txt1.text
end if
end sub
</script>

<Mobile:Form id="f1" runat="server">

<Mobile:Label runat="server">
Please enter a number from 1 through 100
</Mobile:Label>
  <Mobile:TextBox id="txt1" runat="server"/>
  <Mobile:RangeValidator
ControlToValidate="txt1"
Type="Integer"
MaximumValue="100"
MinimumValue="1"

Text="Invalid number"
runat="server" />
  <Mobile:RequiredFieldValidator
ControlToValidate="txt1"
Text="A number is required"

runat="server" />
  <Mobile:Command runat="server"
OnClick="page2">Submit
</Mobile:Command>

</Mobile:Form>
<Mobile:Form id="f2" runat="server">
<Mobile:Label id="lbl2" runat="server" />

</Mobile:Form>

posted on 2009-03-17 17:20  黄裳  阅读(221)  评论(0)    收藏  举报

导航