This documentation is archived and is not being maintained.
Required Field Validator Control Sample
.NET Framework 1.1
The required field validator control described here extends the base validator control described in the Base Validator Control Sample. This validator adds the following functionality to the base class:
- It exposes a property named
InitialValue
that allows the page developer to set an initial value for the required field. - It overrides the
EvaluateIsValid
method (defined as an abstract method inBaseDomValidator
) to provide logic to determine whether the value of the required field has changed when the user moves to another field. - It overrides AddAttributesToRender (inherited from WebControl) to provide a client-side handler for the evaluation logic. The client-side handler is a function defined in the script library.
To compile and build this sample, see the instructions in Validator Control Samples. For an overview of client-side scripting in server controls, see Client-Side Functionality in a Server Control.
// RequiredDomValidator.cs. namespace DomValidators { using System.ComponentModel; using System.Diagnostics; using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; [ ToolboxData("<{0}:RequiredDomValidator runat=server ErrorMessage=\"RequiredDomValidator\"></{0}:RequiredDomValidator>") ] public class RequiredDomValidator : BaseDomValidator { [ Bindable(true), Category("Behavior"), DefaultValue(""), Description("Initial Value") ] public string InitialValue { get { object o = ViewState["InitialValue"]; return((o == null) ? String.Empty : (string)o); } set { ViewState["InitialValue"] = value; } } protected override void AddAttributesToRender(HtmlTextWriter writer) { base.AddAttributesToRender(writer); if (RenderUplevel) { writer.AddAttribute("evaluationfunction", "RequiredFieldValidatorEvaluateIsValid"); writer.AddAttribute("initialvalue", InitialValue); } } protected override bool EvaluateIsValid() { // Get the control value; return true if it is not found. string controlValue = GetControlValidationValue(ControlToValidate); if (controlValue == null) { Debug.Fail("Should have been caught by PropertiesValid check."); return true; } // See if the control has changed. return(!controlValue.Trim().Equals(InitialValue.Trim())); } } } [Visual Basic] ' RequiredDomValidator.vb Option Explicit Option Strict Imports System.ComponentModel Imports System.Diagnostics Imports System Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Namespace DomValidators <ToolboxData("<{0}:RequiredDomValidator runat=server ErrorMessage=""RequiredDomValidator""></{0}:RequiredDomValidator>")> _ Public Class RequiredDomValidator Inherits BaseDomValidator <Bindable(True), _ Category("Behavior"), _ DefaultValue(""), _ Description("Initial Value")> _ Public Property InitialValue() As String Get Dim o As Object = ViewState("InitialValue") If o Is Nothing Then Return String.Empty Else Return CStr(o) End If End Get Set ViewState("InitialValue") = value End Set End Property Protected Overrides Sub AddAttributesToRender(writer As HtmlTextWriter) MyBase.AddAttributesToRender(writer) If MyBase.RenderUplevel Then writer.AddAttribute("evaluationfunction", "RequiredFieldValidatorEvaluateIsValid") writer.AddAttribute("initialvalue", InitialValue) End If End Sub Protected Overrides Function EvaluateIsValid() As Boolean ' Get the control value; return true if it is not found. Dim controlValue As String = GetControlValidationValue(MyBase.ControlToValidate) If controlValue Is Nothing Then Debug.Fail("Should have been caught by PropertiesValid check.") Return True End If ' See if the control has changed. Return Not controlValue.Trim().Equals(InitialValue.Trim()) End Function End Class End Namespace
See Also
Validator Control Samples | Base Validator Control Sample | Regular Expression Validator Control Sample | Script Library for Validator Sample | Configuration File for Validator Sample | Test Page for Validator Sample | Developing a Validator Control
Show: