In this article I will explain what RequiredFieldValidator
in ASP.NET is and what are the syntax and properties.
The RequiredFieldValidator control is used to make
an input control a required field. This Validator control is ASP.NET control
The RequiredFieldValidator is
actually very simple, and yet very useful. You can use it to make sure that the
user has entered something in a TextBox control. Let's give it a try, and add a
RequiredFieldValidator to our page. We will also add a TextBox to validate, as
well as a button to submit the form with.
When you use the RequiredFieldValidator
control inside an UpdatePanel control, make sure that the validator control and
the control it is associated with are in the same panel.
Syntax:
<asp:RequiredFieldValidator />
Main Properties:
o
ControlToValidate:
Gets or sets the input control to validate.
o
Display:
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)
- 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.
o
ErrorMessage:
Gets or sets the text for the error message displayed in a ValidationSummary control
when validation fails.
o
Text:
Gets or sets the text displayed in the validation control when validation
fails.
o
ValidationGroup:
Gets or sets the name of the validation group to which this validation control
belongs.
o
InitialValue:
Specifies the starting value of the input control. Default value is
""
o Runat:
Specifies that the control is a server control. Must be set to
"server"
Example 1: Below example is for
DropDownList
<asp:DropDownList ID='ddlCompTypeFT' runat="Server" Width="150px" AutoPostBack="true" > </asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvCompTypeFT" runat="server" InitialValue="0"
Display="Dynamic" ForeColor="Red" ControlToValidate="ddlCompTypeFT" ValidationGroup="Add">*</asp:RequiredFieldValidator>
Example 2: Below
example is for TextBox
<asp:TextBox ID='txtCoefficientET' runat="Server" Text='<%# Eval("Coefficient")%>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RFVCoefficientET" runat="server"
Display="Dynamic" ForeColor="Red" ControlToValidate="txtCoefficientET" ValidationGroup="UPDATE">*</asp:RequiredFieldValidator>
No comments:
Post a Comment