/// <summary> /// Adds a binding error to the collection of binding errors. /// </summary> /// <param name="ErrorMessage"></param> /// <param name="control"></param> /// <returns>false if the control was not able to get a control reference to attach hotlinks and an icon. Error message always gets added</returns> public bool AddBindingError(string ErrorMessage, Control Control) { wwDataBindingItem DataBindingItem = null; if (Control == null) { this.BindingErrors.Add(new BindingError(ErrorMessage)); return(false); } foreach (wwDataBindingItem Ctl in this.DataBindingItems) { if (Ctl.ControlId == Control.ID) { Ctl.ControlInstance = Control; DataBindingItem = Ctl; break; } } // *** No associated control found - just add the error message if (DataBindingItem == null) { this.BindingErrors.Add(new BindingError(ErrorMessage, Control.ClientID)); return(false); } return(this.AddBindingError(ErrorMessage, DataBindingItem)); }
/// <summary> /// Creates the text for binding error messages based on the /// BindingErrorMessage property of a data bound control. /// /// If set the control calls this method render the error message. Called by /// the various controls to generate the error HTML based on the <see>Enum /// ErrorMessageLocations</see>. /// /// If UseClientScriptHtmlInjection is set the error message is injected /// purely through a client script JavaScript function which avoids problems /// with Controls.Add() when script tags are present in the container. /// </summary> /// <param name="Item"> /// Instance of the control that has an error. /// </param> /// <returns>String</returns> internal string GetBindingErrorMessageHtml(wwDataBindingItem Item) { string Image = null; if (string.IsNullOrEmpty(this.ErrorIconUrl) || this.ErrorIconUrl == "WebResource") { Image = Web.Utils.GetSkinnedUrl("images/error-s.png"); } else { Image = this.ResolveUrl(this.ErrorIconUrl); } string Message = ""; if (Item.ErrorMessageLocation == BindingErrorMessageLocations.WarningIconRight) { Message = String.Format(CultureInfo.CurrentCulture, " <img src=\"{0}\" alt=\"{1}\" />", Image, Item.BindingErrorMessage); } else if (Item.ErrorMessageLocation == BindingErrorMessageLocations.RedTextBelow) { Message = "<br /><span style=\"color:red;\"><smaller>" + Item.BindingErrorMessage + "</smaller></span>"; } else if (Item.ErrorMessageLocation == BindingErrorMessageLocations.RedTextAndIconBelow) { Message = String.Format(CultureInfo.CurrentCulture, "<br /><img src=\"{0}\"> <span style=\"color:red;\" /><smaller>{1}</smaller></span>", Image, Item.BindingErrorMessage); } else if (Item.ErrorMessageLocation == BindingErrorMessageLocations.None) { Message = ""; } else { Message = "<span style='color:red;font-weight:bold;'> * </span>"; } // *** Fix up message so ' are allowed Message = Message.Replace("'", @"\'"); // *** Use Client Side JavaScript to inject the message rather than adding a control if (this.UseClientScriptHtmlInjection && Item.ControlInstance != null) { if (!this._ClientScriptInjectionScriptAdded) { this.AddScriptForAddHtmlAfterControl(); } this.Page.ClientScript.RegisterStartupScript(this.GetType(), Item.ControlId, String.Format(CultureInfo.CurrentCulture, "AddHtmlAfterControl('{0}','{1}');\r\n", Item.ControlInstance.ClientID, Message), true); // *** Message is handled in script so nothing else to write Message = ""; } // *** Message will be embedded with a Literal Control return(Message); }
/// <summary> /// Fires the ValidateControlEvent /// </summary> /// <param name="Item"></param> /// <returns>false - Validation for control failed and a BindingError is added, true - Validation succeeded</returns> public bool OnValidateControl(wwDataBindingItem Item) { if (this.ValidateControl != null && !this.ValidateControl(Item)) { return(false); } return(true); }
/// <summary> /// This method only adds a data binding item, but doesn't bind it /// to anything. This can be useful for only displaying errors /// </summary> /// <param name="ControlToBind"></param> /// <returns></returns> public wwDataBindingItem AddBinding(Control ControlToBind) { wwDataBindingItem Item = new wwDataBindingItem(this); Item.ControlInstance = ControlToBind; Item.ControlId = ControlToBind.ID; Item.Page = this.Page; this.DataBindingItems.Add(Item); return(Item); }
/// <summary> /// Manages errors that occur during unbinding. Sets BindingErrors collection and /// and writes out validation error display to the page if specified /// </summary> /// <param name="Item"></param> /// <param name="ex"></param> private void HandleUnbindingError(wwDataBindingItem Item, Exception ex) { Item.IsBindingError = true; // *** Display Error info by setting BindingErrorMessage property try { string ErrorMessage = null; // *** Must check that the control exists - if invalid ID was // *** passed there may not be an instance! if (Item.ControlInstance == null) { ErrorMessage = "Invalid Control: " + Item.ControlId; } else { string DerivedUserFieldName = this.DeriveUserFieldName(Item); if (ex is RequiredFieldException) { ErrorMessage = DerivedUserFieldName + " can't be left empty"; } else if (ex is ValidationErrorException) { /// *** Binding Error Message will be set ErrorMessage = ex.Message; } // *** Explicit error message returned else if (ex is BindingErrorException) { ErrorMessage = ex.Message + " for " + DerivedUserFieldName; } else { if (string.IsNullOrEmpty(Item.BindingErrorMessage)) { ErrorMessage = DerivedUserFieldName + ": " + ex.Message; // Change to this 2009.01.26 to yield a more informative message } //ErrorMessage = "Invalid format for " + DerivedUserFieldName; else { // *** Control has a pre-assigned error message ErrorMessage = Item.BindingErrorMessage; } } } this.AddBindingError(ErrorMessage, Item); } catch (Exception) { this.AddBindingError("Binding Error", Item); } }
/// <summary> /// Adds a binding to the control. This method is a simple /// way to establish a binding. /// /// Returns the Item so you can customize properties further /// </summary> /// <param name="ControlToBind"></param> /// <param name="ControlPropertyToBind"></param> /// <param name="SourceObjectToBindTo"></param> /// <param name="SourceMemberToBindTo"></param> public wwDataBindingItem AddBinding(Control ControlToBind, string ControlPropertyToBind, object SourceObjectToBindTo, string SourceMemberToBindTo) { wwDataBindingItem Item = new wwDataBindingItem(this); Item.ControlInstance = ControlToBind; Item.ControlId = ControlToBind.ID; Item.BindingSourceObject = SourceObjectToBindTo; Item.BindingSourceMember = SourceMemberToBindTo; this.DataBindingItems.Add(Item); return(Item); }
/// <summary> /// Add a wwDataBindingItem to the collection /// </summary> /// <param name="index"></param> /// <param name="Item"></param> public void AddAt(int index, wwDataBindingItem Item) { if (_ParentDataBinder != null) { Item.Page = _ParentDataBinder.Page; Item.Binder = _ParentDataBinder; // *** VS Designer adds new items as soon as their accessed // *** but items may not be valid so we have to clean up if (this._ParentDataBinder.DesignMode) { UpdateListInDesignMode(); } } InnerList.Insert(index, Item); }
/// <summary> /// Add a wwDataBindingItem to the collection /// </summary> /// <param name="Item"></param> public void Add(wwDataBindingItem Item) { if (_ParentDataBinder != null) { Item.Page = _ParentDataBinder.Page; Item.Binder = _ParentDataBinder; // *** VS Designer adds new items as soon as their accessed // *** but items may not be valid so we have to clean up if (this._ParentDataBinder.DesignMode) { // *** Remove any blank items UpdateListInDesignMode(); } } this.InnerList.Add(Item); }
/// <summary> /// Adds a binding error for DataBindingItem control. This is the most efficient /// way to add a BindingError. The other overloads call into this method after /// looking up the Control in the DataBinder. /// </summary> /// <param name="ErrorMessage"></param> /// <param name="BindingItem"></param> /// <returns></returns> public bool AddBindingError(string ErrorMessage, wwDataBindingItem BindingItem) { // *** Associated control found - add icon and link id if (BindingItem.ControlInstance != null) { this.BindingErrors.Add(new BindingError(ErrorMessage, BindingItem.ControlInstance.ClientID)); } else { // *** Just set the error message this.BindingErrors.Add(new BindingError(ErrorMessage)); return(false); } BindingItem.BindingErrorMessage = ErrorMessage; // *** Insert the error text/icon as a literal if (this.ShowBindingErrorsOnControls && BindingItem.ControlInstance != null) { // *** Retrieve the Html Markup for the error // *** NOTE: If script code injection is enabled this is done with client // *** script code to avoid Controls.Add() functionality which may not // *** always work reliably if <%= %> tags are in document. Script HTML injection // *** is the preferred behavior as it should work on any page. If script is used // *** the message returned is blank and the startup script is embedded instead string HtmlMarkup = this.GetBindingErrorMessageHtml(BindingItem); if (!string.IsNullOrEmpty(HtmlMarkup)) { LiteralControl Literal = new LiteralControl(HtmlMarkup); Control Parent = BindingItem.ControlInstance.Parent; int CtlIdx = Parent.Controls.IndexOf(BindingItem.ControlInstance); try { // *** Can't add controls to the Control collection if <%= %> tags are on the page Parent.Controls.AddAt(CtlIdx + 1, Literal); } catch {; } } } return(true); }
/// <summary> /// Returns a UserField name. Returns UserFieldname if set, or if not /// attempts to derive the name based on the field. /// </summary> /// <param name="Control"></param> /// <returns></returns> protected string DeriveUserFieldName(wwDataBindingItem Item) { if (!string.IsNullOrEmpty(Item.UserFieldName)) { return(Item.UserFieldName); } string ControlID = Item.ControlInstance.ID; // *** Try to get a name by stripping of control prefixes string ControlName = Regex.Replace(Item.ControlInstance.ID, "^txt|^chk|^lst|^rad|", "", RegexOptions.IgnoreCase); if (ControlName != ControlID) { return(ControlName); } // *** Nope - use the default ID return(ControlID); }
/// <summary> /// Returns a specific DataBinding Item for a given control. /// Always returns an item even if the Control is not found. /// If you need to check whether this is a valid item check /// the BindingSource property for being blank. /// /// Extender Property Get method /// </summary> /// <param name="control"></param> /// <returns></returns> public wwDataBindingItem GetDataBindingItem(Control control) { foreach (wwDataBindingItem Item in this.DataBindingItems) { if (Item.ControlId == control.ID) { // *** Ensure the binder is set on the item Item.Binder = this; return(Item); } } wwDataBindingItem NewItem = new wwDataBindingItem(this); NewItem.ControlId = control.ID; NewItem.ControlInstance = control; this.DataBindingItems.Add(NewItem); return(NewItem); }
/// <summary> /// Adds a binding error message to a specific control attached to this binder /// BindingErrors collection. /// </summary> /// <param name="ControlName">Form relative Name (ID) of the control to set the error on</param> /// <param name="ErrorMessage">The Error Message to set it to.</param> /// <returns>true if the control was found. False if not found, but message is still assigned</returns> public bool AddBindingError(string ErrorMessage, string ControlName) { wwDataBindingItem DataBindingItem = null; foreach (wwDataBindingItem Ctl in this.DataBindingItems) { if (Ctl.ControlId == ControlName) { DataBindingItem = Ctl; break; } } if (DataBindingItem == null) { this.BindingErrors.Add(new BindingError(ErrorMessage)); return(false); } return(this.AddBindingError(ErrorMessage, DataBindingItem)); }