Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / ndp / fx / src / xsp / System / Web / UI / WebControls / CustomValidator.cs / 1 / CustomValidator.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Web.UI.WebControls {
using System.ComponentModel;
using System.Web;
using System.Security.Permissions;
using System.Web.Util;
///
/// Allows custom code to perform
/// validation on the client and/or server.
///
[
DefaultEvent("ServerValidate"),
ToolboxData("<{0}:CustomValidator runat=\"server\" ErrorMessage=\"CustomValidator\">{0}:CustomValidator>")
]
[AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
public class CustomValidator : BaseValidator {
private static readonly object EventServerValidate= new object();
///
/// Gets and sets the custom client Javascript function used
/// for validation.
///
[
WebCategory("Behavior"),
Themeable(false),
DefaultValue(""),
WebSysDescription(SR.CustomValidator_ClientValidationFunction)
]
public string ClientValidationFunction {
get {
object o = ViewState["ClientValidationFunction"];
return((o == null) ? String.Empty : (string)o);
}
set {
ViewState["ClientValidationFunction"] = value;
}
}
[
WebCategory("Behavior"),
Themeable(false),
DefaultValue(false),
WebSysDescription(SR.CustomValidator_ValidateEmptyText),
]
public bool ValidateEmptyText {
get {
object o = ViewState["ValidateEmptyText"];
return((o == null) ? false : (bool)o);
}
set {
ViewState["ValidateEmptyText"] = value;
}
}
///
/// Represents the method that will handle the
/// event of a
/// .
///
[
WebSysDescription(SR.CustomValidator_ServerValidate)
]
public event ServerValidateEventHandler ServerValidate {
add {
Events.AddHandler(EventServerValidate, value);
}
remove {
Events.RemoveHandler(EventServerValidate, value);
}
}
///
///
/// Adds the properties of the control to the
/// output stream for rendering on the client.
///
protected override void AddAttributesToRender(HtmlTextWriter writer) {
base.AddAttributesToRender(writer);
if (RenderUplevel) {
string id = ClientID;
HtmlTextWriter expandoAttributeWriter = (EnableLegacyRendering) ? writer : null;
AddExpandoAttribute(expandoAttributeWriter, id, "evaluationfunction", "CustomValidatorEvaluateIsValid", false);
if (ClientValidationFunction.Length > 0) {
AddExpandoAttribute(expandoAttributeWriter, id, "clientvalidationfunction", ClientValidationFunction);
if (ValidateEmptyText) {
AddExpandoAttribute(expandoAttributeWriter, id, "validateemptytext", "true", false);
}
}
}
}
///
///
/// Checks the properties of the control for valid values.
///
protected override bool ControlPropertiesValid() {
// Need to override the BaseValidator implementation, because for CustomValidator, it is fine
// for the ControlToValidate to be blank.
string controlToValidate = ControlToValidate;
if (controlToValidate.Length > 0) {
// Check that the property points to a valid control. Will throw and exception if not found
CheckControlValidationProperty(controlToValidate, "ControlToValidate");
}
return true;
}
///
///
/// EvaluateIsValid method
///
protected override bool EvaluateIsValid() {
// If no control is specified, we always fire the event. If they have specified a control, we
// only fire the event if the input is non-blank.
string controlValue = String.Empty;
string controlToValidate = ControlToValidate;
if (controlToValidate.Length > 0) {
controlValue = GetControlValidationValue(controlToValidate);
Debug.Assert(controlValue != null, "Should have been caught be property check");
// If the text is empty, we return true. Whitespace is ignored for coordination wiht
// RequiredFieldValidator.
if ((controlValue == null || controlValue.Trim().Length == 0) &&
!ValidateEmptyText) {
return true;
}
}
return OnServerValidate(controlValue);
}
///
/// Raises the
/// event for the .
///
protected virtual bool OnServerValidate(string value) {
ServerValidateEventHandler handler = (ServerValidateEventHandler)Events[EventServerValidate];
ServerValidateEventArgs args = new ServerValidateEventArgs(value, true);
if (handler != null) {
handler(this, args);
return args.IsValid;
}
else {
return true;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Web.UI.WebControls {
using System.ComponentModel;
using System.Web;
using System.Security.Permissions;
using System.Web.Util;
///
/// Allows custom code to perform
/// validation on the client and/or server.
///
[
DefaultEvent("ServerValidate"),
ToolboxData("<{0}:CustomValidator runat=\"server\" ErrorMessage=\"CustomValidator\">{0}:CustomValidator>")
]
[AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
public class CustomValidator : BaseValidator {
private static readonly object EventServerValidate= new object();
///
/// Gets and sets the custom client Javascript function used
/// for validation.
///
[
WebCategory("Behavior"),
Themeable(false),
DefaultValue(""),
WebSysDescription(SR.CustomValidator_ClientValidationFunction)
]
public string ClientValidationFunction {
get {
object o = ViewState["ClientValidationFunction"];
return((o == null) ? String.Empty : (string)o);
}
set {
ViewState["ClientValidationFunction"] = value;
}
}
[
WebCategory("Behavior"),
Themeable(false),
DefaultValue(false),
WebSysDescription(SR.CustomValidator_ValidateEmptyText),
]
public bool ValidateEmptyText {
get {
object o = ViewState["ValidateEmptyText"];
return((o == null) ? false : (bool)o);
}
set {
ViewState["ValidateEmptyText"] = value;
}
}
///
/// Represents the method that will handle the
/// event of a
/// .
///
[
WebSysDescription(SR.CustomValidator_ServerValidate)
]
public event ServerValidateEventHandler ServerValidate {
add {
Events.AddHandler(EventServerValidate, value);
}
remove {
Events.RemoveHandler(EventServerValidate, value);
}
}
///
///
/// Adds the properties of the control to the
/// output stream for rendering on the client.
///
protected override void AddAttributesToRender(HtmlTextWriter writer) {
base.AddAttributesToRender(writer);
if (RenderUplevel) {
string id = ClientID;
HtmlTextWriter expandoAttributeWriter = (EnableLegacyRendering) ? writer : null;
AddExpandoAttribute(expandoAttributeWriter, id, "evaluationfunction", "CustomValidatorEvaluateIsValid", false);
if (ClientValidationFunction.Length > 0) {
AddExpandoAttribute(expandoAttributeWriter, id, "clientvalidationfunction", ClientValidationFunction);
if (ValidateEmptyText) {
AddExpandoAttribute(expandoAttributeWriter, id, "validateemptytext", "true", false);
}
}
}
}
///
///
/// Checks the properties of the control for valid values.
///
protected override bool ControlPropertiesValid() {
// Need to override the BaseValidator implementation, because for CustomValidator, it is fine
// for the ControlToValidate to be blank.
string controlToValidate = ControlToValidate;
if (controlToValidate.Length > 0) {
// Check that the property points to a valid control. Will throw and exception if not found
CheckControlValidationProperty(controlToValidate, "ControlToValidate");
}
return true;
}
///
///
/// EvaluateIsValid method
///
protected override bool EvaluateIsValid() {
// If no control is specified, we always fire the event. If they have specified a control, we
// only fire the event if the input is non-blank.
string controlValue = String.Empty;
string controlToValidate = ControlToValidate;
if (controlToValidate.Length > 0) {
controlValue = GetControlValidationValue(controlToValidate);
Debug.Assert(controlValue != null, "Should have been caught be property check");
// If the text is empty, we return true. Whitespace is ignored for coordination wiht
// RequiredFieldValidator.
if ((controlValue == null || controlValue.Trim().Length == 0) &&
!ValidateEmptyText) {
return true;
}
}
return OnServerValidate(controlValue);
}
///
/// Raises the
/// event for the .
///
protected virtual bool OnServerValidate(string value) {
ServerValidateEventHandler handler = (ServerValidateEventHandler)Events[EventServerValidate];
ServerValidateEventArgs args = new ServerValidateEventArgs(value, true);
if (handler != null) {
handler(this, args);
return args.IsValid;
}
else {
return true;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- nulltextcontainer.cs
- CompositeFontInfo.cs
- URLIdentityPermission.cs
- SettingsAttributeDictionary.cs
- StringAnimationBase.cs
- CqlQuery.cs
- TypeDescriptorFilterService.cs
- ReflectionPermission.cs
- FileLogRecordHeader.cs
- TypedTableGenerator.cs
- SmiContext.cs
- Transform.cs
- FormsAuthentication.cs
- Popup.cs
- ConstraintConverter.cs
- TwoPhaseCommit.cs
- UserPreferenceChangedEventArgs.cs
- EmptyControlCollection.cs
- StrokeNodeOperations2.cs
- EventHandlers.cs
- TabRenderer.cs
- ScriptHandlerFactory.cs
- HostingEnvironmentException.cs
- TableRowCollection.cs
- Part.cs
- DateTimeFormatInfo.cs
- HostingPreferredMapPath.cs
- RadioButton.cs
- CardSpacePolicyElement.cs
- TabPanel.cs
- IdentifierService.cs
- GenericAuthenticationEventArgs.cs
- ReceiveDesigner.xaml.cs
- TableLayoutStyleCollection.cs
- GenericNameHandler.cs
- DependencyProperty.cs
- Transaction.cs
- ConsumerConnectionPoint.cs
- RightsManagementInformation.cs
- ListItemsPage.cs
- TraceSource.cs
- AccessKeyManager.cs
- ExpandSegmentCollection.cs
- ConfigurationStrings.cs
- ToolStripSystemRenderer.cs
- TableLayoutPanelBehavior.cs
- CreatingCookieEventArgs.cs
- ConditionedDesigner.cs
- RepeaterItemEventArgs.cs
- XmlnsCompatibleWithAttribute.cs
- SQLDoubleStorage.cs
- TreeViewHitTestInfo.cs
- RNGCryptoServiceProvider.cs
- IdnMapping.cs
- ItemCheckedEvent.cs
- Selection.cs
- WmpBitmapDecoder.cs
- SHA1CryptoServiceProvider.cs
- EntityModelBuildProvider.cs
- EditingScopeUndoUnit.cs
- MultiTouchSystemGestureLogic.cs
- AmbientEnvironment.cs
- StrongNameKeyPair.cs
- EntityModelBuildProvider.cs
- RelationshipNavigation.cs
- JoinCqlBlock.cs
- ListCardsInFileRequest.cs
- FreeFormDragDropManager.cs
- ListViewItemEventArgs.cs
- DefaultMemberAttribute.cs
- DateTimeUtil.cs
- IntegerValidatorAttribute.cs
- WindowsFormsSectionHandler.cs
- ParameterModifier.cs
- PeerEndPoint.cs
- ParallelDesigner.cs
- TypeConverter.cs
- XmlElementAttributes.cs
- Helper.cs
- ToolStripAdornerWindowService.cs
- ControlBindingsCollection.cs
- CommonObjectSecurity.cs
- odbcmetadatacolumnnames.cs
- DesignerUtility.cs
- uribuilder.cs
- InputScopeNameConverter.cs
- LinqDataSource.cs
- ValuePattern.cs
- ExpressionCopier.cs
- CollectionContainer.cs
- StyleSheetRefUrlEditor.cs
- LeftCellWrapper.cs
- ShutDownListener.cs
- DataGridViewRowDividerDoubleClickEventArgs.cs
- GregorianCalendarHelper.cs
- Int32AnimationBase.cs
- FormViewRow.cs
- AddInDeploymentState.cs
- DateTimeSerializationSection.cs
- KerberosTicketHashIdentifierClause.cs