Beispiel #1
0
        private static HtmlTableCell GetCompleteHtmlTableCell(KeyValuePair <string, int> item, CheckBoxListSettings settings, IList <int> selectedCheckBoxes)
        {
            //Add checkBox
            var htmlTableCheckboxCell = new HtmlTableCell
            {
                InnerHtml = GenerateHtmlMarkupCheckBox(item, settings, selectedCheckBoxes) + GenerateHtmlMarkupLabel(item)
            };

            return(htmlTableCheckboxCell);
        }
Beispiel #2
0
        private static string GenerateHtmlMarkupCheckBox(KeyValuePair <string, int> item, CheckBoxListSettings settings, IList <int> selectedCheckBoxes)
        {
            var tagBuilder = new TagBuilder("input");

            tagBuilder.MergeAttribute("type", "checkbox");
            tagBuilder.MergeAttribute("name", settings.CblName);
            tagBuilder.MergeAttribute("value", item.Value.ToString(CultureInfo.InvariantCulture));

            if (selectedCheckBoxes != null)
            {
                if (selectedCheckBoxes.Contains(item.Value))
                {
                    tagBuilder.MergeAttribute("checked", null);
                }
            }

            return(tagBuilder.ToString(TagRenderMode.SelfClosing));
        }
Beispiel #3
0
        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, IDictionary <string, int> items, IList <int> selectedCheckBoxes, CheckBoxListSettings settings)
        {
            var htmlTable = new HtmlTable();

            var tempTableRow = new HtmlTableRow();

            foreach (var item in items)
            {
                if (tempTableRow.Cells.Count >= ((int)settings.CblRepeatColumns))
                {
                    htmlTable.Rows.Add(tempTableRow);
                    tempTableRow = new HtmlTableRow();
                }

                var tableCell = GetCompleteHtmlTableCell(item, settings, selectedCheckBoxes);
                tempTableRow.Cells.Add(tableCell);
            }

            if (tempTableRow.Cells.Count > 0)
            {
                htmlTable.Rows.Add(tempTableRow);
            }

            string result;

            using (var sw = new StringWriter())
            {
                htmlTable.RenderControl(new HtmlTextWriter(sw));
                result = sw.ToString();
            }
            return(new MvcHtmlString(result));
        }