Пример #1
0
        public static MvcHtmlString CheckBoxWithValue(this HtmlHelper htmlHelper, string name, bool isChecked, string value)
        {
            string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
            ModelState modelState;

            //将ModelState设置为表示是否勾选布尔值
            if (htmlHelper.ViewData.ModelState.TryGetValue(fullHtmlFieldName, out modelState))
            {
                htmlHelper.ViewData.ModelState.SetModelValue(fullHtmlFieldName, new ValueProviderResult(isChecked, isChecked.ToString(), CultureInfo.CurrentCulture));
            }
            MvcHtmlString html;
            try
            {
                html = htmlHelper.CheckBox(name, isChecked);
            }
            finally
            {
                //将ModelState还原
                if (null != modelState)
                {
                    htmlHelper.ViewData.ModelState[fullHtmlFieldName] = modelState;
                }
            }
            string htmlString = html.ToHtmlString();
            var index = htmlString.LastIndexOf('<');
            //过滤掉类型为"hidden"的<input>元素
            XElement element = XElement.Parse(htmlString.Substring(0, index));
            element.SetAttributeValue("value", value);
            return new MvcHtmlString(element.ToString());
        }
 public static CheckBoxWrapper CheckBoxWithIdAndValue(this Browser browser, [NotNull] string id, [NotNull] string value)
 {
     const string message = "checkbox with id '{0}' and value '{1}'";
     var checkBox = browser.CheckBoxes.FirstOrDefault(x => x.Id == id && x.GetAttributeValue("value") == value) ??
                    browser.CheckBox(Find.ById(id));
     return new CheckBoxWrapper(checkBox, String.Format(message, id, value));
 }
 public static string CheckBox(this HtmlHelper instance, string id, bool isChecked, string labelText)
 {
     return (new StringBuilder())
                 .Append(instance.CheckBox(id, isChecked))
                 .Append("<label for=\"")
                 .Append(id)
                 .Append("\">")
                 .Append(labelText)
                 .Append("</label>")
             .ToString();
 }
Пример #4
0
 public static MvcHtmlString CheckBox(this HtmlHelper helper, string name, bool isEditable, bool isChecked, IDictionary<string, object> htmlAttributes)
 {
     ClassAttributeFix(ref htmlAttributes);
     if (isEditable)
     {
         return helper.CheckBox(name, isChecked, htmlAttributes);
     }
     else
     {
         htmlAttributes.Add(new KeyValuePair<string, object>("disabled", "disabled"));
         return helper.CheckBox(name, isChecked, htmlAttributes);
     }
 }
        public static MvcHtmlString ThemeVarEditor(this HtmlHelper html, ThemeVariableInfo info, object value)
        {
            Guard.ArgumentNotNull(info, "info");

            string expression = html.NameForThemeVar(info);

			var strValue = string.Empty;

			var arrValue = value as string[];
			if (arrValue != null)
			{
				strValue = arrValue.Length > 0 ? arrValue[0] : value.ToString();
			}
			else
			{
				strValue = value.ToString();
			}

			var currentTheme = ThemeHelper.ResolveCurrentTheme();
			var isDefault = strValue.IsCaseInsensitiveEqual(info.DefaultValue);

			MvcHtmlString control;

            if (info.Type == ThemeVariableType.Color)
            {
				control = html.ColorBox(expression, strValue, info.DefaultValue);
            }
            else if (info.Type == ThemeVariableType.Boolean)
            {
				control = html.CheckBox(expression, strValue.ToBool());
            }
			else if (info.Type == ThemeVariableType.Select)
			{
				control = ThemeVarSelectEditor(html, info, expression, strValue);
			}
			else
			{
				control = html.TextBox(expression, isDefault ? "" : strValue, new { placeholder = info.DefaultValue });
			}

			if (currentTheme != info.Manifest)
			{
				// the variable is inherited from a base theme: display an info badge
				var chainInfo = "<span class='themevar-chain-info'><i class='fa fa-chain fa-flip-horizontal'></i>&nbsp;{0}</span>".FormatCurrent(info.Manifest.ThemeName);
				return MvcHtmlString.Create(control.ToString() + chainInfo);
			}
			else
			{
				return control;
			}	
        }
        public static MvcHtmlString CheckBox(
            this HtmlHelper htmlHelper,
            string name,
            bool isChecked,
            Property property,
            IDictionary<string, object> htmlAttributes)
        {
            var validationAttributes = PropertyUnobtrusiveValidationAttributesGenerator
                .GetValidationAttributes(property, htmlHelper.ViewContext);

            htmlAttributes = htmlAttributes.Merge(validationAttributes);

            return htmlHelper.CheckBox(name, isChecked, htmlAttributes);
        }
 public static string CheckBoxList(this HtmlHelper htmlHelper, IDictionary<string, string> checkBoxes, int boxesPerBreak)
 {
     string retval="";
     int box = 0;
     foreach(var i in checkBoxes)
     {
         retval += htmlHelper.CheckBox(i.Key, new {value = i.Value});
         retval += "<label for=\"" + i.Key + "\">" + i.Value + "</label>\n";
         box++;
         if(box % boxesPerBreak == 0)
         {
             retval += "<br />\n";
         }
     }
     return (retval);
 }
Пример #8
0
        public static string CheckBoxList(this HtmlHelper htmlHelper, string name, CheckValues values, IDictionary<string, object> htmlAttributes)
        {
            if (values == null)
                throw new ArgumentException("Value cannot be null.", "values");

            var sb = new StringBuilder();
            
            for (int i = 0; i < values.Count; i++)
            {
                sb.Append(htmlHelper.Hidden(name + "[" + i + "].Key", values.Keys[i]));
                sb.Append("<label>");
                sb.Append(htmlHelper.CheckBox(name + "[" + i + "].Value", values.Values[i], htmlAttributes));
                sb.Append(values.Keys[i]);
                sb.Append("</label>");
            }

            return sb.ToString();
        }
        public static MvcHtmlString CheckBox(
            this HtmlHelper htmlHelper,
            string name,
            bool isChecked,
            Property property,
            IDictionary<string, object> htmlAttributes)
        {
            // create own metadata based on PropertyViewModel
            var metadata = new ModelMetadata(
                ModelMetadataProviders.Current,
                property.Entity.Type,
                null,
                property.TypeInfo.Type,
                property.Name);
            var validationAttributes =
                htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata);

            htmlAttributes = htmlAttributes.Merge(validationAttributes);

            return htmlHelper.CheckBox(name, isChecked, htmlAttributes);
        }
Пример #10
0
        public static IHtmlString CheckboxList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> items)
        {
            TagBuilder ulTag = new TagBuilder("ul");
            ulTag.AddCssClass("checkbox-list");

            foreach (var item in items)
            {
                var liTag = new TagBuilder("li");
                var ckValue = item.Value ?? item.Text;
                var ckId = name + "_" + ckValue;
                var checkboxTag = helper.CheckBox(name, item.Selected, new { value = ckValue, id = ckId }); //helper.RadioButton(name, rbValue, item.Selected, new { id = rbId });

                var labelTag = new TagBuilder("label");
                labelTag.MergeAttribute("for", ckId);
                labelTag.MergeAttribute("class", "inline");
                labelTag.InnerHtml = item.Text ?? item.Value;

                liTag.InnerHtml = checkboxTag.ToString() + labelTag.ToString();

                ulTag.InnerHtml += liTag.ToString();
            }

            return new HtmlString(ulTag.ToString());
        }
        public static MvcHtmlString ThemeVarEditor(this HtmlHelper html, ThemeVariableInfo info, object value)
        {
            Guard.ArgumentNotNull(info, "info");

            string expression = html.NameForThemeVar(info);

            if (info.Type == ThemeVariableType.Color)
            {
                return html.ColorBox(expression, value.ToString());
            }
            else if (info.Type == ThemeVariableType.Boolean)
            {
                return html.CheckBox(expression, value.ToString().ToBool());
            }
            else if (info.Type == ThemeVariableType.Select)
            {
                return ThemeVarSelectEditor(html, info, expression, value.ToString());
            }

            return html.TextBox(expression, value);
        }
Пример #12
0
 public static MvcHtmlString CheckBox(this HtmlHelper htmlHelper, String name, Boolean isChecked, Hash htmlAttributes)
 {
     return htmlHelper.CheckBox(name, isChecked, HashHelper.ToStringKeyDictinary( htmlAttributes ));
 }
 public static CheckBoxWrapper CheckBoxWithId(this Browser browser, [NotNull] string id)
 {
     const string checkBoxWithId = "checkbox with id '{0}'";
     var checkBox = browser.CheckBoxes.FirstOrDefault(x => x.Id == id) ?? browser.CheckBox(Find.ById(id));
     return new CheckBoxWrapper(checkBox, String.Format(checkBoxWithId, id));
 }
        private static void AddHeader(this HtmlHelper html,
                                      IList<string> headers,
                                      TagBuilder table,
                                      bool isStandalone,
                                      bool isSelection,
                                      bool withTitle,
                                      bool defaultChecked) {
            if (isStandalone) {
                int pageSize, maxPage, currentPage, total;
                html.GetPagingValues(out pageSize, out maxPage, out currentPage, out total);

                var tagFormat = new TagBuilder("div");
                tagFormat.AddCssClass(IdConstants.CollectionFormatClass);

                tagFormat.InnerHtml += GetSubmitButton(IdConstants.ListButtonClass, MvcUi.List, IdConstants.PageAction, new RouteValueDictionary(new { page = currentPage, pageSize, NofCollectionFormat = IdConstants.ListDisplayFormat }));
                tagFormat.InnerHtml += GetSubmitButton(IdConstants.TableButtonClass, MvcUi.Table, IdConstants.PageAction, new RouteValueDictionary(new { page = currentPage, pageSize, NofCollectionFormat = IdConstants.TableDisplayFormat }));

                table.InnerHtml += tagFormat;
            }

            if (headers.Any() || isSelection) {
                var row1 = new TagBuilder("tr");
                if (isSelection) {
                    var cbTag = new TagBuilder("th");
                    cbTag.InnerHtml += html.CheckBox(IdConstants.CheckboxAll, defaultChecked);
                    cbTag.InnerHtml += GetLabelTag(true, MvcUi.All, () => IdConstants.CheckboxAll);
                    row1.InnerHtml += cbTag.ToString();
                }

                if (withTitle) {
                    var emptyCell = new TagBuilder("th");
                    row1.InnerHtml += emptyCell;
                }

                foreach (string s in headers) {
                    var cell = new TagBuilder("th");
                    cell.SetInnerText(s);
                    row1.InnerHtml += cell;
                }

                table.InnerHtml += row1 + Environment.NewLine;
            }
        }
        private static string CollectionTable(this HtmlHelper html,
                                              IObjectFacade collectionNakedObject,
                                              Func<IObjectFacade, string> linkFunc,
                                              Func<IAssociationFacade, bool> filter,
                                              Func<IAssociationFacade, int> order,
                                              bool isStandalone,
                                              bool withSelection,
                                              bool withTitle,
                                              bool defaultChecked = false) {
            var table = new TagBuilder("table");
            table.AddCssClass(html.CollectionItemTypeName(collectionNakedObject));
            table.InnerHtml += Environment.NewLine;

            string innerHtml = "";

            var collection = collectionNakedObject.ToEnumerable().ToArray();

            var collectionSpec = collectionNakedObject.ElementSpecification;

            var collectionAssocs = html.CollectionAssociations(collection, collectionSpec, filter, order);

            int index = 0;
            foreach (var item in collection) {
                var row = new TagBuilder("tr");

                if (withSelection) {
                    var cbTag = new TagBuilder("td");
                    int i = index++;
                    string id = "checkbox" + i;
                    string label = GetLabelTag(true, (i + 1).ToString(CultureInfo.InvariantCulture), () => id);
                    cbTag.InnerHtml += (label + html.CheckBox(Encode(html.Facade().OidTranslator.GetOidTranslation(item)), defaultChecked, new { id, @class = IdConstants.CheckboxClass }));
                    row.InnerHtml += cbTag.ToString();
                }

                if (withTitle) {
                    var itemTag = new TagBuilder("td");
                    itemTag.InnerHtml += linkFunc(item);
                    row.InnerHtml += itemTag.ToString();
                }

                string[] collectionValues = collectionAssocs.Select(a => html.GetViewField(new PropertyContext(html.IdHelper(), item, a, false), a.Description, true, true)).ToArray();

                foreach (string s in collectionValues) {
                    row.InnerHtml += new TagBuilder("td") { InnerHtml = s };
                }
                innerHtml += (row + Environment.NewLine);
            }

            var headers = collectionAssocs.Select(a => a.Name).ToArray();
            html.AddHeader(headers, table, isStandalone, withSelection, withTitle, defaultChecked);
            table.InnerHtml += innerHtml;

            return table + html.AddFooter(collectionNakedObject);
        }
 private static void AddCheckBox(this HtmlHelper html, TagBuilder tag, RouteValueDictionary htmlAttributes, string id, bool? isChecked) {
     if (isChecked == null) {
         tag.InnerHtml += html.CheckBox(id, htmlAttributes).ToString();
     } else {
         tag.InnerHtml += html.CheckBox(id, isChecked.Value, htmlAttributes).ToString();
     }
     tag.InnerHtml += html.ValidationMessage(id);
 }
 public static MvcHtmlString Boolean(this HtmlHelper html, string name, bool value)
 {
     return html.CheckBox(name, value, new { onclick = "return false;" });
 }
Пример #18
0
    public static string ControlFor(this HtmlHelper helper, string table, string column,object value)
    {
        string result = "";

        //look the table up
        DB db = DB.CreateDB();

        ITable tbl = db.FindTableByClassName(table);

        if (tbl != null) {
            IColumn col = tbl.GetColumn(column);

            if (col != null) {
                //yay - let's work some magic
                if (col.IsPrimaryKey) {
                    //label
                    result = string.Format("<input type=hidden name='{0}' value='{1}' />{1}", column, value.ToString());
                } else if (col.IsForeignKey) {
                    //dropdown
                    //HACK: Work the Controller T4 file to be data aware or create a list of custom controls
                    //TODO: I'm fully aware of the mess this is :) and I need to fix it
                    var fk = db.FindByPrimaryKey(col.Name);
                    List<SelectListItem> items = new List<SelectListItem>();
                    if (fk != null) {
                        using (var rdr = db.SelectColumns(fk.PrimaryKey.Name,fk.Descriptor.Name).From(fk.Name).OrderAsc(fk.Descriptor.Name).ExecuteReader()) {
                            while (rdr.Read()) {
                                var item = new SelectListItem();
                                item.Text = rdr[fk.Descriptor.Name].ToString();
                                item.Value = rdr[fk.PrimaryKey.Name].ToString();

                                item.Selected = item.Value.Equals(value.ToString(), StringComparison.InvariantCultureIgnoreCase);
                                items.Add(item);
                            }
                        }

                    }

                    result = helper.DropDownList(column, items);
                } else if (col.IsNumeric) {
                    //max-length enabled textbox
                    result = helper.TextBox(column, value, new { maxlength = col.MaxLength, size=col.MaxLength });
                } else if (col.IsDateTime) {
                    //jquery date
                    result = helper.TextBox(column, value, new { @class = "datepicker" });
                } else if (col.IsReadOnly) {
                    //disabled textbox
                    result = helper.TextBox(column, value, new { disabled = "disabled" });
                } else if (col.DataType == DbType.Boolean) {
                    //checkbox
                    bool isChecked = false;
                    bool.TryParse(value.ToString(), out isChecked);
                    result = helper.CheckBox(column, isChecked);
                } else if (col.MaxLength > 500) {
                    result = helper.TextArea(column, value.ToString(), new { style = "width:100%;height:50px" });

                } else {
                    result = helper.TextBox(column, value, new { size = col.MaxLength == 0 ? 40 : col.MaxLength });
                }
            }

        }

        return result;
    }
 public static IHtmlString CheckBox(this HtmlHelper html, string id, bool isChecked, string labelText)
 {
     return (html.CheckBox(id, isChecked) + new HtmlElement("label").Attribute("for", id).Html(labelText).ToString()).Raw();
 }
        public static string SearchControl(this Tk5FieldInfoEx field, DataRow dataRow, DataSet dataSet)
        {
            TkDebug.AssertArgumentNull(field, "field", null);

            ControlType ctrlType = field.InternalControl.SrcControl;
            string result = string.Empty;

            switch (ctrlType)
            {
                case ControlType.Combo:
                case ControlType.RadioGroup:
                    result = field.Combo(dataRow, dataSet, true);
                    break;
                case ControlType.Text:
                case ControlType.TextArea:
                    result = field.Input(dataRow, true);
                    break;
                case ControlType.CheckBox:
                    result = field.CheckBox(dataRow, true);
                    break;
                case ControlType.Date:
                    result = field.Date(dataRow, true);
                    break;
                case ControlType.DateTime:
                    result = field.DateTime(dataRow, true);
                    break;
                case ControlType.Time:
                    result = field.Time(dataRow, true);
                    break;
                case ControlType.EasySearch:
                    result = field.EasySearch(dataRow, true);
                    break;
                case ControlType.Label:
                case ControlType.Password:
                case ControlType.RichText:
                case ControlType.Upload:
                case ControlType.CheckBoxList:
                    result = string.Format(ObjectUtil.SysCulture, "{0}的控件类型为{1},不支持查询",
                       field.NickName, ctrlType);
                    break;
            }
            return result;
        }