From 5c21799e51df810c18e38c4b1ce0b5797ea92c88 Mon Sep 17 00:00:00 2001 From: Niels R Date: Wed, 13 Jun 2012 08:46:21 +0200 Subject: [PATCH] Add encoding and additional attributes to controls * Add HTML encoding to value of CheckBoxControl * Add tabindex attribute to Control (and derived controls) * Add placeholder attribute to TextBoxControl * Add UseLineBreaks property to RadioButtonListControl --- Library/Source/Controls/CheckBoxControl.cs | 4 +++- .../Source/Controls/CheckBoxListControl.cs | 3 ++- Library/Source/Controls/Control.cs | 15 +++++++++++++++ Library/Source/Controls/DropdownControl.cs | 3 ++- Library/Source/Controls/MemoControl.cs | 1 + Library/Source/Controls/PasswordBoxControl.cs | 1 + Library/Source/Controls/RadioButtonControl.cs | 1 + .../Source/Controls/RadioButtonListControl.cs | 19 ++++++++++++++----- Library/Source/Controls/TextBoxControl.cs | 11 +++++++++++ .../FormCheckBoxListAttribute.cs | 5 +++-- .../FormAttributes/FormFieldAttribute.cs | 2 ++ .../FormRadioButtonListAttribute.cs | 2 ++ .../FormAttributes/FormTextBoxAttribute.cs | 2 ++ 13 files changed, 59 insertions(+), 10 deletions(-) diff --git a/Library/Source/Controls/CheckBoxControl.cs b/Library/Source/Controls/CheckBoxControl.cs index 1674ef1..0a0de00 100644 --- a/Library/Source/Controls/CheckBoxControl.cs +++ b/Library/Source/Controls/CheckBoxControl.cs @@ -26,6 +26,7 @@ using System; using System.Collections.Generic; +using System.Web; namespace Vici.Mvc { @@ -45,13 +46,14 @@ protected override string GenerateHtml(View view, string className, string class string s = "" + HttpUtility.HtmlEncode(value); + s += " type='checkbox' value='" + HttpUtility.HtmlEncode(Items[i].Key.ToString()) + "'" + (isChecked ? " checked='checked'" : "") + " />" + HttpUtility.HtmlEncode(value); if (Style == CheckBoxListStyle.Div) s += ""; diff --git a/Library/Source/Controls/Control.cs b/Library/Source/Controls/Control.cs index f57fab8..b4dd290 100644 --- a/Library/Source/Controls/Control.cs +++ b/Library/Source/Controls/Control.cs @@ -41,6 +41,7 @@ public abstract class Control private string _cssClassError; private bool _autoPost; private string _onChange; + private short _tabIndex = -1; private bool _enabled = true; private static readonly Dictionary _defaultCssClasses = new Dictionary(); @@ -157,6 +158,12 @@ public string OnChange set { _onChange = value; } } + public short TabIndex + { + get { return _tabIndex; } + set { _tabIndex = value; } + } + public bool Enabled { get { return _enabled; } @@ -273,6 +280,14 @@ protected string AddEnabledAttribute(string html) return html; } + protected string AddTabIndexAttribute(string html) + { + if (TabIndex >= 0) + html += " tabindex=\""+ TabIndex + "\""; + + return html; + } + public void HandlePostBack() { diff --git a/Library/Source/Controls/DropdownControl.cs b/Library/Source/Controls/DropdownControl.cs index 148592e..d74490c 100644 --- a/Library/Source/Controls/DropdownControl.cs +++ b/Library/Source/Controls/DropdownControl.cs @@ -256,6 +256,7 @@ protected override string GenerateHtml(View view, string className, string class s = AddClassAttribute(s, className , classNameError); s = AddEnabledAttribute(s); s = AddOnChangeAttribute(s); + s = AddTabIndexAttribute(s); s += ">\r\n"; @@ -267,7 +268,7 @@ protected override string GenerateHtml(View view, string className, string class string value = view.ParseTranslations(String.Format(formatString, Items[i].Key, Items[i].Value)); - s += "\r\n"; + s += "\r\n"; } s += ""; diff --git a/Library/Source/Controls/MemoControl.cs b/Library/Source/Controls/MemoControl.cs index 3d1b254..8bd5b59 100644 --- a/Library/Source/Controls/MemoControl.cs +++ b/Library/Source/Controls/MemoControl.cs @@ -67,6 +67,7 @@ protected override string GenerateHtml(View view, string className, string clasE s = AddEnabledAttribute(s); s = AddOnChangeAttribute(s); s = AddReadOnlyAttribute(s); + s = AddTabIndexAttribute(s); s += " rows=\"" + Height + "\""; s += " cols=\"" + Width + "\""; diff --git a/Library/Source/Controls/PasswordBoxControl.cs b/Library/Source/Controls/PasswordBoxControl.cs index e7d4f7a..ce61fe5 100644 --- a/Library/Source/Controls/PasswordBoxControl.cs +++ b/Library/Source/Controls/PasswordBoxControl.cs @@ -44,6 +44,7 @@ protected override string GenerateHtml(View view, string className, string clasE s = AddClassAttribute(s, className, clasError); s = AddEnabledAttribute(s); s = AddOnChangeAttribute(s); + s = AddTabIndexAttribute(s); if (MaxLength != Int32.MaxValue) s += " maxlength=\"" + MaxLength + "\""; diff --git a/Library/Source/Controls/RadioButtonControl.cs b/Library/Source/Controls/RadioButtonControl.cs index 8f8bb47..e864b7b 100644 --- a/Library/Source/Controls/RadioButtonControl.cs +++ b/Library/Source/Controls/RadioButtonControl.cs @@ -73,6 +73,7 @@ protected override string GenerateHtml(View view, string className, string class s = AddClassAttribute(s, className, classNameError); s = AddEnabledAttribute(s); + s = AddTabIndexAttribute(s); if (!string.IsNullOrEmpty(_onClick)) s += " onclick=\"" + _onClick + "\""; diff --git a/Library/Source/Controls/RadioButtonListControl.cs b/Library/Source/Controls/RadioButtonListControl.cs index 83296cf..56047c5 100644 --- a/Library/Source/Controls/RadioButtonListControl.cs +++ b/Library/Source/Controls/RadioButtonListControl.cs @@ -76,6 +76,8 @@ public Item(object key, object value) private string _keyMember; private string _valueMember; + private bool _useLineBreaks; + private string _valueFormatString; private readonly List _items = new List(); @@ -126,6 +128,12 @@ public string ValueMember } } + public bool UseLineBreaks + { + get { return _useLineBreaks; } + set { _useLineBreaks = value; } + } + public string ValueFormatString { get { return _valueFormatString; } @@ -183,15 +191,16 @@ protected override string GenerateHtml(View view, string className, string class string formatString = _valueFormatString ?? "{1}"; // Append the elements of the list - for (int i = 0; i < Items.Count; i++) + foreach (Item t in Items) { - bool isCurrent = Equals(Value, Items[i].Key); + bool isCurrent = Equals(Value, t.Key); - string value = view.ParseTranslations(String.Format(formatString, Items[i].Key, Items[i].Value)); + string value = view.ParseTranslations(String.Format(formatString, t.Key, t.Value)); output.Append("" + HttpUtility.HtmlEncode(value) + "\r\n"); + AddClassAttribute("", className, classNameError) + " " + AddNameAttribute("") + " value=\"" + + HttpUtility.HtmlEncode(t.Key.ToString()) + "\"" + (isCurrent ? " checked" : "") + ">" + HttpUtility.HtmlEncode(value) + + (_useLineBreaks ? "
" : "") + "\r\n"); } return output.ToString(); diff --git a/Library/Source/Controls/TextBoxControl.cs b/Library/Source/Controls/TextBoxControl.cs index 3affdd2..c029c80 100644 --- a/Library/Source/Controls/TextBoxControl.cs +++ b/Library/Source/Controls/TextBoxControl.cs @@ -39,6 +39,7 @@ public class TextBoxControl : Control private string _onKeyDown; private string _onKeyUp; private bool _readOnly; + private string _placeholder; public TextBoxControl(string name) : base(name) { @@ -92,6 +93,12 @@ public bool ReadOnly set { _readOnly = value; } } + public string Placeholder + { + get { return _placeholder; } + set { _placeholder = value; } + } + protected override string GenerateHtml(View view, string className, string clasError) { string s = ""; } diff --git a/Library/Source/FormAttributes/FormCheckBoxListAttribute.cs b/Library/Source/FormAttributes/FormCheckBoxListAttribute.cs index 86dbc78..e64cb43 100644 --- a/Library/Source/FormAttributes/FormCheckBoxListAttribute.cs +++ b/Library/Source/FormAttributes/FormCheckBoxListAttribute.cs @@ -61,8 +61,9 @@ protected internal override void SetControlValue(Control control, object value) else checkBoxListControl.Values.Clear(); - foreach (object valueObject in (IEnumerable) value) - checkBoxListControl.Values.Add(valueObject); + if (value != null) + foreach (object valueObject in (IEnumerable) value) + checkBoxListControl.Values.Add(valueObject); } protected internal override Control CreateControl(string name) diff --git a/Library/Source/FormAttributes/FormFieldAttribute.cs b/Library/Source/FormAttributes/FormFieldAttribute.cs index ba72af0..4ff9c21 100644 --- a/Library/Source/FormAttributes/FormFieldAttribute.cs +++ b/Library/Source/FormAttributes/FormFieldAttribute.cs @@ -36,6 +36,7 @@ public abstract class FormFieldAttribute : Attribute public bool AutoPost; public string OnChange; public string ValidationErrorMsg; + public short TabIndex; protected internal Type FieldType; internal object FormData; @@ -53,6 +54,7 @@ internal Control CreateControl() control.Id = Id; control.AutoPost = AutoPost; control.OnChange = OnChange; + control.TabIndex = TabIndex; return control; } diff --git a/Library/Source/FormAttributes/FormRadioButtonListAttribute.cs b/Library/Source/FormAttributes/FormRadioButtonListAttribute.cs index 4bf8075..e3408c6 100644 --- a/Library/Source/FormAttributes/FormRadioButtonListAttribute.cs +++ b/Library/Source/FormAttributes/FormRadioButtonListAttribute.cs @@ -33,6 +33,7 @@ public class FormRadioButtonListAttribute : FormFieldAttribute public string KeyMember { get; set; } public string ValueMember { get; set; } public object DataSource { get; set; } + public bool UseLineBreaks { get; set; } public string ValueFormatString { get; set; } public FormRadioButtonListAttribute() @@ -61,6 +62,7 @@ protected internal override Control CreateControl(string name) DataSource = DataSource, KeyMember = KeyMember, ValueMember = ValueMember, + UseLineBreaks = UseLineBreaks, ValueFormatString = ValueFormatString }; diff --git a/Library/Source/FormAttributes/FormTextBoxAttribute.cs b/Library/Source/FormAttributes/FormTextBoxAttribute.cs index e0999e3..c868293 100644 --- a/Library/Source/FormAttributes/FormTextBoxAttribute.cs +++ b/Library/Source/FormAttributes/FormTextBoxAttribute.cs @@ -43,6 +43,7 @@ public class FormTextBoxAttribute : FormFieldAttribute public string OnKeyUp; public string OnKeyPress; public bool ReadOnly; + public string Placeholder; protected internal override bool IsRightType() { @@ -87,6 +88,7 @@ protected internal override Control CreateControl(string name) control.OnKeyUp = OnKeyUp; control.OnKeyPress = OnKeyPress; control.ReadOnly = ReadOnly; + control.Placeholder = Placeholder; return control; }