Created
June 11, 2014 08:41
-
-
Save Konard/5699aa0ad9f8327547f8 to your computer and use it in GitHub Desktop.
RequiredIfCheckedFieldValidator расширения веб-контрола RequiredFieldValidator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ComponentModel; | |
using System.Web.UI; | |
using System.Web.UI.WebControls; | |
namespace Avalast.Web.UI.WebControls | |
{ | |
public class RequiredIfCheckedFieldValidator : RequiredFieldValidator | |
{ | |
/// <summary> | |
/// CheckBox на основе значения которого включается и выключается логика проверки валидатора. | |
/// </summary> | |
[DefaultValue("")] | |
[IDReferenceProperty] | |
[Themeable(false)] | |
[TypeConverter(typeof(ValidatedControlConverter))] | |
public string CheckBoxToCheck | |
{ | |
get | |
{ | |
var value = ViewState["5BBE8A83-926F-4CA8-9281-65F6628620A6"]; | |
return ((value == null) ? String.Empty : (string)value); | |
} | |
set | |
{ | |
ViewState["5BBE8A83-926F-4CA8-9281-65F6628620A6"] = value; | |
} | |
} | |
protected override bool EvaluateIsValid() | |
{ | |
if (string.IsNullOrWhiteSpace(CheckBoxToCheck)) | |
return base.EvaluateIsValid(); | |
var checkBox = FindControl(CheckBoxToCheck) as CheckBox; | |
if (checkBox == null) | |
throw new Exception(string.Format("CheckBox с указанным ID {0} не найден.", CheckBoxToCheck)); | |
return !checkBox.Checked || base.EvaluateIsValid(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment