/// <summary> /// Create a select list /// </summary> /// <param name="name">Name use for the id and name of the html element</param> /// <param name="options">The list of options to add to the select list</param> /// <returns>A TagBuilder class to continue to create the tag by adding attr classes etc.</returns> public TagBuilder Select(string name, SelectItemList options) { return new TagBuilder("select").options(options.ToArray()).IDandName(name); }
/// <summary> /// Creates a list of checkbox to allow the user to select from multiple values. Values are posted to the server as 1,2,3 etc. /// </summary> /// <param name="name">The name to assign to the checkbox inputs</param> /// <param name="items">The list of items for the list</param> /// <param name="selectedValues">The list of values to select</param> /// <returns> /// Span with the id of [name]Container that encompases the whole list. /// A span item for each checkbox with the class checkboxContainer assigned. A [name]_chk_[i] for each checkbox input to be rendered. /// A hidden input field for each checkbox item, when it is submitted to the server, it will be the list of values separated by commas /// </returns> public TagBuilder CheckboxList(string name, SelectItemList items, IEnumerable<int> selectedValues) { if (selectedValues != null) { foreach (var val in selectedValues) { foreach (var item in items.ToArray()) { if (val == int.Parse(item.Value)) item.Selected = true; } } } return CheckboxList(name, items, -1); }
/// <summary> /// Creates a list of checkbox to allow the user to select multiple values. Values are posted to the server as 1,2,3 etc. /// </summary> /// <param name="name">The name to assign to the checkbox inputs</param> /// <param name="items">The list of items for the list</param> /// <param name="tabIndex">The tab index for the items</param> /// <returns> /// Span with the id of [name]Container that encompases the whole list. /// A span item for each checkbox with the class checkboxContainer assigned. A [name]_chk_[i] for each checkbox input to be rendered. /// A hidden input field for each checkbox item, when it is submitted to the server, it will be the list of values separated by commas /// </returns> public TagBuilder CheckboxList(string name, SelectItemList items, int tabIndex) { var parent = new TagBuilder("span").ID(name + "Container"); var i = 0; foreach (var item in items.ToArray()) { var checkboxId = name + "_chk_" + i; var checkboxName = name + "_chk"; var hiddenId = name + i; var hiddenName = name; // Add item container var checkboxContainer = parent.AddChild("span").AddClass("checkboxContainer"); // Add the radio input checkboxContainer.AddChild("input").type("checkbox") .Name(checkboxName) .ID(checkboxId) .value(item.Value) .@checked(item.Selected) .tabIndex(tabIndex, -1) .onclick("jQuery('#" + hiddenId + "').val((this.checked ? $(this).val() : ''));"); // Add the hidden field that will hold the value (default the value if the item is selected) checkboxContainer.AddChild("input").type("hidden") .Name(hiddenName) .ID(hiddenId) .valueIf(item.Selected, item.Value); // Add the radio label checkboxContainer.AddChild("label").@for(name + i).AddClass("checkboxLabel").InnerText(item.Text); // Make the id unique i += 1; } return parent; }
/// <summary> /// Create a radio list /// </summary> /// <param name="name">Element Name</param> /// <param name="items">Option list</param> /// <param name="tabIndex">The tabIndex to assign to the radio item</param> /// <param name="addCssClassToInput">Add a CSS class to every input created</param> /// <param name="addCssClassToLabel">Add a css class to every label for every input</param> /// <param name="addCssClassToRow">This will add a set of css classes to the the row contianing the the input and label</param> /// <returns>A TagBuilder class to continue to create the tag by adding attr classes etc.</returns> public TagBuilder RadioList(string name, SelectItemList items, int tabIndex, string addCssClassToInput, string addCssClassToLabel, string addCssClassToRow) { var parent = new TagBuilder("span").ID(name + "Container"); if (string.IsNullOrEmpty(addCssClassToInput)) addCssClassToInput = ""; if (string.IsNullOrEmpty(addCssClassToLabel)) addCssClassToLabel = ""; if (string.IsNullOrEmpty(addCssClassToRow)) addCssClassToRow = ""; var i = 0; foreach (var item in items.ToArray()) { // Add radioItem var radioItem = parent.AddChild("span").AddClass("radioItem " + addCssClassToRow); // Add the radio input var radio = radioItem.AddChild("input").Name(name).type("radio").value(item.Value).ID(name + i).AddClass(addCssClassToInput); // Mark as selected if (item.Selected) radio.@checked(true); // Set the tabIndex if (tabIndex > -1) radio.tabIndex(tabIndex); // Add the radio label radioItem.AddChild("label").@for(name + i).AddClass("radioLabel " + addCssClassToLabel).InnerText(item.Text); // Make the id unique i += 1; } return parent; }
/// <summary> /// <summary> /// Create a radio list /// </summary> /// <param name="name">The name to assign to the checkbox inputs</param> /// <param name="items">The list of items for the list</param> /// <param name="selectedValues">The list of values to select</param> /// <returns> /// Span with the id of [name]Container that encompases the whole list. /// A span item for each checkbox with the class checkboxContainer assigned. A [name]_chk_[i] for each checkbox input to be rendered. /// A hidden input field for each checkbox item, when it is submitted to the server, it will be the list of values separated by commas /// </returns> public TagBuilder CheckboxList(string name, SelectItemList items, List<string> selectedValues) { if (selectedValues != null && selectedValues.Count > 0) { foreach (var val in selectedValues) { foreach (var item in items.ToArray()) { if (val == item.Value) item.Selected = true; } } } return CheckboxList(name, items, -1); }
/// <summary> /// Create a radio list /// </summary> /// <param name="name">Element Name</param> /// <param name="items">Option list</param> /// <returns>A TagBuilder class to continue to create the tag by adding attr classes etc.</returns> public TagBuilder RadioList(string name, SelectItemList items) { return RadioList(name, items, -1, string.Empty, string.Empty, string.Empty); }
/// <summary> /// Create a radio list /// </summary> /// <param name="name">Element Name</param> /// <param name="items">Option list</param> /// <param name="tabIndex">The tabIndex to assign to the radio item</param> /// <returns>A TagBuilder class to continue to create the tag by adding attr classes etc.</returns> public TagBuilder RadioList(string name, SelectItemList items, int tabIndex) { return RadioList(name, items, tabIndex, string.Empty, string.Empty, string.Empty); }
/// <summary> /// Create a radio list where the value is meant to be a string. /// </summary> /// <param name="name">Element Name</param> /// <param name="value">True or false. These string value will be translated to true: on, yes, true, 1 as needed to identify as true</param> /// <param name="items">Option list</param> /// <param name="tabIndex">The tab index to assign to the radio item</param> /// <param name="addCssClassToInput">Classes to add to the radio input</param> /// <param name="addCssClassToLabel">Classes to add to the the label of the input control</param> /// <param name="addCssClassToRow">This will add a set of css classes to the the row contianing the the input and label</param> /// <returns>A TagBuilder class to continue to create the tag by adding attr classes etc.</returns> public TagBuilder RadioList(string name, string value, SelectItemList items, int tabIndex, string addCssClassToInput, string addCssClassToLabel, string addCssClassToRow) { if (addCssClassToInput == null) throw new ArgumentNullException("addCssClassToInput"); if (string.IsNullOrEmpty(value) == false) { foreach (var item in items.ToArray()) { if (item.Value.ToLower() == value.ToLower()) item.Selected = true; } } return RadioList(name, items, tabIndex, addCssClassToInput, addCssClassToLabel, addCssClassToRow); }
/// <summary> /// Create a radio list where the value is meant to be a string. /// </summary> /// <param name="name">Element Name</param> /// <param name="value">True or false. These string value will be translated to true: on, yes, true, 1 as needed to identify as true</param> /// <param name="items">Option list</param> /// <returns>A TagBuilder class to continue to create the tag by adding attr classes etc.</returns> public TagBuilder RadioList(string name, string value, SelectItemList items) { return RadioList(name, value, items.ToArray()); }
/// <summary> /// Create a radio list where the value is meant to be a boolean value. /// </summary> /// <param name="name">Element Name</param> /// <param name="value">True or false. These string value will be translated to true: on, yes, true, 1 as needed to identify as true</param> /// <param name="items">Option list</param> public TagBuilder RadioList(string name, bool value, SelectItemList items) { foreach (SelectItem item in items.ToArray()) { var itemBoolValue = false; switch (item.Value.ToLower()) { case "on": itemBoolValue = true; break; case "yes": itemBoolValue = true; break; case "true": itemBoolValue = true; break; case "1": itemBoolValue = true; break; } item.Selected = (itemBoolValue == value); } return RadioList(name, items, -1, string.Empty, string.Empty, string.Empty); }
/// <summary> /// Create a radio list /// </summary> /// <param name="name">Element Name</param> /// <param name="items">Option list</param> /// <param name="tabIndex">The tabIndex to assign to the radio item</param> /// <returns>A TagBuilder class to continue to create the tag by adding attr classes etc.</returns> public TagBuilder BoundRadioList(string name, SelectItemList items, int tabIndex) { return BoundRadioList(name, items.ToArray(), tabIndex); }
/// <summary> /// Create a radio list /// </summary> /// <param name="name">Element Name</param> /// <param name="items">Option list</param> /// <returns>A TagBuilder class to continue to create the tag by adding attr classes etc.</returns> public TagBuilder BoundRadioList(string name, SelectItemList items) { return BoundRadioList(name, items.ToArray(), -1); }