using System; using System.Collections.Generic; using System.Web.UI; using System.Web.UI.WebControls; using umbraco; using umbraco.cms.businesslogic.datatype; [assembly: WebResource("umbraco.editorControls.MultipleTextstring.MultipleTextstring.css", "text/css")] [assembly: WebResource("umbraco.editorControls.MultipleTextstring.MultipleTextstring.js", "application/x-javascript")] namespace umbraco.editorControls.MultipleTextstring { /// /// The MultipleTextstring control sets a character limit on a TextBox. /// [ValidationProperty("IsValid")] public class MultipleTextstringControl : PlaceHolder { /// /// Field for the list of values. /// private List values; /// /// The HiddenField to store the selected values. /// private HiddenField SelectedValues = new HiddenField(); /// /// Gets or sets the options. /// /// The options. public MultipleTextstringOptions Options { get; set; } /// /// Gets the value of IsValid. /// /// Returns 'Valid' if valid, otherwise an empty string. public string IsValid { get { if (!string.IsNullOrEmpty(this.Values)) { return "Valid"; } return string.Empty; } } /// /// Gets or sets the values. /// /// The values. public string Values { get { return this.SelectedValues.Value; } set { this.SelectedValues.Value = value; } } /// /// Initialize the control, make sure children are created /// /// An object that contains the event data. protected override void OnInit(EventArgs e) { base.OnInit(e); this.EnsureChildControls(); } /// /// Add the resources (sytles/scripts) /// /// The object that contains the event data. protected override void OnLoad(EventArgs e) { base.OnLoad(e); // Adds the client dependencies. this.AddResourceToClientDependency("umbraco.editorControls.MultipleTextstring.MultipleTextstring.css", ClientDependencyType.Css); this.AddResourceToClientDependency("umbraco.editorControls.MultipleTextstring.MultipleTextstring.js", ClientDependencyType.Javascript); } /// /// Raises the event. /// /// An object that contains the event data. protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); // initalise the string array/list. this.values = new List(); // load the values into a string array/list. if (!string.IsNullOrEmpty(this.Values)) { this.values.AddRange(this.Values.Split(new[] { Environment.NewLine }, StringSplitOptions.None)); } // check the minimum number allowed, add extra fields. if (this.values.Count < this.Options.Minimum && this.Options.Minimum > 1) { this.values.AddRange(new string(',', this.Options.Minimum - 1).Split(new[] { ',' }, StringSplitOptions.None)); } // check the maxmimum number allowed, remove the excess. if (this.values.Count > this.Options.Maximum && this.Options.Maximum > 0) { this.values.RemoveRange(this.Options.Maximum, this.values.Count - this.Options.Maximum); } // if there are no selected values... if (this.values.Count == 0) { // ... then add an empty string to display a single textstring box. this.values.Add(string.Empty); } } /// /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering. /// protected override void CreateChildControls() { base.CreateChildControls(); this.EnsureChildControls(); // populate the control's attributes. this.SelectedValues.ID = this.SelectedValues.ClientID; // add the controls. this.Controls.Add(this.SelectedValues); } /// /// Sends server control content to a provided object, which writes the content to be rendered on the client. /// /// The object that receives the server control content. protected override void Render(HtmlTextWriter writer) { writer.AddAttribute(HtmlTextWriterAttribute.Class, "MultipleTextstring"); writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID); writer.RenderBeginTag(HtmlTextWriterTag.Div); // loop through each value foreach (string value in this.values) { writer.AddAttribute(HtmlTextWriterAttribute.Class, "textstring-row"); writer.RenderBeginTag(HtmlTextWriterTag.Div); // input tag writer.AddAttribute(HtmlTextWriterAttribute.Class, "textstring-row-field"); writer.RenderBeginTag(HtmlTextWriterTag.Div); writer.WriteLine("", value.Replace("'", "'")); // append the add/remove buttons writer.WriteLine(" ", GlobalSettings.Path); writer.WriteLine(" ", GlobalSettings.Path); writer.RenderEndTag(); // .textstring-row-field writer.WriteLine("
", GlobalSettings.Path); writer.RenderEndTag(); // .textstring-row } this.SelectedValues.RenderControl(writer); writer.RenderEndTag(); // .MultipleTextstring // add jquery window load event var javascriptMethod = string.Format("jQuery('#{0}').MultipleTextstring('#{1}', {2}, {3});", this.ClientID, this.SelectedValues.ClientID, this.Options.Minimum, this.Options.Maximum); var javascript = string.Concat(""); writer.WriteLine(javascript); } } }