Requiredfieldvalidator - DotNet and DataBase

Follow us on Facebook

SQL Server Mountain View Mountain View Mountain View

Saturday, 18 August 2018


RequiredFieldValidator: This validation control will be used when a particular control should contain data while inserting into the database. For example, we are working on a user registration form, here username field is mandatory to be filled. In that case instead of validating the data in the code behind using the traditional if condition, RequiredFieldValidator fulfills our need.

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Error Message"        ControlToValidate="ControlID"></asp:RequiredFieldValidator>

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RequiredFieldvalidator.aspx.cs" Inherits="WebProgramming.Validators.RequiredFieldvalidator" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
                      UserName:  <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="rfcUsername" runat="server" ErrorMessage="UserName is required." ControlToValidate="txtUserName"></asp:RequiredFieldValidator></td>
                        <asp:Button ID="btnSubmit" runat="server" Text="Submit"  /></td>


Here rfcUsername is the Required field validator which monitors the value in txtUserName text box.
In case the user not supplied the data to the username field,  it throws the error message as below.

No comments:

Post a comment


Get Updates On

Discussion updates

Straight Into Your INBOX!

Enter your email address to subscribe to this website and receive notifications of new posts by email.