1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
|
//
// System.Web.UI.WebControls.BaseValidator
//
// Authors:
// Chris Toshok (toshok@novell.com)
//
// (C) 2005-2010 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Web.Configuration;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Collections;
using System.Security.Permissions;
namespace System.Web.UI.WebControls
{
// CAS
[AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
// attributes
[DefaultProperty("ErrorMessage")]
[Designer("System.Web.UI.Design.WebControls.BaseValidatorDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
public abstract class BaseValidator : Label, IValidator
{
bool render_uplevel;
bool valid;
Color forecolor;
bool pre_render_called = false;
protected BaseValidator ()
{
this.valid = true;
this.ForeColor = Color.Red;
}
// New in NET1.1 sp1
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public override string AssociatedControlID {
get { return base.AssociatedControlID; }
set { base.AssociatedControlID = value; }
}
[Themeable (false)]
[DefaultValue ("")]
public virtual string ValidationGroup {
get { return ViewState.GetString ("ValidationGroup", String.Empty); }
set { ViewState["ValidationGroup"] = value; }
}
[Themeable (false)]
[DefaultValue (false)]
public bool SetFocusOnError {
get { return ViewState.GetBool ("SetFocusOnError", false); }
set { ViewState["SetFocusOnError"] = value; }
}
/* listed in corcompare */
[MonoTODO("Why override?")]
[PersistenceMode (PersistenceMode.InnerDefaultProperty)]
[DefaultValue ("")]
public override string Text
{
get { return base.Text; }
set { base.Text = value; }
}
[IDReferenceProperty (typeof (Control))]
[Themeable (false)]
[TypeConverter(typeof(System.Web.UI.WebControls.ValidatedControlConverter))]
[DefaultValue("")]
[WebSysDescription ("")]
[WebCategory ("Behavior")]
public string ControlToValidate {
get { return ViewState.GetString ("ControlToValidate", String.Empty); }
set { ViewState ["ControlToValidate"] = value; }
}
[Themeable (false)]
[DefaultValue(ValidatorDisplay.Static)]
[WebSysDescription ("")]
[WebCategory ("Appearance")]
public ValidatorDisplay Display {
get { return (ValidatorDisplay)ViewState.GetInt ("Display", (int)ValidatorDisplay.Static); }
set { ViewState ["Display"] = (int)value; }
}
[Themeable (false)]
[DefaultValue(true)]
[WebSysDescription ("")]
[WebCategory ("Behavior")]
public bool EnableClientScript {
get { return ViewState.GetBool ("EnableClientScript", true); }
set { ViewState ["EnableClientScript"] = value; }
}
public override bool Enabled {
get { return ViewState.GetBool ("BaseValidatorEnabled", true); }
set { ViewState ["BaseValidatorEnabled"] = value; }
}
[Localizable (true)]
[DefaultValue("")]
[WebSysDescription ("")]
[WebCategory ("Appearance")]
public string ErrorMessage {
get { return ViewState.GetString ("ErrorMessage", String.Empty); }
set { ViewState ["ErrorMessage"] = value; }
}
[DefaultValue(typeof (Color), "Red")]
public override Color ForeColor {
get { return forecolor; }
set {
forecolor = value;
base.ForeColor = value;
}
}
[Browsable(false)]
[DefaultValue(true)]
[Themeable (false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[WebSysDescription ("")]
[WebCategory ("Misc")]
public bool IsValid {
get { return valid; }
set { valid = value; }
}
protected bool PropertiesValid {
get {
Control control = NamingContainer.FindControl (ControlToValidate);
if (control == null)
return false;
else
return true;
}
}
protected bool RenderUplevel {
get { return render_uplevel; }
}
internal bool GetRenderUplevel ()
{
return render_uplevel;
}
protected override void AddAttributesToRender (HtmlTextWriter writer)
{
/* if we're rendering uplevel, add our attributes */
if (render_uplevel) {
/* force an ID here if we weren't assigned one */
if (ID == null)
writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
if (ControlToValidate != String.Empty)
RegisterExpandoAttribute (ClientID, "controltovalidate", GetControlRenderID (ControlToValidate));
if (ErrorMessage != String.Empty)
RegisterExpandoAttribute (ClientID, "errormessage", ErrorMessage, true);
if (ValidationGroup != String.Empty)
RegisterExpandoAttribute (ClientID, "validationGroup", ValidationGroup, true);
if (SetFocusOnError)
RegisterExpandoAttribute (ClientID, "focusOnError", "t");
bool enabled = IsEnabled;
if (!enabled)
RegisterExpandoAttribute (ClientID, "enabled", "False");
if (enabled && !IsValid)
RegisterExpandoAttribute (ClientID, "isvalid", "False");
else {
if (Display == ValidatorDisplay.Static)
writer.AddStyleAttribute ("visibility", "hidden");
else
writer.AddStyleAttribute ("display", "none");
}
if (Display != ValidatorDisplay.Static)
RegisterExpandoAttribute (ClientID, "display", Display.ToString ());
}
base.AddAttributesToRender (writer);
}
internal void RegisterExpandoAttribute (string controlId, string attributeName, string attributeValue)
{
RegisterExpandoAttribute (controlId, attributeName, attributeValue, false);
}
internal void RegisterExpandoAttribute (string controlId, string attributeName, string attributeValue, bool encode)
{
Page page = Page;
if (page.ScriptManager != null)
page.ScriptManager.RegisterExpandoAttributeExternal (this, controlId, attributeName, attributeValue, encode);
else
page.ClientScript.RegisterExpandoAttribute (controlId, attributeName, attributeValue, encode);
}
protected void CheckControlValidationProperty (string name, string propertyName)
{
Control control = NamingContainer.FindControl (name);
PropertyDescriptor prop = null;
if (control == null)
throw new HttpException (String.Format ("Unable to find control id '{0}'.", name));
prop = BaseValidator.GetValidationProperty (control);
if (prop == null)
throw new HttpException (String.Format ("Unable to find ValidationProperty attribute '{0}' on control '{1}'", propertyName, name));
}
protected virtual bool ControlPropertiesValid ()
{
if (ControlToValidate.Length == 0) {
throw new HttpException (String.Format ("ControlToValidate property of '{0}' cannot be blank.", ID));
}
CheckControlValidationProperty (ControlToValidate, String.Empty);
return true;
}
protected virtual bool DetermineRenderUplevel ()
{
if (!EnableClientScript)
return false;
return UplevelHelper.IsUplevel (
System.Web.Configuration.HttpCapabilitiesBase.GetUserAgentForDetection (HttpContext.Current.Request));
}
protected abstract bool EvaluateIsValid ();
protected string GetControlRenderID (string name)
{
Control control = NamingContainer.FindControl (name);
if (control == null)
return null;
return control.ClientID;
}
protected string GetControlValidationValue (string name)
{
Control control = NamingContainer.FindControl (name);
if (control == null)
return null;
PropertyDescriptor prop = BaseValidator.GetValidationProperty (control);
if (prop == null)
return null;
object o = prop.GetValue (control);
if (o == null)
return String.Empty;
if (o is ListItem)
return ((ListItem) o).Value;
return o.ToString ();
}
public static PropertyDescriptor GetValidationProperty (object component)
{
PropertyDescriptorCollection props;
System.ComponentModel.AttributeCollection col;
props = TypeDescriptor.GetProperties (component);
col = TypeDescriptor.GetAttributes (component);
foreach (Attribute at in col) {
ValidationPropertyAttribute vpa = at as ValidationPropertyAttribute;
if (vpa != null && vpa.Name != null)
return props[vpa.Name];
}
return null;
}
protected internal override void OnInit (EventArgs e)
{
Page page = Page;
/* according to an msdn article, this is done here */
if (page != null) {
page.Validators.Add (this);
page.GetValidators (ValidationGroup).Add (this);
}
base.OnInit (e);
}
protected internal override void OnPreRender (EventArgs e)
{
base.OnPreRender (e);
pre_render_called = true;
ControlPropertiesValid ();
render_uplevel = DetermineRenderUplevel ();
if (render_uplevel)
RegisterValidatorCommonScript ();
}
protected internal override void OnUnload (EventArgs e)
{
Page page = Page;
/* according to an msdn article, this is done here */
if (page != null) {
page.Validators.Remove (this);
string validationGroup = ValidationGroup;
if (!String.IsNullOrEmpty (validationGroup))
page.GetValidators (ValidationGroup).Remove (this);
}
base.OnUnload (e);
}
protected void RegisterValidatorCommonScript ()
{
Page page = Page;
if (page != null) {
if (page.ScriptManager != null) {
page.ScriptManager.RegisterClientScriptResourceExternal (this, typeof (BaseValidator), "WebUIValidation_2.0.js");
page.ScriptManager.RegisterClientScriptBlockExternal (this, typeof (BaseValidator), "ValidationInitializeScript", page.ValidationInitializeScript, true);
page.ScriptManager.RegisterOnSubmitStatementExternal (this, typeof (BaseValidator), "ValidationOnSubmitStatement", page.ValidationOnSubmitStatement);
page.ScriptManager.RegisterStartupScriptExternal (this, typeof (BaseValidator), "ValidationStartupScript", page.ValidationStartupScript, true);
} else if (!page.ClientScript.IsClientScriptIncludeRegistered (typeof (BaseValidator), "Mono-System.Web-ValidationClientScriptBlock")) {
page.ClientScript.RegisterClientScriptInclude (typeof (BaseValidator), "Mono-System.Web-ValidationClientScriptBlock",
page.ClientScript.GetWebResourceUrl (typeof (BaseValidator), "WebUIValidation_2.0.js"));
page.ClientScript.RegisterClientScriptBlock (typeof (BaseValidator), "Mono-System.Web-ValidationClientScriptBlock.Initialize", page.ValidationInitializeScript, true);
page.ClientScript.RegisterOnSubmitStatement (typeof (BaseValidator), "Mono-System.Web-ValidationOnSubmitStatement", page.ValidationOnSubmitStatement);
page.ClientScript.RegisterStartupScript (typeof (BaseValidator), "Mono-System.Web-ValidationStartupScript", page.ValidationStartupScript, true);
}
}
}
protected virtual void RegisterValidatorDeclaration ()
{
Page page = Page;
if (page != null) {
if (page.ScriptManager != null) {
page.ScriptManager.RegisterArrayDeclarationExternal (this, "Page_Validators", String.Concat ("document.getElementById ('", ClientID, "')"));
page.ScriptManager.RegisterStartupScriptExternal (this, typeof (BaseValidator), ClientID + "DisposeScript",
@"
document.getElementById('" + ClientID + @"').dispose = function() {
Array.remove(Page_Validators, document.getElementById('" + ClientID + @"'));
}
", true);
} else
page.ClientScript.RegisterArrayDeclaration ("Page_Validators", String.Concat ("document.getElementById ('", ClientID, "')"));
}
}
protected internal override void Render (HtmlTextWriter writer)
{
if (!IsEnabled && !EnableClientScript)
return;
if (render_uplevel) {
/* according to an msdn article, this is done here */
RegisterValidatorDeclaration ();
}
bool render_tags = false;
bool render_text = false;
bool render_nbsp = false;
bool v = IsValid;
if (!pre_render_called) {
render_tags = true;
render_text = true;
} else if (render_uplevel) {
render_tags = true;
render_text = Display != ValidatorDisplay.None;
} else {
if (Display != ValidatorDisplay.None) {
render_tags = !v;
render_text = !v;
render_nbsp = v && Display == ValidatorDisplay.Static;
}
}
if (render_tags) {
AddAttributesToRender (writer);
writer.RenderBeginTag (HtmlTextWriterTag.Span);
}
if (render_text || render_nbsp) {
string text;
if (render_text) {
text = Text;
if (String.IsNullOrEmpty (text))
text = ErrorMessage;
} else
text = " ";
writer.Write (text);
}
if (render_tags)
writer.RenderEndTag ();
}
/* the docs say "public sealed" here */
public void Validate ()
{
if (IsEnabled && Visible)
IsValid = ControlPropertiesValid () && EvaluateIsValid ();
else
IsValid = true;
}
}
}
|