// Authors:
// Chris Toshok (toshok@novell.com)
//
-// (C) 2005 Novell, Inc (http://www.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
using System.Collections;
using System.Security.Permissions;
-namespace System.Web.UI.WebControls {
-
+namespace System.Web.UI.WebControls
+{
// CAS
[AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
bool render_uplevel;
bool valid;
Color forecolor;
+ bool pre_render_called = false;
protected BaseValidator ()
{
// New in NET1.1 sp1
[Browsable(false)]
-#if ONLY_1_1
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
-#endif
[EditorBrowsable(EditorBrowsableState.Never)]
public override string AssociatedControlID {
- get {
- return base.AssociatedControlID;
- }
- set {
- base.AssociatedControlID = value;
- }
+ get { return base.AssociatedControlID; }
+ set { base.AssociatedControlID = value; }
}
-#if NET_2_0
[Themeable (false)]
[DefaultValue ("")]
public virtual string ValidationGroup {
get { return base.Text; }
set { base.Text = value; }
}
-#endif
-#if NET_2_0
[IDReferenceProperty (typeof (Control))]
[Themeable (false)]
-#endif
[TypeConverter(typeof(System.Web.UI.WebControls.ValidatedControlConverter))]
[DefaultValue("")]
[WebSysDescription ("")]
set { ViewState ["ControlToValidate"] = value; }
}
-#if NET_2_0
[Themeable (false)]
-#endif
-#if ONLY_1_1
- [Bindable(true)]
-#endif
[DefaultValue(ValidatorDisplay.Static)]
[WebSysDescription ("")]
[WebCategory ("Appearance")]
set { ViewState ["Display"] = (int)value; }
}
-#if NET_2_0
[Themeable (false)]
-#endif
[DefaultValue(true)]
[WebSysDescription ("")]
[WebCategory ("Behavior")]
}
public override bool Enabled {
- get { return ViewState.GetBool ("Enabled", true); }
- set { ViewState ["Enabled"] = value; }
+ get { return ViewState.GetBool ("BaseValidatorEnabled", true); }
+ set { ViewState ["BaseValidatorEnabled"] = value; }
}
-#if NET_2_0
[Localizable (true)]
-#endif
-#if ONLY_1_1
- [Bindable(true)]
-#endif
[DefaultValue("")]
[WebSysDescription ("")]
[WebCategory ("Appearance")]
-#if NET_2_0
- public
-#else
- public virtual
-#endif
- string ErrorMessage {
+ public string ErrorMessage {
get { return ViewState.GetString ("ErrorMessage", String.Empty); }
set { ViewState ["ErrorMessage"] = value; }
}
[Browsable(false)]
[DefaultValue(true)]
-#if NET_2_0
[Themeable (false)]
-#endif
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[WebSysDescription ("")]
[WebCategory ("Misc")]
-#if NET_2_0
- public
-#else
- public virtual
-#endif
- bool IsValid {
+ public bool IsValid {
get { return valid; }
set { valid = value; }
}
return render_uplevel;
}
- [MonoTODO()]
- // for 2.0: not XHTML attributes must be registered with RegisterExpandoAttribute
- // when it will be implemented, in this case WebUIValidation_2.0.js muist be refactored
protected override void AddAttributesToRender (HtmlTextWriter writer)
{
/* if we're rendering uplevel, add our attributes */
writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
if (ControlToValidate != String.Empty)
- writer.AddAttribute ("controltovalidate", GetControlRenderID (ControlToValidate));
+ RegisterExpandoAttribute (ClientID, "controltovalidate", GetControlRenderID (ControlToValidate));
if (ErrorMessage != String.Empty)
- writer.AddAttribute ("errormessage", ErrorMessage);
- if (Text != String.Empty)
- writer.AddAttribute ("text", Text);
-#if NET_2_0
+ RegisterExpandoAttribute (ClientID, "errormessage", ErrorMessage, true);
+
if (ValidationGroup != String.Empty)
- writer.AddAttribute ("validationgroup", ValidationGroup);
+ RegisterExpandoAttribute (ClientID, "validationGroup", ValidationGroup, true);
if (SetFocusOnError)
- Page.ClientScript.RegisterExpandoAttribute (ClientID, "focusOnError", "t");
-#endif
- if (!Enabled)
- writer.AddAttribute ("enabled", "false");
-
-#if NET_2_0
- if (Enabled && !IsValid) {
-#else
- if (!IsValid) {
-#endif
- writer.AddAttribute ("isvalid", "false");
- }
+ 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");
}
if (Display != ValidatorDisplay.Static)
- writer.AddAttribute ("display", Display.ToString());
+ 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);
protected virtual bool ControlPropertiesValid ()
{
if (ControlToValidate.Length == 0) {
- throw new HttpException("ControlToValidate property cannot be emtpy");
+ throw new HttpException (String.Format ("ControlToValidate property of '{0}' cannot be blank.", ID));
}
- CheckControlValidationProperty (ControlToValidate, "");
+ CheckControlValidationProperty (ControlToValidate, String.Empty);
return true;
}
{
if (!EnableClientScript)
return false;
-
- try {
- if (Page == null || Page.Request == null)
- return false;
- }
- catch {
- /* this can happen with a fake Page in nunit
- * tests, since Page.Context == null */
- return false;
- }
-
- return (
- /* From someplace on the web: "JavaScript 1.2
- * and later (also known as ECMAScript) has
- * built-in support for regular
- * expressions" */
- ((Page.Request.Browser.EcmaScriptVersion.Major == 1
- && Page.Request.Browser.EcmaScriptVersion.Minor >= 2)
- || (Page.Request.Browser.EcmaScriptVersion.Major > 1))
-
- /* document.getElementById, .getAttribute,
- * etc, are all DOM level 1. I don't think we
- * use anything in level 2.. */
- && Page.Request.Browser.W3CDomVersion.Major >= 1);
+ return UplevelHelper.IsUplevel (
+ System.Web.Configuration.HttpCapabilitiesBase.GetUserAgentForDetection (HttpContext.Current.Request));
}
protected abstract bool EvaluateIsValid ();
return null;
object o = prop.GetValue (control);
- return o != null ? o.ToString () : String.Empty;
+
+ if (o == null)
+ return String.Empty;
+
+ if (o is ListItem)
+ return ((ListItem) o).Value;
+
+ return o.ToString ();
}
public static PropertyDescriptor GetValidationProperty (object o)
return null;
}
-#if NET_2_0
- protected internal
-#else
- protected
-#endif
- override void OnInit (EventArgs e)
+ 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);
-
-#if NET_2_0
- Page.GetValidators (ValidationGroup).Add (this);
-#endif
+ if (page != null) {
+ page.Validators.Add (this);
+ page.GetValidators (ValidationGroup).Add (this);
}
base.OnInit (e);
}
- bool pre_render_called = false;
-
-#if NET_2_0
- protected internal
-#else
- protected
-#endif
- override void OnPreRender (EventArgs e)
+ protected internal override void OnPreRender (EventArgs e)
{
base.OnPreRender (e);
pre_render_called = true;
+
+ ControlPropertiesValid ();
render_uplevel = DetermineRenderUplevel ();
- if (render_uplevel) {
+ if (render_uplevel)
RegisterValidatorCommonScript ();
-
- Page.ClientScript.RegisterOnSubmitStatement ("Mono-System.Web-ValidationOnSubmitStatement",
- "if (!ValidatorOnSubmit()) return false;");
- Page.ClientScript.RegisterStartupScript ("Mono-System.Web-ValidationStartupScript",
- "<script language=\"JavaScript\">\n" +
- "<!--\n" +
- "var Page_ValidationActive = false;\n" +
- "ValidatorOnLoad();\n" +
- "\n" +
- "function ValidatorOnSubmit() {\n" +
- " if (Page_ValidationActive) {\n" +
- " return ValidatorCommonOnSubmit();\n" +
- " }\n" +
- " return true;\n" +
- "}\n" +
- "// -->\n" +
- "</script>\n");
- }
}
-#if NET_2_0
- protected internal
-#else
- protected
-#endif
- override void OnUnload (EventArgs e)
+ 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);
-
-#if NET_2_0
- if (ValidationGroup != "")
- Page.GetValidators (ValidationGroup).Remove (this);
-#endif
-
+ 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 ()
{
- if (!Page.ClientScript.IsClientScriptIncludeRegistered (GetType (), "Mono-System.Web-ValidationClientScriptBlock"))
- Page.ClientScript.RegisterClientScriptInclude (GetType (), "Mono-System.Web-ValidationClientScriptBlock",
- Page.ClientScript.GetWebResourceUrl (GetType (),
-#if NET_2_0
- "WebUIValidation_2.0.js"));
-#else
- "WebUIValidation.js"));
-#endif
-
+ 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.ClientScript.RegisterArrayDeclaration ("Page_Validators",
- String.Format ("document.getElementById ('{0}')", ClientID));
+ 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, "')"));
+ }
}
-#if NET_2_0
- protected internal
-#else
- protected
-#endif
- override void Render (HtmlTextWriter writer)
+ protected internal override void Render (HtmlTextWriter writer)
{
-#if NET_2_0
- if (!Enabled && !EnableClientScript)
+ if (!IsEnabled && !EnableClientScript)
return;
-#endif
+
if (render_uplevel) {
/* according to an msdn article, this is done here */
RegisterValidatorDeclaration ();
if (!pre_render_called) {
render_tags = true;
render_text = true;
- }
- else if (render_uplevel) {
+ } else if (render_uplevel) {
render_tags = true;
-#if NET_2_0
render_text = Display != ValidatorDisplay.None;
-#else
- if (Display != ValidatorDisplay.None)
- render_text = !v || Display == ValidatorDisplay.Dynamic;
-#endif
- }
- else {
- if (Display == ValidatorDisplay.Static) {
+ } else {
+ if (Display != ValidatorDisplay.None) {
render_tags = !v;
render_text = !v;
- render_nbsp = v;
+ render_nbsp = v && Display == ValidatorDisplay.Static;
}
}
string text;
if (render_text) {
text = Text;
- if (text == "")
+ if (String.IsNullOrEmpty (text))
text = ErrorMessage;
- } else {
+ } else
text = " ";
- }
writer.Write (text);
}
- if (render_tags) {
+ if (render_tags)
writer.RenderEndTag ();
- }
}
/* the docs say "public sealed" here */
-#if NET_2_0
- public
-#else
- public virtual
-#endif
- void Validate ()
+ public void Validate ()
{
- if (Enabled && Visible)
+ if (IsEnabled && Visible)
IsValid = ControlPropertiesValid () && EvaluateIsValid ();
else
IsValid = true;
}
}
-
}