-->

22 December 2019

Asp.Net RequiredFieldValidator Control

  Asp.Net CS By Example       22 December 2019

   RequiredFieldValidator :

 This control is used to validate control.If required control is not contain any value then this control event fire on button click event.


Syntax:
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" Text="" InitialValue="none" 
SetFocusOnError="true" 
 ControlToValidate="dropFavoriteColor" runat="server" />



Properties:
Sr.No Property Description
1) ID Set the uniqe value to control to access in code-behind code.
2) ControlToValidate which control is check or The ID of the form field validated.
3) Text The error message displayed when validation fails.
4) SetFocusOnError Set focus to associated control.
5) InitialValue set the intial value of control.
6) ValidationGroup set the group of validation.
7) EnableClientScript To enable validate on client side.
8) CausesValidation Is used to disable validation on secefic action e.g. Reset button click. set false to disable validation.

##### Properties: ######
1) Display Property:
All the validation controls include a Display property that determines how the validation
error message is rendered. This property accepts any of the following three possible values:
. Static
. Dynamic
. None

2) Validation Groups:
If We added more than one form to a page, and both forms contained
validation controls, the validation controls in both forms were evaluated regardless of
which form we submitted. To specify the control is of which form use 'ValidationGroup' property.

3) Disabling Validation:
All the button controls—Button, LinkButton, and ImageButton—include a
'CausesValidation' property. If we assign the value False to this property, clicking the
button bypasses any validation in the page.

4) To Set Intial Value of Control:
'InitialValue' property The value of the control is assigned to this property

Code: RequiredValidatorDemo.aspx

<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="RequiredValidatorDemo.aspx.cs" 
Inherits="WebMSV.Programming.RequiredValidatorDemo" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
     <style>
        .column
        {
            float: left;
            width: 30%;
            margin-left: 10px;
            background-color: white;
            border: solid 1px black;
            padding: 10px;
            border-radius: 25px;
        }
        fieldset
        {
            background-color: #a9a9a9;
            border: solid 2px #d81a72;
            border-radius: 20px;
            color:Black;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div class="column">
        
            <fieldset>
                <legend>Required Validator Demo</legend>
                <asp:Label ID="lblName" Width="100" AssociatedControlID="txtName" runat="server"> 
        Name:</asp:Label>
                <asp:TextBox ID="txtName" MaxLength="20" runat="server" Width="100"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvName" ControlToValidate="txtName" Text="Enter Name" 
BackColor="#2be274" ForeColor="#d8341a" EnableClientScript="true"
                    SetFocusOnError="true" runat="server"></asp:RequiredFieldValidator>
                <br />
                <br />
                <asp:Button runat="server" ID="btnValidate" Text="Validate" />
            </fieldset>
            </div>
    </form>
</body>
</html>

Output:

logoblog

Thanks for reading Asp.Net RequiredFieldValidator Control

Previous
« Prev Post